开发者

Send data from one form to another

I made a secondary form named Form2 for my application in which the user can specify a Picture, Color and Text.

Now, on this Form2, after the user presses OK button, I want to send the the particulars to Form 1 and open it up. For eg: If the user sets the color=Red, Picture=Img.gif, Text=Hi on Form2, then the TextBox on Form1 should display开发者_运维问答 Hi in red color and a picture in the PicBox in Form1.

How is the data sent form one form to another?


You can do it exactly the same way you would assign values from any class to any other class.

e.g.:

var form1=new Form1();
form1.SetProps(Red,img.gif,"Hi");

Where Form1 obviously needs something like:

public void SetProps(color c, ...)  

You get the point.

A better way of doing that, specifically with forms is having a constructor in form2 which receives the arguments from Form1, and calling it before closing Form1:

i.e. in Form2:

var form1 = new Form1(Red,img.gif,"Hi");      
this.Close();
form1.Show();  

(don't get me on the syntax..)


This could be done in several ways. One of them could be to pass an object as parameter in the constructor of Form2. This object could be a class with some properties to store the values collected by Form2. As the object will be passed as reference from the application to Form2, the application will have available this values as soon as Form2 updates them.

Another way could be to add some public properties to Form2 to store the values collected by Form2. As Form2 is created and referenced by the application, the application should have access to those properties as long as it keeps referenced Form2.

Once the application have the values from From2, it can pass then to the another form easily.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜