开发者

Passing object to different windows forms

I want to pass a C# object between win for开发者_高级运维ms. At the moment, I have setup a basic project to learn how to do this which consists of two forms - form1 and form2 and a class called class1.cs which contains get and set methods to set a string variable with a value entered in form1. (Form 2 is supposed to get the value stored in the class1 object)

How can I get the string value from the object that was set in form1? Do I need to pass it as a parameter to form2?

Any comments/help will be appeciated!

Thanks,

EDIT: Here's the code I have at the moment: (form1.cs)

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();

        Form2 form2 = new Form2();

        form2.Show();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
        {
            Class1 class1 = new Class1();

            class1.setStringValue(textBox1.Text);
        }
    }
}

}


There are a few different ways to do this, you could use a static class object, the above example would be ideal for this activity.

public static class MyStaticClass
{
  public static string MyStringMessage {get;set;}
}

You don't need to instance the class, just call it

MyStaticClass.MyStringMessage = "Hello World";
Console.WriteLine (MyStaticClass.MyStringMessage);

If you want an instance of the object you can pass the class object that you create on Form1 into Form2 with the following.

private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form2 form2 = new Form2();
        form2.MyClass = class1;
        form2.Show();
    }

Then create a property on Form2 to accept the class object.

public Class1 MyClass {get;set;}

remember to make the Class1 object a global variable rather than create it within button 2 itself.


Yes, in Form1 you declare an instance of Class1 and then set the parameters as needed, then you pass it to Form2. You could for example have a constructor in Form2 and have a Class1 parameter in it. Assuming that Form1 creates Form2, otherwise you have to have some way for Form1 to find Form2 to pass the instance across.


Since I have been working in ASP.Net the last couple years I have found myself working with Newtonsoft.Json a lot. Turns out to be great within WinForms too, and in this case seemed to simplify passing objects between forms... even complex ones are a breeze!

The implementation is like this:

// Set Object Property within Form
 public partial class FlashNotify : Form
 {
     public string Json { get; set; }
 }

On the form load event you can then grab your object:

 private void FlashNotify_Load(object sender, EventArgs e)
 { 
    // Deserialize from string back to object
    CommUserGroupMessage msg = new CommUserGroupMessage();
    msg = Newtonsoft.Json.JsonConvert.DeserializeObject<CommUserGroupMessage>(Json);
 }

Lastly is passing the object to the form:

// Serialize the Object into a string to pass
 string json = Newtonsoft.Json.JsonConvert.SerializeObject(msg);
 FlashNotify fn = new FlashNotify();
 fn.Json = json;
 fn.Show();

Granted the original selected answer is probably easier, however I like this approach as it avoids the need to replicate the class within your form, which I think makes it easier to maintain (Correction: Miss correctly read the static class in the example, I at first thought it was replicated within the form).


Using Delegate you can easily pass data to a form.

Technically, a delegate is a reference type used to encapsulate a method with a specific signature and return type

Step 1

Add a delegate signature to form1 as below:

public delegate void delPassDataToFrom(Object obj);

Step 2 In form1’s any button click event handler, instantiate form2 class and delegate. Assign a function in form2 to the delegate and call the delegate as below:

private void btnSend_Click(object sender, System.EventArgs e)
{
   Form2 frm = new Form2();
   delPassDataToFrom del = new delPassDataToFrom(frm.retrieveData);
   del(objectToPass);
   frm.Show();
}

Step 3

In form2, add a function to which the delegate should point to. This function will use the object passed:

public void retrieveData(Object objPassedFromParent)
{
  //use the objPassedFromParent object as needed
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜