开发者

Call panel.invalidate outside form class in C#

I need to call "panel.invalidate" outside my form (WINform) class also I need to change some other controls as well, I read similar question here, and tried what they said, but it didn't work开发者_开发技巧 and I wasn't convinced at all.

The answer I read was about exposing a public method like this:

public void EnableButton(bool enable)
{
    this.myButton.Enabled = enable;
}

Also I made a static instance in the other file

 static Form1 myForm = new Form1();

Any useful suggestions??


The problem is the "myForm" reference. It is a reference to an instance of Form1 that isn't visible and doesn't match the one that the user is looking at. It can't be a match, you created a new one.

Whatever class needs to update the form must have a constructor that takes a Form1 reference. You can create the class object in your Form1 constructor or Load event, pass "this". Using Application.OpenForms[0] is another way to get the reference, one you should not use.


Are you updating from the same thread? Otherwise you might need to use Invoke. Here's a nice short article about how to do that:

http://blogs.msdn.com/csharpfaq/archive/2004/03/17/91685.aspx


Control.Invalidate() is a public method, but the control itself is most likely not public. You will have to expose the call to Control.Invalidate() through a public facing method in your form or by marking the control in question as public.

public class MyForm : Form {
    private TextBox tbxName = new TextBox();

    public InvalidateTextBox() {
        tbxName.Invalidate();
    }
}

OR

public class MyForm : Form {
    public TextBox tbxName = new TextBox();
}

public class SomeOtherClass {
    public void InvalidateTextBox(MyForm form) {
        form.tbxName.Invalidate();
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜