Update TextBox from another class not working

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1854 Views - Last Post: 15 August 2012 - 07:11 PM Rate Topic: -----

#1 actus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Update TextBox from another class not working

Posted 13 August 2012 - 01:57 PM

I am trying to update a textbox from a class in the same namespace. The simple sample below firesoff a message box with that proves that class b is sending the information to class a. However the textbox is not working.
CLASS A -> it is a form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Class1 x = new Class1();
            x.aupdate();
            AppendTextBoxLine("Class A update");


        }
        public void AppendTextBoxLine(string myStr)
        {
            
            if (textBox1.Text.Length > 0)
            {
                textBox1.AppendText(Environment.NewLine);
            }
            textBox1.AppendText(myStr);
            MessageBox.Show(myStr);
        }
    }
}




This is class b
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;



namespace WindowsFormsApplication3
{
    class Class1
    {
        public void  aupdate()
        {
            Form1 y = new Form1();
            y.AppendTextBoxLine("Class B Update");
           

        }
    }
}




Thanks in Advance

Is This A Good Question/Topic? 0
  • +

Replies To: Update TextBox from another class not working

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:04 PM

What do you mean "the textbox is not working"?

The code you have above is doing exactly what was written to do.
        public void  aupdate()
        {
            Form1 y = new Form1();
            y.AppendTextBoxLine("Class B Update");
           

        }



Line 3 creates a new instance of a form.
Line 4 calls AppendTextBoxLine() on that instance of the form, and as you've observed it pops up the message box to confirm that it got that for.

If you were expecting line 3 to magically find the running instance of the form, you should review what you know about C#.
Was This Post Helpful? 1
  • +
  • -

#3 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:08 PM

Firstly, it makes a lot of sense to name your classes descriptively, so you and anyone else using them knows what they're for. Class1 is not a good name for a class.

Second, do you know the difference between classes and objects? Your code has a class Form1, inherited from Form. In that, when you click a button, you're creating a new instance (object) of Class1 named x (also a bad name - what is x for?) x has a function name aupdate, which you call from the Form1 class - the function creates a new instance of the Form1 class, to which you're trying to append a text box. Unfortunately, it's a different instance from the one you're expecting it to append.

Instead of void, why not have Class1.aupdate() return a string which you append to the textbox in the original instance of Form1?
Was This Post Helpful? 0
  • +
  • -

#4 actus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:13 PM

Thanks Skydiver for the quick response. I am new to c# and am trying to understand some of the nuances. The issue is:

The textbox does not update with the String from class b however it displays the string in the MessageBox that is called from the form.

So I am trying to understand how the MessageBox is able to display the value of the sting from class b however the textbox is not updated.
Was This Post Helpful? 0
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 486
  • Joined: 13-June 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:16 PM

Again, you're looking at the textbox in a different instance of Form1. One line 15 of your second section of code, you create a new instance of Form1, then on 16 you try to append it. You haven't used any code to show this From1, but it exists. You may have appended the text in that, but not the Form1 that's shown when your program runs.

Are you following a guided learning course or reverse-engineering existing code?
Was This Post Helpful? 0
  • +
  • -

#6 actus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:21 PM

View PostMrShoes, on 13 August 2012 - 02:08 PM, said:

Firstly, it makes a lot of sense to name your classes descriptively, so you and anyone else using them knows what they're for. Class1 is not a good name for a class.

Second, do you know the difference between classes and objects? Your code has a class Form1, inherited from Form. In that, when you click a button, you're creating a new instance (object) of Class1 named x (also a bad name - what is x for?) x has a function name aupdate, which you call from the Form1 class - the function creates a new instance of the Form1 class, to which you're trying to append a text box. Unfortunately, it's a different instance from the one you're expecting it to append.

Instead of void, why not have Class1.aupdate() return a string which you append to the textbox in the original instance of Form1?


Yes I know the difference between the class and object. I did not write the sample for elegance just to ask a question. Ok now you said something that I need to focus on. How do I find the current instance of Form1?

Thanks for the help.

View PostMrShoes, on 13 August 2012 - 02:16 PM, said:

Again, you're looking at the textbox in a different instance of Form1. One line 15 of your second section of code, you create a new instance of Form1, then on 16 you try to append it. You haven't used any code to show this From1, but it exists. You may have appended the text in that, but not the Form1 that's shown when your program runs.

Are you following a guided learning course or reverse-engineering existing code?


I am a python programmer and a php programmer who needs to learn c# for an application that needs to be written. I am trying to bridge the gap from what I know and what I need to learn. I appreciate the assistance.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:27 PM

You could iterate over the Application.OpenForms to find the form you are interested in, but that is just plain old ugly.

If you really need to find the current instance of a form, and there can only ever be one instance of the form, the applying theSingleton design pattern is more appropriate and clues in the reader that there should only ever be one instance of the form.

Singleton design pattern and the Visual Studio Forms Designer don't mix well though. You'll have to cheat and let the Designer create as many instances of the form as it wants, but you'll need to be disciplined and only use your form through the singleton and never through new Form().

This post has been edited by Skydiver: 13 August 2012 - 02:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 actus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:32 PM

View PostSkydiver, on 13 August 2012 - 02:27 PM, said:

You could iterate over the Application.OpenForms to find the form you are interested in, but that is just plain old ugly.

If you really need to find the current instance of a form, and there can only ever be one instance of the form, the applying theSingleton design pattern is more appropriate and clues in the reader that there should only ever be one instance of the form.

Singleton design pattern and the Visual Studio Forms Designer don't mix well though. You'll have to cheat and let the Designer create as many instances of the form as it wants, but you'll need to be disciplined and only use your form through the singleton and never through new Form().



Thanks.... The MS world is interesting... I have to say that I am impressed with C# and the VS IDE.
Was This Post Helpful? 0
  • +
  • -

#9 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Update TextBox from another class not working

Posted 13 August 2012 - 02:44 PM

You can always make a parameterized constructor for Class1 into which you pass a reference to your current (GUI) instance. Not sure if it's an 'acceptable' practice among OO purists, but here's an example:

Form1:
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 x = new Class1(this); // Pass in the running instance
            x.aupdate();
            AppendTextBoxLine("Class A update");

        }


Class1:
namespace WindowsFormsApplication3
{
    class Class1
    {
        Form1 y;
        public Class1(Form1 form) {
            y = form;
        }
        
        public void  aupdate()
        {
            y.AppendTextBoxLine("Class B Update");
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#10 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3168
  • View blog
  • Posts: 9,578
  • Joined: 05-May 12

Re: Update TextBox from another class not working

Posted 13 August 2012 - 03:00 PM

+1. To AVReidy's idea. That is assuming you drink the dependency injection koolaid. Personally I like that flavor of koolaid. :lol:

This post has been edited by Skydiver: 13 August 2012 - 03:30 PM

Was This Post Helpful? 0
  • +
  • -

#11 actus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Re: Update TextBox from another class not working

Posted 14 August 2012 - 06:10 AM

View PostAVReidy, on 13 August 2012 - 02:44 PM, said:

You can always make a parameterized constructor for Class1 into which you pass a reference to your current (GUI) instance. Not sure if it's an 'acceptable' practice among OO purists, but here's an example:

Form1:
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 x = new Class1(this); // Pass in the running instance
            x.aupdate();
            AppendTextBoxLine("Class A update");

        }




This was Money.... This actually made sense and I love the koolaid like Skydiver said...


THANK!
Was This Post Helpful? 0
  • +
  • -

#12 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Update TextBox from another class not working

Posted 15 August 2012 - 07:08 AM

No, tightly binding the forms together by passing references back and forth in constructs is not considered an 'acceptable' way by most companies. It makes the connection very fragile. This is why we have events people.

Form A to Form B communication is the like the #3 most frequently asked question and we have tutorials for it.


See FAQ # 3. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#13 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4311
  • View blog
  • Posts: 7,467
  • Joined: 08-June 10

Re: Update TextBox from another class not working

Posted 15 August 2012 - 08:20 AM

My cross-form communication tutorial:

http://www.dreaminco...ny-other-forms/
Was This Post Helpful? 0
  • +
  • -

#14 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,357
  • Joined: 02-June 10

Re: Update TextBox from another class not working

Posted 15 August 2012 - 08:26 AM

Just FYI Curtis:
Yours is the first tutorial I point people to with that FAQ item:

Attached Image
Was This Post Helpful? 0
  • +
  • -

#15 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

Re: Update TextBox from another class not working

Posted 15 August 2012 - 05:15 PM

View Postactus, on 14 August 2012 - 08:10 AM, said:

View PostAVReidy, on 13 August 2012 - 02:44 PM, said:

You can always make a parameterized constructor for Class1 into which you pass a reference to your current (GUI) instance. Not sure if it's an 'acceptable' practice among OO purists, but here's an example:

Form1:
        private void button1_Click(object sender, EventArgs e)
        {
            Class1 x = new Class1(this); // Pass in the running instance
            x.aupdate();
            AppendTextBoxLine("Class A update");

        }




This was Money.... This actually made sense and I love the koolaid like Skydiver said...


THANK!

Glad I could help!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2