Form1 F1 = new form1 ();
F1.parameter ["XXX"] = "XXX"; (string)
F1.show ();
When this value is used in F1
String S = type (Me. Parameter ["XXX"], "string ")
VB. NET syntax
The sentence I wrote may be incorrect.
But that's all.
Later, I checked that this DLL turns out to be a hashtable parameter.
A value passing method between forms like this can be used to transmit values of different types.
My question is: how should I write this parent form so that all the forms that inherit it can implement the method I mentioned to pass values ??
In fact, parameter is only an attribute provided by form1. You can also implement it yourself. For example:
Public class clsparameters
{
Arraylist arrparameters;
Public clsparameters ()
{
Arrparameters = new arraylist ();
}
Public object this [int Index]
{
Get {
If (I> = 0 & I <arrparameters. Count)
Return arrparameters [I];
Else
Throw new applicationexception ("invalid Index ");
}
Set {
If (I> = 0 & I <arrparameters. Count)
Arrparameters [I] = value;
Else
Throw new applicationexception ("invalid Index ");
}
}
}
Add the following to your form1:
Public clsparameters parameters = new clsparameters ();
You can use this. Parameters [I] for access.
Or
F1.parameter ["XXX"] = "XXX ";
I have come up with a method.
Value transmitted by form1 to form2
Declare a hashtable in form2
Instantiate in the form2 Constructor
Write value in form1
In form2
Public hashtable parameter = NULL;
Public void form2 ()
{
Parameter = new hashtable ();
}
Form1
Form2 F2 = new form2 ();
F2.parameter. Add ("XXX", "XXX ");
F2.show ();
Write the value in form2 in this way.
String S = This. Parameter ["XXX"]. tostring ();
MessageBox. Show (s );
Method 2:
Step 2: Add a form form2 on the solution, and add textbox1, textbox2, and button1. Set the title of button1 to "OK" and the dialogresult attribute to "OK ";
Step 2: Add two private fields: _ username and _ password in the form2 class code, and add two public attributes: username and password. The Code is as follows:
Public partial class form2: Form
{
Private string _ username;
Private string _ password;
Public String Username
{
Get
{
Return _ username;
}
Set
{
_ Username = value;
}
}
Public String Password
{
Get
{
Return _ password;
}
Set
{
_ Password = value;
}
}
.....
}
Step 2: Demonstration of mutual value transfer between forms
Add a botton1 and a combox1 in form1, and use the following code in the botton1_click function:
Private void button#click (Object sender, eventargs E)
{
Form2 myform2 = new form2 ();
Myform2.username = "Richard"; // form1 transfers a value to form2 !!!
Myform2.password = "pwd1234 ";
Dialogresult result = myform2.showdialog ();
If (result = dialogresult. OK)
{
Combobox1.items. Add (myform2.username); // form2 transmits a value to form1 !!!
Combobox2.items. Add (myform2.password );
}
}
To fully demonstrate the above functions, you also need to complete the form2 Code as follows:
1st. Add the following code to form2_load:
Private form2_load (Object sender, eventargs E)
{
Textbox1.text = _ username;
Textbox2.text = _ password;
}
2nd. Add the following code to the textchanged events of textbox1 and textbox2:
Private void textbox1_textchanged (Object sender, eventargs E)
{
This. Username = textbox1.text;
}
Private void textbox2_textchanged (Object sender, eventargs E)
{
This. Password = textbox2.text;
}