0

I am developing a C# win forms project ,I have one main GUI (name : Form1) and a class (name :AnotherClass) , i am just updating the GUI text box from AnotherClass ,application gets complied and runs successfully but still text Box on GUI not updated. Here is my code :

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

    private void button1_Click(object sender, EventArgs e)
    {
        AnotherClass obj = new AnotherClass();
        obj.updategui();
    }
    public void AppendText(String text)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new Action<string>(AppendText), new object[] { text });
            return;
        }
        this.textBox1.Text += text; 
    }

AnotherClass

class AnotherClass
{
    public void updategui()
    {
        Form1 mainform = new Form1();
        mainform.AppendText("Say Hi");

    }

}

Any One Having idea of why the GUI not updated ? please comment with your working code. Regards.

Albert
  • 11
  • 3
  • Creating a new main form without showing it looks strange to me. – Uwe Keim Jun 17 '15 at 06:41
  • 2
    _(How did this question manage to receive two upvotes?)_ – Uwe Keim Jun 17 '15 at 06:42
  • 1
    You should pass your parent form to 'AnotherClass' (either in the constructor/property/method parameter). Otherwise you simple create a new instance of Form1 – Nissim Jun 17 '15 at 06:44
  • I created new instance of form1 in another class named mainform ... – Albert Jun 17 '15 at 06:48
  • @UweKeim i think question is unclear to you... GUI form is initliazed as application gets started, whats looks strange to you ...? – Albert Jun 17 '15 at 06:49
  • @Peter Duniho This question is not about communication between forms , why not clearly read question and then add comment ? why u mark it as duplicate ? – Albert Jun 17 '15 at 07:10
  • It absolutely is about communication between classes, which is really the same thing as between two forms. The various solutions found in the duplicate question will be helpful to you in implementing the relationship between your classes in a way that _works_. Right now, you have two classes, where one (`AnotherClass`) is trying to contact the other (`Form1`), but fails because it's creating a _new_ instance of the other instead of using an existing reference to the instance you want to use. – Peter Duniho Jun 17 '15 at 07:15
  • And if you don't want to bother reading through all the helpful advice on that duplicate question, there are dozens of similar duplicates on Stack Overflow addressing the same basic issue. Please do a search on the existing questions before posting your question. – Peter Duniho Jun 17 '15 at 07:17
  • @PeterDuniho i am implementing control invoke, or threading concepts if you don't know the solution don't(as u just giving irrelevant links ,how can u mark a question of invoke , threads same as Communicate between two windows forms in C# strange understanding by you) don't deviate other experts for giving helping tips . – Albert Jun 17 '15 at 08:02
  • Your question has **nothing** at all to do with the use of `Control.Invoke()`, and **everything** to do with the fact that `AnotherClass` does not have a reference to the correct instance of `Form1`. Which is _exactly_ what the whole question of classes _or_ forms "communicating with each other" addresses. If you'd spend more time reading the answers in the duplicate question, and/or reading the answers in other similar questions, rather than continuing to insist that even though you don't know the answer to your question you know it's not a duplicate of any other questions, you'd do better. – Peter Duniho Jun 17 '15 at 08:08
  • Here are just a handful of other similar questions with answers, which I found in just a few minutes of searching: http://stackoverflow.com/a/29586717, http://stackoverflow.com/q/12428829, http://stackoverflow.com/a/15766860, and http://stackoverflow.com/q/24398814 – Peter Duniho Jun 17 '15 at 08:18
  • Is there any expert to help me get this problem solved ? – Albert Jun 17 '15 at 09:49

0 Answers0