Home>

Summary

General projects are made up of multiple forms, and data is often flexibly transferred between forms.Let's share some of my experiences below:

There are many ways to pass values ​​to a form.Here are just some of the ones I have used,I do n’t know what the official call these methods,You can also look for others.

Passing values ​​through the constructor

This is the easiest way,For example, I want to pass a string from form1 to form2

First, make a slight modification in the constructor of form2:

public form2 (string s)
 {
 initializecomponent ();
 this.labelrecieve.text=s;
 }

Added a string parameter s

Then, in the click event of form1, pass in the parameters to be passed,The following snippet:

private void buttonshow_click (object sender, eventargs e)
 {
 ...
 form2 f2;
 if (textboxsend.text == "")
 {
 f2=new form2 ("i" m from form1 ");
 }
 else
 {
 f2=new form2 (textboxsend.text);
 }
 dialogresult r=f2.showdialog ();//As for show and showdialog, there is no need to say any more
 ...
 }
</p>
<p>

When the click event is triggered,form2 receives the textboxsend.text or "i" m from form1 "passed by f1 and uses labelrecieve to accept it.

This method,Although simple and convenient,But after all the functions are limited,When a large amount of diverse data needs to be passed,Constructors are not enough.

Passing by attributes

Attributes can easily pass multiple data (isn't it just to add multiple attributes?), Suppose I want to pass a certain string from form2 to form3. First we add an attribute for form3 called mystring

public form3 ()
 {
 initializecomponent ();
 }
 public string mystring
 {
 get
 {
 return this.textboxf3.text;
 }
 set
 {
 if (value == null)
 textboxf3.text=string.empty;
 else
 textboxf3.text=value;
 }
 }

Use this property to set the text of the text box.

Then, in the click event of form2,Just make

f3.mystring="i" m from form2 ";

This is also very convenient,It is worth mentioning that,When passing through attributes like this,Data can be transferred from form2 to form3 and back from form3. Such as

if (r == dialogresult.ok)
 {
 f3.mystring="i" m from form3 ";
 this.labelrecieve.text=f3.mystring;
 //When form3 is closed, save mystring to form2's label.
 }

Attribute passing is the most commonly used method.

Passing parameters with events

Frankly, I have rarely used this yet,To be precise, this should be called event and attribute passing value.

Now I took some operations in form4,Assuming form4 is part of the type library we developed,When I want to publish it to other colleagues,We can use such events to pass values.

First, in form4, add definitions of event parameter types and event handler delegates:

public class myeventargs:eventargs
 {
 string mystring;
 public string mystring
 {
 get
 {
 return mystring;
 }
 set
 {
 if (value!=null)
 mystring=value;
 else
 mystring=string.empty;
 }
 }
</p>
<p>
public myeventargs (string s)
 {
 this.mystring=s;
 }
 }
</p>
<p>
//Delegation of event processing method
 public delegate void myeventhandler (object sender, myeventargs e);

Then, we trigger such a public event myclick in the click event

public event myeventhandler myclick;
 private void buttonclick_click (object sender, eventargs e)
 {
 if (this.myclick!=null)
 this.myclick (this, new myeventargs (textbox4.text));
 this.dialogresult=dialogresult.ok;
 }

We encapsulate the text in textbox4 into an event parameter object such as myeventargs.In this way, it will be passed to form3 with the event. At the same time, we will add an event processing method in form3.

void f4_myclick (object sender, form4.myeventargs e)
 {
 mystring=e.mystring;
 this.textboxf3.text=string.format ("from f4:{0}", e.mystring);
 }

In the click event we call this method through the delegate

form4 f4=new form4 ();
 f4.myclick +=new form4.myeventhandler (f4_myclick);

After the myclick event of the f4 object is triggered,Will call the f4_myclick method here, we can use this method to get data from the event parameters,Here is put into textboxf3.

Just write here,What is described in the article are the most basic forwarding,Of course, the parameters passed can be various,You can also use generic delegation,You can also "pass" the constrol in one form to another form.

c
  • Previous Javascript implementation supports mobile device galleries
  • Next C # yield wrong usage in WCF (1)