DDuffy
DDuffy

Reputation: 413

updating a textbox from another form. C#

I have looked at various answers, i have googled my nut off for a good few hours now, and still cant seem to get this to work. I am trying to update a textbox on a form. i have simplified the code i am using in the hope that it was just something i was adding unnecessarily, but still cant get it to work. I know the text is being passed to the textbox and stored in the box, but it will not display in the actual box.

In form one (Form_DMM);

private void BtnTest_Click(object sender, EventArgs e)
{
    ErrorHandling EH = new ErrorHandling();
    EH.updatetbtest();
}

in separate class;

    public void updatetbtest()
    {
        string FailedMessagePB = "Test Message" + "\n";
        Form_DMM FormDMM = new Form_DMM();
        FormDMM.TextBoxAppend(FailedMessagePB);
        FormDMM.TextBoxAppend2 = FailedMessagePB;
        FormDMM = null;
    }

passed back to form one;

public void TextBoxAppend(string WriteMessage)
{
    TB_Issues.AppendText(WriteMessage + "\n");
    System.Windows.Forms.Application.DoEvents();
    TB_Issues.Invalidate();
    TB_Issues.Update();
    TB_Issues.Refresh();
    MessageBox.Show(TB_Issues.Text);
}

    public string TextBoxAppend2
    {
        get
        {
            return TB_Issues.Text;
        }
        set
        {
            TB_Issues.Text = TB_Issues.Text + value + "\n";
            System.Windows.Forms.Application.DoEvents();
            TB_Issues.Invalidate();
            TB_Issues.Update();
            TB_Issues.Refresh();

            MessageBox.Show(TB_Issues.Text);
        }

    }

As you can see i have two separate attempts at updating the textbox, neither of which will display the test message in the textbox, but the messagebox that pops up will show the test message. it will even show the double test message from the TB_Issues.AppendText().

Can someone please help and tell me where i'm going wrong. This is driving me insane!

Upvotes: 1

Views: 875

Answers (3)

Leonid Malyshev
Leonid Malyshev

Reputation: 475

You try to create new form. But you must use existing. You can pass existing form calling EH.updatetbtest(this); And of course adding parameter to declaration like updatetbtest(Form_DMM FormDMM). And delete declaration and new in the function body.

Upvotes: 1

Michael Seidel
Michael Seidel

Reputation: 81

From the problem posted the error is in the ErrorHandling class. In the method updatetbtest you create a new Insance of the form object. So you create a separate form object, change the text and then you lose any reference because you set the variable to null. The message box is displayed because the TextBoxAppend method is called and the messagebox is a separate instance. The new instance you create is never displayed.

You have to hand over your calling form instance to the updatetbtest method.

Something like this:

private void BtnTest_Click(object sender, EventArgs e)
{
    ErrorHandling EH = new ErrorHandling();
    EH.updatetbtest(this);
}


public void updatetbtest(Form_DMM form)
{
    string FailedMessagePB = "Test Message" + "\n";
    form.TextBoxAppend(FailedMessagePB);
}

Upvotes: 1

Faraz Ahmed
Faraz Ahmed

Reputation: 1607

you can use like this

public void updatetbtest(Form_DMM FormDMM)
    {
        string FailedMessagePB = "Test Message" + "\n";
        FormDMM.TextBoxAppend(FailedMessagePB);
        FormDMM.TextBoxAppend2 = FailedMessagePB;
    }

and in your buttontest_Click

private void BtnTest_Click(object sender, EventArgs e)
    {
        ErrorHandling EH = new ErrorHandling();
        EH.updatetbtest(this);
    }

Upvotes: 2

Related Questions