There
are so many cases in which programmer have to pass some values from one form to
another, like he/she has a list form and want to edit that record in
another form, in that case he/she has to pass a unique key of that record so
that record can be processed easily.
We all known about concepts of classes and its constructor.
It is a basic approach in Object Oriented Programming to pass some parameters
through constructor.
In Visual Studio, by default
we have a single form i.e. Form1 in our project, lets add an another form i.e. Form2
and make its constructor same as below:
public Form2(string str = "Nothing has been passed")
{
InitializeComponent();
MessageBox.Show(str);
}
Now
in Form1 create a button and in click event of that button create an object of
Form2 with parameter and finally show that form using Show() method.
private void button1_Click(object sender, EventArgs e)
{
Form2 obj = new Form2("Pass this
parameter");
obj.Show();
}
A
message box will appear with showing the parameter you have passed through
constructor. If you have not passed anything then also a message box will appear showing "Nothing has been passed", because i have set its default value.
There are many another methods to pass
parameters like, using objects, using properties, using delegates etc. In all
these methods I have found that this is so simple and easy method.Download Example.
How to pass value using public property