Form and code not processed

I have a text box with some text in it ("hello"). If I change the text to something else and get the text from the text field (through the code), I will get a "hello", although I changed it.

In another case, when I change the check state of a flag, the flag will not be visually checked (or disabled).

Does anyone have an idea what is happening and how to synchronize them?

I opened a new project, this is the only function in my form:

public void Switch()
    {
        Checkbox1.Checked = !Checkbox1.Checked;
    }

and I call it from program.cs:

static MainForm MyForm;
MyForm = new MainForm();
MyForm.Switch();
+3
source share
1 answer

I assume that you just instantiate MainFormin Mainand then call Switch.

This means that you are not sending a request in the user interface theme.

, Invoke :

public void Switch()
{
    // true if off of the UI thread, and thus must be Invoked
    if (this.InvokeRequired)
    {
        // off UI thread; put it onto it
        this.Invoke(Switch);
    }
    else
    {
        // on UI thread
        Checkbox1.Checked = ! Checkbox1.Checked;
    }
}

. Invoke, , ( API).

0

All Articles