Changing the label of from2 from within the function of form1
Ok, so I have Form1 calling Form2 in a way that allows me to access Form1's functions from Form2. Code Below...
Form1....
private void btnShowForm2_Click(object sender, EventArgs e)
{
frmForm2 tempDialog = new frmForm2(this);
tempDialog.ShowDialog();
}
Form2...
开发者_StackOverflow private frmForm1 _parent;
public frmForm2(frmForm1 frm1)
{
InitializeComponent();
_parent = frm1;
}
private void btnDoFunction_Click(object sender, EventArgs e)
{
_parent.DoProcess();
}
Now I have a new problem. I'm trying to update a status label of Form2, but the function that's processing the task at hand is in Form1. How can I change the label of Form2 from within the Function of Form1?
You can do so using delegate and event.
- Make an event in form1 using a delegate
- On progress in form1's process trigger the event.
- put a handler to the form1 event in form2.
- Extract the progress from that eventargs implemented object in the handler.
- Show it in the lable in form2
This is a mess... form1 displays form2 which calls a method on form1 which updates the contents form2? Your question itself is easy (move the tempDialog variable to the form1 class instead of within this method - then you can use this during DoProcess to tweak whatever controls you want) but there is a serious maintainability problem here that you should step back and try to address.
If you really want to access frm2.lbl from frm1 then frm2.lbl must be public, this can be changed in the properties window of the label on Form2. (under modifiers I think)
private void btnDoFunction_Click(object sender, EventArgs e)
{
_parent.DoProcess();
}
(in form1)
public void DoProcess();
{
tempDialog.lable.Text = "hope this works";
}
精彩评论