开发者

Connection between forms

I'm programming in c# Express 2010, and I have many forms in the program. I want one form be able to see the variables and methods in another form such as Form 8 can see and use the methods defined in form 5, should I define an object of type form 5 in form 8? how does a form can see variables i开发者_高级运维n another form in c#?

Thanks


You can do two things here:

1) As @Yuck suggested, creating a class that will contain the members you need to share between those two forms, and pass an instance of that class when creating a new instance of a Form (in the constructor, or through a property, etc)

2) Create a static class that will have the required fields in such a place where both forms have access, and then you will be able to do:

Information.Field1
Information.Field2
Information.Method1()

supposing the class is Information and some properties are Field1, Field2 and a method Method1().

Of course, the second option isn't always possible, because it could depend on non-static fields, etc.

Edit:
Another thing to consider is to pass that object within an event. You can create an event in one of the forms, and suscribe to it in the other form.

Of course, that will depend on what exactly you need to do, but those 3 are valid options to share an object between two forms.


Create a class with the shared values you need, then pass instances of that class between the forms as a constructor argument. I can't create a code sample as you didn't mention what data elements are required.


This doesn't sound like a very good design. For the purpose of your question, you can pass an instance of form1 to form2 via a constructor when instantiating form2, and then form2 will have access to all of form1's public fields. Tell us a little more about what you are trying to do, there is probably a better way without having to pass around instances of your forms and their data.


Use Application.OpenForms to get all open forms in your application then by iterating over it like

  foreach (var frm in Application.OpenForms)
        {
            if (frm.GetType() == typeof(Form2))
            {
                (frm as Form2).Method1();
            }

        }

You can access each form and by casting it original type you can use public variable and methods of this form.


You don't need to create a new class to hold the shared (global) values. You can simply define those values as properties of whatever form they are in (with appropriate getters and setters) and refer to them as SomeFormOrAnother.ThisProperty. This is slightly better than simply making the form variables themselves public.

Mind you, for a complex application this is likely not the best design, but for something relatively simple it's quick and effective.


There are a lot of different ways to handle communication between forms. Since you describe this as a questionaire of sorts, I'm going to assume that the second form is one which you're showing modally (meaning you can't click the form underneath while it's open), on which you collect some functionally related stuff and add it all up to display in a simpler format back on your main form.

Assuming you're showing the second form from a button click, put code like this in the button click method:

using (ChildForm child = new ChildForm())
{
    child.ShowDialog(this); // the "this" is important, since it lets 
                            // the child reference the parent form in 
                            // a simple way

    // the code here will block (or stop) on the above line until the user
    // closes the form. Then you can still reference the child's properties,
    // even though it's no longer visible.
    string something = child.something;
}

To make the child form's something property visible, you declare it as public in the child form, like so:

public string something = String.Empty;

According to many, this is wrong - you should make something accesible like this:

private string _something = String.Empty;
public string something
{
    get
    {
        return _something;
    }
}

I'll leave that debate to others. In your child form, whenever you need to refer back to a method in the parent form (you have to make any such method public), you do it like this:

ParentForm parent = (ParentForm)this.Owner;
int someValueOrOther = parent.callSomePublicMethod();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜