I have an MDI form with two forms in it. When i click a button in MDI form, my form F1 Opens.
My question is given below.
When i click a button B1 in formF1, it opens the other form F2. i did this. IN this form F2, there is a combobox with a set of values.
My task is when i select a value in the combox and click the button B2 enclosed in that form, the value selected should go as a string in FOrm F1. Since i am new to dot net, Can you please take some time and give me a sample code so that i customize myself.
Thanks
Guest UserPosted Jan 13, 2009, 5:04 PM
private void btnShowF1_Click(object sender, EventArgs e)
{
F1 f1 = new F1();
f1.MdiParent = this;
f1.Show();
}
In form F1, which has a button "btnShowF2" and a label "lblState":
private void btnShowF2_Click(object sender, EventArgs e)
{
F2 f2 = new F2();
f2.MdiParent = this.MdiParent;
f2.Show();
}
public void setLabel(string labelText)
{
this.lblState.Text = labelText;
}
In form F2 which has a combo box named "cmbStates":
private void cmbStates_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < this.MdiParent.MdiChildren.Length; i++)
{
if (this.MdiParent.MdiChildren[i] is F1)
{
F1 f1 = (F1)(this.MdiParent.MdiChildren[i]);
f1.setLabel(cmbStates.Text);
break;
}
}
}
When you select a new value in F2's combo, it will change the label text on the first F1 form it finds (since it's an MDI form, you could have more than one F1!) If you remove the "break" statement, the label in all F1's will be updated.
Guest UserPosted Jan 14, 2009, 11:26 AM
public void setStatusText(string newStatus)
{
this.toolStripStatusLabel.Text = newStatus;
}
Then, in form F2, I added the following code to the combo box's SelectedIndexChanged event handler shown above:
if (this.MdiParent is MDIParent1)
((MDIParent1)this.MdiParent).setStatusText(cmbStates.Text);
Now when you select a different value in F2's combo box, you should see the label on F1 change and also see the status bar text on the MDI form change to the same value.
Pierre CornelissenPosted Jan 14, 2009, 8:41 AM
Thanks
Pierre
Niradhip ChakrabortyPosted Jan 13, 2009, 5:10 PM
check out the following URL:
1.http://bytes.com/topic/net/answers/629914-how-do-i-pass-value-one-form-another-form-net-windows-application
2.http://bytes.com/groups/net-c/650115-how-call-event-another-form-c-windows-forms
3.http://www.c-sharpcorner.com/UploadFile/alindag/HowToPassValuesBetweenWindowsForms.htm02242006061520AM/HowToPassValuesBetweenWindowsForms.htm.aspx?ArticleID=519479e8-d064-44a1-84b1-5ffc033dbbf3