views:

596

answers:

3

I'm trying to bind a Textbox.Text to Form.Text (which sets the title of the form). The binding itself works. But, the title isn't updated until I move the entire form.

How can I achieve Form.Text being updated without moving the form? I'd like Form.Text being updated directly when I type something in the Textbox.

Edit; I set the title of the Form in a TextChanged event which is fired by a ToolStripTextbox:

public partial class ProjectForm : Form
{
   public ProjectForm()
   {
       // my code contains all sorts of code here, 
       // but nothing that has something to do with the text.
   }
}

private void projectName_TextChanged_1(object sender, EventArgs e)
{
    this.Text = projectName.TextBox.Text;
}

And the Databinding version:

public partial class ProjectForm : Form
{
   public ProjectForm()
   {
       this.projectName.TextBox.DataBindings.Add("Text", this, "Text", true, DataSourceUpdateMode.OnValidation);
   }
}

Edit 2: I see I forgot to mention something. Don't know if it adds anything, but my apllication is a MDI application. The part of the title that changes is:

ApplicationName [THIS CHANGES, BUT ONLY AFTER MOVING/RESIZING]
+1  A: 

What about using the TextChanged event of the TextBox, like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        this.Text = this.textBox1.Text;
    }
}
João Angelo
This results in the same behavior as described in my Question. So the Text is updated when I move/resize the form.
MysticEarth
Don't know what to tell you then, I created a simple application with just that code and I had no problems. It seems to be a case of: "Hey, it works on my machine." Sorry, I can't help you any further...
João Angelo
+2  A: 

Classic problem, you're not updating the existing form's Text property but a new one that is not visible. Call the Show() method after you change the Text.

Source

You can also try invalidating your form in the TextChanged event so it will force a re-paint.

Edit 1: This StackOverflow question may provide an answer for you since you are a MDI application

Edit 2: It could be that this operation is not thread-safe and therefore the UI thread is blocking. Therefore, you need to invoke another function in order to cause it to update. I had a similar problem with StatusBar Labels a while back. Here is some example code if you do not know how to use delegates:

public delegate void updateFormTextD(string text);

private void formText(string text)
{
     this.Text = text;
}

private void updateFormText(string text)
{
     Invoke(new updateFormTextD(formText), text);
}
0A0D
I tried to invalidate the form, doesn't give any other results. The title is only updated when the form is moved or resized.
MysticEarth
See my edit above
0A0D
Thanks for the pointer. But no luck. I see the ActivateMdiChild works, but the title isn't updated (while I directly see the updated title in the list while ALT + TAB...)
MysticEarth
See my edit 2 above
0A0D
Didn't work either :( But thanks nonetheless for helping :)
MysticEarth
I am out of answers at the moment... I google'd everything I could and looked at all my code. Sorry I could not be of any more help.
0A0D
A: 

I had the same problem that almost freaks me out. Eventually I found that my form title update request by "this.text = " was blocked by the method "WndProc(ref Message message)". At the end of WndProc method I added "base.WndProc(ref message)", which passes along all messages to the base also. After that I could update successfully my form title by "this.text = ".

Therefore, I suggest you to look for a method that blocking your form title for being updated.

Albert Jann