I will be doing this tutorial on the method of using the constructors to pass the data. I choose this method because, in my opinion, it is easier to understand, and it also uses less code.
I will be doing this tutorial using C#, .Net 2.0, and Visual Studio 2005.
So we will start by creating a new project in Visual Studio.
1. Open Visual Studio
2. File --> New --> Project
3. Visual C# --> Windows --> Windows Application
4. Give the project a descriptive name, or leave it as the default. I will be naming mine "PassingDataExample".
5. Click OK.
Next, I will rename the form to give it a more descriptive name. I will name it "frmParentForm".
Now, we will add controls to the form. I normally do this using code, but you can use the designer if you like.
Code for the Parent Form's local class variables for the textbox and button. Also the Parent Form's constructor, which is added by default, and the Parent Form's Form_Load event that will call a method to create the controls.
// If you use the designer to create the controls,
// you will NOT need these two declarations
TextBox textBox1;
Button btnSendToNextForm;
public frmParentForm()
{
InitializeComponent();
}
private void frmParentForm_Load(object sender, EventArgs e)
{
// If you use the designer to create the controls,
// you will NOT need this method
AddControlsToForm();
}
If you use the designer to create the controls, you will NOT need this method.
// Adds a textbox and button control to the form.
private void AddControlsToForm()
{
textBox1 = new TextBox();
textBox1.Name = "textBox1";
textBox1.Location = new Point(91, 78);
textBox1.Size = new Size(100, 20);
btnSendToNextForm = new Button();
btnSendToNextForm.Text = "Send";
btnSendToNextForm.Name = "btnSendToNextForm";
btnSendToNextForm.Location = new Point(101, 129);
btnSendToNextForm.Size = new Size(75, 23);
btnSendToNextForm.Click += new EventHandler(btnSendToNextForm_Click);
this.Controls.Add(textBox1);
this.Controls.Add(btnSendToNextForm);
this.ActiveControl = textBox1;
}
Next, we are going to create a second form:
1. Project --> Add Windows Form
2. Rename the form. I am going to rename it to "frmChildForm".
3. Click Add.
I am going to re-use code from the Parent form's AddControlsToForm method(which a few changes). Again, if you want to use the designer to create the controls, knock yourself out.
Code for the Child Form's local class variables for the textbox and button. Also the Child Form's constructor, which is added by default. There is no Form_Load because it is not needed.
TextBox textBox1;
Button btnClose;
public frmChildForm()
{
InitializeComponent();
}
If you use the designer to create the controls, you will NOT need this method. Notice the few differences.
// Adds a textbox and button control to the form.
private void AddControlsToForm()
{
textBox1 = new TextBox();
textBox1.Name = "textBox1";
textBox1.Location = new Point(91, 78);
textBox1.Size = new Size(100, 20);
btnClose = new Button();
btnClose.Text = "Close";
btnClose.Name = "btnClose";
btnClose.Location = new Point(101, 129);
btnClose.Size = new Size(75, 23);
btnClose.Click += new EventHandler(btnClose_Click);
this.Controls.Add(textBox1);
this.Controls.Add(btnClose);
this.ActiveControl = textBox1;
}
Next, we are going to add a new constructor to the Child Form. The new constructor will accept a string value which will represent the text from the textbox on the Parent Form. We are also going to add a new declaration for a string variable, and set the text of the textbox to the string value.
TextBox textBox1;
Button btnClose;
[b][size=4]string textFromParent = string.Empty;[/size][/b]
public frmChildForm()
{
InitializeComponent();
}
[b][size=4]public frmChildForm(string text)
{
InitializeComponent();
textFromParent = text;
AddControlsToForm();
textBox1.Text = textFromParent;
}[/size][/b]
Next, we are going to add code to the Click_Event of btnSendToNextForm on the Parent Form. This code will create an instance of the Child Form, and populate it's constructor with the value from the textbox. It will show it as a dialog box.
void btnSendToNextForm_Click(object sender, EventArgs e)
{
frmChildForm newChildForm = new frmChildForm(textBox1.Text);
newChildForm.ShowDialog();
newChildForm.Dispose();
}
And the last piece of code is the Click_Event of btnClose on the Child Form.
void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
And there you go. You have now passed data between two forms.
I have also attached the solution.
Attached File(s)
-
PassingDataExample.zip (40.33K)
Number of downloads: 4500




MultiQuote






|