|
-
Jan 6th, 2006, 02:50 PM
#1
Thread Starter
Lively Member
access label in other form
I have two forms and one 1 form there is a button, which does a lot of stuff but there is one problem. I use the below code to access the other from (frmMain)'s label called currentBalance. But at runtime frmMain's label does not change.
Code:
frmMain MainForm = new frmMain();
MainForm.currentBalance.Text = "hello";
Why doesn't the label change it's text? In vb this works.
-
Jan 6th, 2006, 03:51 PM
#2
Frenzied Member
Re: access label in other form
You may want to check the vb (.net?) code that works. What you're doing there is creating a new form and changing something in the new form. You're not showing the new form, so you don't see anything happen.
Instead of creating a new form grab the instance of the one you want to change.
Mike
-
Jan 6th, 2006, 06:21 PM
#3
Thread Starter
Lively Member
Re: access label in other form
if i put frmMain.currentBalance.Text = "hello"; it comes up with an error.
-
Jan 6th, 2006, 06:35 PM
#4
Frenzied Member
Re: access label in other form
I just looked at your code again and realized I was wrong about you creating a new instance and modifying from there.
Can you post how you're defining currentBalance in your FormMain? Also post the exact error you're getting (the stack trace).
Mike
-
Jan 7th, 2006, 09:47 AM
#5
Re: access label in other form
Create a static class from which you instantiate both forms. You can then view form2 from form1's button click event.
-
Jan 7th, 2006, 06:42 PM
#6
Thread Starter
Lively Member
Re: access label in other form
sorry mendhak but i don't know what you mean.
If i put
frmMain.currentBalance.Text = "hello";
the error that comes up is: Error 1 An object reference is required for the nonstatic field, method, or property 'KACash.frmMain.currentBalance' E:\My Documents\Visual Studio 2005\Projects\KACash\KACash\frmLogin.cs 48 33 KACash
-
Jan 7th, 2006, 07:42 PM
#7
Re: access label in other form
There's no way that code is working in VB.NET so I assume that you mean VB6. All this comes down to is straight OOP. The code you posted is creating an object and manipulating THAT object. If you already have a frmMain object then it will remain unaffected. Let's say you have a notebook that you want to write on. If you go to a shop and by a new notebook and write on that, would you be surprised that what you wrote didn't appear on the first notebook? No you wouldn't, because they are two ditinct objects and doing something to one has no affect on the other, which is exactly the case in your code. You need a reference to the EXISTING frmMain object to be able to affect it. How is this second form created? Is it created and displayed from the existing frmMain? If so, the easiest way to do what you want is to pass a reference to the existing frmMain as an argument to the constructor of the second form. You can then save this reference to a class-level variable which can then be used whenever you like from within the second form.
-
Jan 8th, 2006, 09:22 AM
#8
Re: access label in other form
I have an old example of this in C#, this one uses delegation.
In Form1, start by declaring an instance of Form2
Code:
private Form2 f2 = new Form2();
In InitializeComponent, add this handler
Code:
f2.OnMessage += new Form2.Message(this.txtMessage);
Also,
Code:
private void button1_Click(object sender, System.EventArgs e)
{
f2.writeMessage("A message from Form 1");
f2.Show();
}
private void txtMessage(string str)
{
this.textBox1.Text = str;
}
In Form2, create the delegate and the event
Code:
public delegate void Message(string str);
public event Message OnMessage;
And add these:
Code:
public void writeMessage(string str)
{
this.textBox1.Text = str;
}
private void button1_Click(object sender, System.EventArgs e)
{
this.OnMessage("A message from Form2");
}
Also, in InitializeComponent for Form2, you want to make sure that when it is closed, it isn't really closed, but 'hidden' so that the reference still exists.
Code:
this.Closing += new CancelEventHandler(this.Form2_Closing);
So you can add Form2_Closing
Code:
private void Form2_Closing(object sender, CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
-
Jan 9th, 2006, 02:58 PM
#9
Thread Starter
Lively Member
Re: access label in other form
On don't think it needs to be as complecated as that. I have a form, that when a button is clicked it opens another form on top of it. The form has a button that when clicked closes the second form and changes the first forms text. I am possitive that in vb.net all i need to do was put frmMain.currentBalance.Text = "Test";
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
|