Ok, this one’s easy for you guys, I basically have a C# winform app, all it has is a RichTextBox, and there’s a class called Terminal, inside that class there’s a RichTextBox data memeber and some other things, I pass the RichTextBox of my form to the constructor of Terminal, like this:
public Terminal(RichTextBox terminalWindow)
{
this.terminalWindow = terminalWindow;
CommandsBuffer = new List<string>();
currentDirectory = homeDirectory;
this.terminalWindow.TextChanged += new EventHandler(terminalWindow_TextChanged);
InsertCurrentDirectory();
}
this is what I’m doing in the InsertCurrentDirectory() method:
private void InsertCurrentDirectory()
{
terminalWindow.Text = terminalWindow.Text.Insert(0, currentDirectory);
terminalWindow.Text = terminalWindow.Text.Insert(terminalTextLength, ":");
terminalWindow.SelectionStart = terminalTextLength + 1;
}
As you can see I’ve registered the event before calling this method, but the problem is, the event is not firing even though I’m changing the text from inside this method.
But it actually fired when I changed the text in the constructor right after registering the event, like this for example:
public Terminal(RichTextBox terminalWindow)
{
// ...
this.terminalWindow.TextChanged += new EventHandler(terminalWindow_TextChanged);
this.terminalWindow.Text = "the event fired here";
}
This is the TextChanged event just in case you wanna know what’s in it:
void terminalWindow_TextChanged(object sender, EventArgs e)
{
terminalTextLength = terminalWindow.Text.Length;
}
why did this happen? why didn’t the event fire from inside the method? how can i fire it?
Thanks.
Probably you create your Terminal class inside your form constructor (After the InitializeComponenet).
At this point in time the form handle and all the handles for your controls exist only in the framework infrastucture, but not in the windows system and thus no message (TextChanged) will be fired.
If you create your Terminal class inside the Form_Load event the TextChanged of the RichTextBox will be called without problems.