Results 1 to 9 of 9

Thread: access label in other form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    83

    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.

  2. #2
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    83

    Re: access label in other form

    if i put frmMain.currentBalance.Text = "hello"; it comes up with an error.

  4. #4
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    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

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    83

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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();
    		}

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2005
    Posts
    83

    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
  •  



Click Here to Expand Forum to Full Width