|
-
Feb 6th, 2011, 01:04 PM
#1
Thread Starter
Hyperactive Member
-
Feb 6th, 2011, 01:12 PM
#2
Re: Going nuts about changing a label text...
Can you show us the code ?
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Feb 6th, 2011, 01:26 PM
#3
Thread Starter
Hyperactive Member
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.
-
Feb 6th, 2011, 01:30 PM
#4
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.
Last edited by motil; Feb 6th, 2011 at 01:39 PM.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Feb 6th, 2011, 01:44 PM
#5
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");
}
}
}
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Feb 6th, 2011, 01:54 PM
#6
Thread Starter
Hyperactive Member
Re: Going nuts about changing a label text...
thank you I will take a look and try to learn!
-
Feb 6th, 2011, 02:18 PM
#7
Thread Starter
Hyperactive Member
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
-
Feb 6th, 2011, 02:29 PM
#8
Re: Going nuts about changing a label text...
the new is creating new instance of Form2, the "this" is the instance of Form1.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Feb 6th, 2011, 02:59 PM
#9
Thread Starter
Hyperactive Member
Re: Going nuts about changing a label text...
Okay now I see, but why won't the controls show up then?
-
Feb 6th, 2011, 03:14 PM
#10
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;
}
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
-
Feb 6th, 2011, 03:57 PM
#11
Thread Starter
Hyperactive Member
Re: Going nuts about changing a label text...
I went with number one and it works flawlessly now, thank you!
-
Feb 6th, 2011, 04:14 PM
#12
Re: [RESOLVED] Going nuts about changing a label text...
No problem, glad I could help.
* Rate It  If you Like it
__________________________________________________________________________________________
" Programming is like sex: one mistake and you’re providing support for a lifetime."
Get last SQL insert ID 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|