C # Four Methods for directly passing parameters in two forms

Source: Internet
Author: User

There are many methods for directly passing parameters in two forms, which makes it very difficult. Actually, it is not that difficult.

PASS Parameters

Method 1: declare the control as public.

Method 2: Use an intermediate class.

Method 3: Use public static to declare the variable to be transmitted

Method 4: Use a constructor to transmit data.

Generally, these four methods are enough.

Example

For example, if I have purchased an inventory software, I need to obtain the cargo number from the cargo information when purchasing the product (the user cannot enter the number ). The cargo information is another form. How can I click the get cargo number button (in the purchase form) to play the shipping object information form, then, select the desired goods from the goods information form and return it to the goods form?

Assume that the purchase form is purchase. cs.

The goods form is material. cs.

In the purchase. cs form, click the get cargo number button and write the following code in the event:

Material m = new material ();
M. ShowDialog ();
If (m. DialogResult = DialogResult. OK)
{
Txtinmid. Text = m. getmid;

// Getmid is a material constructor. txtinmid is the text box that displays the cargo number in purchase. cs.

M. Close ();
}
GetbaseMaterialinfo (); // a method of its own

Then write the following code in the material. cs form.

Public material () // default constructor
{
InitializeComponent ();
}
Public string getmid // this is the case.
{
Get
{
Return dgvmaterial. Rows [dgvmaterial. CurrentRow. Index]. Cells [1]. Value. ToString ();
}
}

This is the activation method (I double-click the datagridview line)

Private void datagridview_CellMouseDoubleClick (object sender, DataGridViewCellMouseEventArgs e)
{
This. DialogResult = DialogResult. OK;
}

In this way, the displayed goods number button in the purchase. cs form will be able to get the goods you selected. Simple.

Related Article

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.