Reputation: 571
I put some colored text to my rich text box my using the following code:
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectedText = "Name";
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectedText = ": some message.";
But when I hide the richtextbox from the user by setting its parent property to null (I have this panel that holds different rich text boxes from time to time), and put it back, the rich text box does not retain the text colors I applied to it. All texts becomes black.
Update: I tried an experiment. In my main program, I have a UserControl (which has a Panel) where I put a RichTextBox with colored text. I have many RichTextBoxes which I store to a HashTable.
So when I need a RichTextBox, I retrieve it from my HashTable, put some colored text into it, put it inside my UserControl's Panel and finally put my UserControl to my program's Form. My UserControl can actually be temporarily removed from the program's Form when a user clicks on a button, I do using Controls.Remove(). To put it back into my Form, I use Controls.Add(). The problem is, when the UserControl is added back, the RichTextBox's texts are not colored anymore.
I tried doing something similar in another experimental program.
public partial class Form1 : Form
{
private chat.UserControl1 ChatWindowKuno = new chat.UserControl1();
private Hashtable htChatLogs = new Hashtable(30);
public Form1()
{
InitializeComponent();
createRTBox();
}
private void createRTBox()
{
RichTextBox richTextBox1 = new RichTextBox();
richTextBox1.Multiline = true;
richTextBox1.Dock = DockStyle.Fill;
richTextBox1.ReadOnly = true;
richTextBox1.BackColor = SystemColors.Window;
htChatLogs.Add("Basta", richTextBox1);
}
private void button1_Click_1(object sender, EventArgs e)
{
if (ChatWindowKuno.Parent == null)
ChatWindowKuno.Parent = tabPage2;
else
ChatWindowKuno.Parent = null;
}
private void button2_Click(object sender, EventArgs e)
{
// Clear all text from the RichTextBox;
RichTextBox richTextBox1 = (RichTextBox)htChatLogs["Basta"];
richTextBox1.Clear();
richTextBox1.SelectionFont = new Font("Segoe UI", 8.25F, FontStyle.Regular);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectedText = "Xel";
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectedText = ": Listening to Be My Last by Utada Hikaru.";
richTextBox1.SelectionColor = Color.Gray;
richTextBox1.SelectionFont = new Font("Segoe UI", 8.25F, FontStyle.Italic);
richTextBox1.SelectedText = " [5:56pm] \n";
richTextBox1.SelectionColor = Color.Gray;
richTextBox1.SelectedText = "[5:56pm] ";
richTextBox1.SelectionFont = new Font("Segoe UI", 8.25F, FontStyle.Regular);
richTextBox1.SelectionColor = Color.Blue;
richTextBox1.SelectedText = "Xel";
richTextBox1.SelectionColor = Color.Black;
richTextBox1.SelectedText = ": Listening to Be My Last by Utada Hikaru.";
}
private void button3_Click(object sender, EventArgs e)
{
RichTextBox richTextBox1 = (RichTextBox)htChatLogs["Basta"];
ChatWindowKuno.ChatLog = richTextBox1;
}
}
The ChatLog property of usercontrol1 is this:
public Control ChatLogPanel
{
get
{
return panel1.Controls[0];
}
set
{
panel1.Controls.Clear();
panel1.Controls.Add(value);
}
}
I click the 3 buttons randomly in my experimental program, but the text colors are retained.
Upvotes: 0
Views: 2227
Reputation: 57210
You shouldn't use Parent
property to hide, but Visible
property instead.
If you hide a richtextbox using richTextBox.Visible = false
it keeps its formatting (tested).
EDIT :
as discussed in the comments below, I suggest you to use only one RichTextBox
and store several Rtf
strings in a Dictionary
(or Hashtable
) to mimic the existence of different RichTextBox
'es.
An example of what I mean can be found Here
Upvotes: 4