Two winforms and two webforms

Source: Internet
Author: User

In our daily WinForm development, we will certainly encounter two processes of passing values to each other. However, the general introduction is superficial and there is no good introduction.

We usually hear the use of constructor and attribute to transmit values. Some say that the use of delegate events to transmit values cannot be achieved due to the limited level of the publisher, however, I have never tried to use static fields to pass values. Now I will introduce the two frequently-used methods for passing values:

First, use the constructor to pass the value. The function of passing the value is understandable. It must be that the form assigns the value to the attribute in another form control during initialization, I now place a TextBox1 and Button1 controls in the parent form, and send a click event Button1_Click

Code
Private void button#click (object sender, EventArgs e)
{
String s = textBox1.Text. Trim ();
Child frmChild = new Child (s );
FrmChild. ShowDialog ();
If (frmChild. DialogResult = DialogResult. OK)
{
TextBox1.Text = frmChild. ReturnValue;
}
}

 

Analyze the code above. This is the parent form. We pass the s value to the subclass form and display it in the mode form at the same time. I 'd like to introduce the mode form and non-mode form, the mode form is triggered, and the parent form cannot be operated at the moment. The non-mode is the opposite. In this way, the mode form can return values. When the DialogResult value of the mode form is set to DialogResult, we can get the pattern form attribute here. As to why we need to customize the attribute, this should be a problem of face-to-face encapsulation.

Next, I will paste the Child. CS code of the subform.

Code
Public partial class Child: Form
{
Public Child (string s)
{
InitializeComponent ();
TextBox1.Text = s;
}

/// <Summary>
/// Custom values are used to return values.
/// </Summary>
Public string ReturnValue
{
Get
{
Return textBox1.Text. Trim ();
}
}

Private void textboxshortdoubleclick (object sender, EventArgs e)
{
This. DialogResult = DialogResult. OK;
This. Close ();
}
}

We have customized a property value ReturnValue and used the get method. Of course, I only placed textBox1 controls on the form, which inspired
Textboxshortdoubleclick event, set the DialogResult of this form to OK, which is also the default value, and then close this, but it is not really closed, in. net has a garbage collection mechanism to recycle. Only when the collection mechanism enumerates it for a period of time does not need to call the Terminator, and then calls the destructor.

It is clear that they have different methods for passing values and different uses. constructor is used to pass new values to another form, and the mode form is used to return values. in fact, a simple example involves many things, the number of instance structures, the role of attributes, and the mode form.

 

Check whether the passing of WinForm is satisfactory. Continue with the two webforms.

 

When we go to 51job.com, whether or not to select a region will pop up a beautiful form and click it. This means that they use the modal window, and the name is changed here, not the mode, I do not know the reason. I prefer to use the code of the parent page to facilitate the speech.

Code
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> modal form parent form </title>
<Script type = "text/javascript">
Function B _click ()
{
Var obj = new Object (); // construct an Object
Var btnvalue = window. showModalDialog ("child.htm", obj );
Document. getElementById ("Button1"). value = btnvalue;
}
</Script>
</Head>
<Body>
<Input id = "Button1" type = "button" value = "region selection" onclick = "B _click ()"/>
</Body>
</Html>

A simple button to stimulate the click event, construct an object, display the modal form, and obtain the return value.
Paste a subpage code below

Code
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> modal form subform </title>
<Script type = "text/javascript">
Function returnvalue (id)
{
If (id = 0)
{
Var returnvalue = document. getElementById ("li1"). innerHTML;
Window. returnValue = returnvalue;
Window. close ();
}
If (id = 1)
{
Var returnvalue = document. getElementById ("li2"). innerHTML;
Window. returnValue = returnvalue;
Window. close ();
}
If (id = 2)
{
Var returnvalue = document. getElementById ("li3"). innerHTML;
Window. returnValue = returnvalue;
Window. close ();
}
If (id = 3)
{
Var returnvalue = document. getElementById ("li4"). innerHTML;
Window. returnValue = returnvalue;
Window. close ();
}
}
</Script>
</Head>
<Body>
<Ul>
<Li id = "li1" onclick = "returnvalue (0)"> Beijing </li>
<Li id = "li2" onclick = "returnvalue (1)"> Guangdong </li>
<Li id = "li3" onclick = "returnvalue (2)"> Hangzhou </li>
<Li id = "li4" onclick = "returnvalue (3)"> Jiangxi </li>
</Ul>
</Body>
</Html>

 


Different values are obtained based on the id of the non-graph.

I will not talk about anything else. I will upload the code.

/Files/izxp/WinForm_01.rar/Files/izxp/WinForm_01.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.