|
Hey, i'm having this problem with one of my labels. I'm attempting to modify a label on a separate class, when i debug it, it shows that the text value has changed, but the label itself doesn't. and yes i have tried the Refresh() method.
|
| Alca7raz Thursday, April 19, 2007 7:01 AM |
Do it like this to handle all eventualities:
private Form2 formRef;
private void nowBtn_Click(object sender, EventArgs e) { if (formRef != null) { formRef = new Form2(this); formRef.FormClosed += Form2Closed; formRef.Show(); } } private void Form2Closed(object sender, FormClosedEventArgs e) { formRef = null; } private void updateLabelText() { if (formRef != null) formRef.updateText("Nobugz waz here"); }
Everything gets to be a lot easier if you use an event. You can now how more than one instance of Form2 and don't have to worry about cleanup:
In Form1: public delegate void NewLabelTextEventHandler(object sender, string text); public event NewLabelTextEventHandler NewLabelText;
private void nowBtn_Click(object sender, EventArgs e) { Form2 f2 = new Form2(this); f2.Show(); } private void updateLabelText() { if (NewLabelText != null) NewLabelText.Invoke(this, "Nobugz waz here"); }
In Form2: public partial class Form2 : Form { public Form2(Form1 mainForm) { InitializeComponent(); mainForm.NewLabelText += updateText; } private void updateText(object sender, string text) { label1.Text = text; } }
|
| nobugz Friday, April 20, 2007 7:09 AM |
Hi,
How r u accessing the lable of one form in another class?
It should work ,if the reference of parent form of label is passed to the second class.
Thanks,
Ch.T.Gopi Kumar. |
| TilakGopi Thursday, April 19, 2007 7:15 AM |
Here is an example of how im doing it:
Code Snippet
Form1 { Form2 formRef = new Form2(this); formRef.updateText(STRING VARIABLE HERE); }
Form2(Form1 mainForm) { internal void updateText(string RecievedText) { Label.Text = RecievedText; Label.Refresh(); } } something along those lines, i dont have my code in front of me at the moment |
| Alca7raz Thursday, April 19, 2007 7:01 PM |
Your "formRef" is wrong. You are creating a new instance of the Form2 class instead of using the existing instance. Since you never call formRef.Show(), that new instance with the updated label text is not visible.
|
| nobugz Thursday, April 19, 2007 7:14 PM |
Ok, ill try to explain a bit more about the program and the problem. the Form2 class has an invisble form covering all of the screen with a label on it. I am showing the form, and the label does show up. the problem i am having is that even with using the Refresh() method, the label isnt changing.Heres a better example of my code:
public partial class Form1 : Form { private void nowBtn_Click(object sender, EventArgs e) { Form2 formRef = new Form2(this); //some code formRef.Show(); } private void updateLabelText() //called from a Timer Tick Event { //some code formRef.updateText(timerText); } }
public partial class Form2 : Form { private Form1 formRef = null; public Form2(Form1 mainForm) { // // The InitializeComponent() call is required for Windows Forms designer support. // this.formRef = mainForm; InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } internal void updateText(string RecievedText) { timerLabel.Text = RecievedText; timerLabel.Refresh(); } }
How would you have me set up my "formRef"? |
| Alca7raz Friday, April 20, 2007 4:29 AM |
| |
public partial class Form1 : Form { private Form2 formRef;
private void nowBtn_Click(object sender, EventArgs e) { formRef = new Form2(this); //some code formRef.Show(); } private void updateLabelText() //called from a Timer Tick Event { //some code formRef.updateText(timerText); } }
public partial class Form2 : Form { private Form1 formRef = null; public Form2(Form1 mainForm) { // // The InitializeComponent() call is required for Windows Forms designer support. // this.formRef = mainForm; InitializeComponent(); // // TODO: Add constructor code after the InitializeComponent() call. // } internal void updateText(string RecievedText) { timerLabel.Text = RecievedText; timerLabel.Refresh(); } }
|
This should work.
Thanks,
Ch.T.Gopi Kumar. |
| TilakGopi Friday, April 20, 2007 5:34 AM |
Do it like this to handle all eventualities:
private Form2 formRef;
private void nowBtn_Click(object sender, EventArgs e) { if (formRef != null) { formRef = new Form2(this); formRef.FormClosed += Form2Closed; formRef.Show(); } } private void Form2Closed(object sender, FormClosedEventArgs e) { formRef = null; } private void updateLabelText() { if (formRef != null) formRef.updateText("Nobugz waz here"); }
Everything gets to be a lot easier if you use an event. You can now how more than one instance of Form2 and don't have to worry about cleanup:
In Form1: public delegate void NewLabelTextEventHandler(object sender, string text); public event NewLabelTextEventHandler NewLabelText;
private void nowBtn_Click(object sender, EventArgs e) { Form2 f2 = new Form2(this); f2.Show(); } private void updateLabelText() { if (NewLabelText != null) NewLabelText.Invoke(this, "Nobugz waz here"); }
In Form2: public partial class Form2 : Form { public Form2(Form1 mainForm) { InitializeComponent(); mainForm.NewLabelText += updateText; } private void updateText(object sender, string text) { label1.Text = text; } }
|
| nobugz Friday, April 20, 2007 7:09 AM |
Thank you. this worked well.
|
| Alca7raz Sunday, April 22, 2007 12:20 AM |