[RESOLVED] Going nuts about changing a label text...
I have tried for like 3 hours now to change the text of label, but I haven't succeeded.
I have tried numerous ways, Invoking, Defining, Subs etc. etc.
I have 2 forms. Form1 and Form2. Form2 is opened by Form1 via ShowDialog. When click a button on Form2 I to change the text of the label on Form1. Form2 also gets closed by that button.
And what I've found out, the text gets changed, but It doesn't appear. The control don't want to display it too me :mad:
Pleeease anyone, I seriously need help :(
Re: Going nuts about changing a label text...
Can you show us the code ?
Re: Going nuts about changing a label text...
I only have code from the latest method I tried, "Subbing":
Form1 is MKVC, Form2 is Settings
I call this in the buttonclick on Form2:
Code:
MKVC form1 = new MKVC();
form1.CalcChapNumber(MKVC.duration);
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
Here is the CalcChapNumber sub:
Code:
public void CalcChapNumber(int runtime)
{
decimal count = runtime / trackBar1.Value;
if (count < 0)
{
MessageBox.Show("Too high interval!");
trackBar1.Value = Properties.Settings.Default.defChapInterval;
}
else
{
decimal roundoff = Math.Floor(count);
if (Properties.Settings.Default.firstChap00)
{
roundoff += 1;
}
lblChapterCount.Text = roundoff.ToString();
}
I have solved it temporarily now by setting a oublic variable on form1 from form2 and then using the DialogResult thingy to check if it should use that variable, and it works. The label changes. But I feel I would like to learn the real way of doing this, and I also need to call a sub on form1 from form2.
Re: Going nuts about changing a label text...
the reason you don't see the label get updated is because you create new instance of form1...
new instance meaning another instance of form1.. you need to pass the original instance of from1 and changes it's.
Re: Going nuts about changing a label text...
ok I made small example for you:
Form1:
Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// public method for changing the label
public void ChangeLabel(string message)
{
this.label1.Text = message;
}
private void Form1_Load(object sender, EventArgs e)
{
// passing the original instance of Form1 to Form2 Constructor
Form2 f = new Form2(this);
f.Show();
}
}
and now form 2:
Code:
public partial class Form2 : Form
{
//declare private member of form1 instance
private Form1 _MainForm { set; get; }
public Form2()
{
InitializeComponent();
}
// constructor that gets Form1 as parameter and set it to From2 private member which we //delcared above
public Form2(Form1 MainForm)
{
this._MainForm = MainForm;
}
private void Form2_Load(object sender, EventArgs e)
{
// if _MainForm is not null change Form1 label
if (_MainForm != null)
{
_MainForm.ChangeLabel("Hello World");
}
}
}
Re: Going nuts about changing a label text...
thank you I will take a look and try to learn! :)
Re: Going nuts about changing a label text...
I looked through the code and used it, but there is one problem.
Are you really passing the original instance when you declare:
Form2 f = new Form2(this);
Feels like the "new" thingy makes it a new instance, and in fact, the form that shows up if I after that call f.ShowDialog(); is just an empty new form :S
Re: Going nuts about changing a label text...
the new is creating new instance of Form2, the "this" is the instance of Form1.
Re: Going nuts about changing a label text...
Okay now I see, but why won't the controls show up then?
Re: Going nuts about changing a label text...
my mistake, the controls are not showing up because in the constructor which get the From1 parameter not calling InitializeComponent() which initialize the form controls. so you have two options:
1. chin the constructors like so:
Code:
public Form2(Form1 MainForm) : this()
{
this._MainForm = MainForm;
}
note the "this()" it make sure that the default constructor will be called, or you can simply add the metohd into both constructors:
Code:
public Form2(Form1 MainForm)
{
InitializeComponent();
this._MainForm = MainForm;
}
Re: Going nuts about changing a label text...
I went with number one and it works flawlessly now, thank you!
Re: [RESOLVED] Going nuts about changing a label text...
No problem, glad I could help.