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.