user1738676
user1738676

Reputation:

Update textbox from a different class

I'm relatively new to C# and come a bit stuck.

I've got a Rich Textbox on a form, and I would like to update this from a different class to the Form itself.

I first tried

Form1.outputTextbox.AppendText(string);  

but the text box was not accessible, made sense. So instead I tried to make a function. On Form1 I created the function

public void updateTextBox(string new_text)
    {
        outputTextBox.AppendText(new_text);
    }

and in the class I used.

Form1.updateTextBox("apple");

The problem I'm having is the only way my class can see the function is if I make it the function static, but when I do that get an error "An object reference is required for the nonstatic field, method, or property 'member'"

Am I close or going to wrong way about this completely? Any help would be appricated.

Upvotes: 0

Views: 3237

Answers (4)

ajd.nas
ajd.nas

Reputation: 404

I know it is very late, but maybe someone need the solution... you can access to all controller from another form without create object by pass it as parameter to constructor...

for Example

    public partial class Form2 : Form
    {
        MainForm mainForm;

        public Form2(MainForm mainForm)
        {
            InitializeComponent();
           this.mainForm = mainForm;
            txtRecive00.TextChanged += new EventHandler(txtRecive8changed);
        }

        void txtRecive8changed(object sender, EventArgs e)
        {
            mainForm.txtRecive1.Text += txtRecive00.Text;
        }

in my case I can update the text in mainForm.txtRecive1.Text from Form2...

and in MineForm we creat object from Form2 like that:

Form2 f2 = new FormMeasure(this);

for more Info show this short video https://www.youtube.com/watch?v=CdH8z_JNi_U

Upvotes: 0

James Shaw
James Shaw

Reputation: 839

Alternatively, you can do something like the following. This takes advantage of custom arguments and events.

namespace WindowsFormsApplication3
{
    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;

    public partial class Form1 : Form
    {
        TextBox textBox;
        SomeClass someClass;

        public Form1()
        {
            InitializeComponent();
            Initialize();
            BindComponents();
        }

        private void BindComponents()
        {
            //EventHandlers
            this.Load += new EventHandler(Form1_Load);
            this.someClass.TextUpdatedEvent += new EventHandler(someClass_TextUpdatedEvent);
        }

        void someClass_TextUpdatedEvent(object sender, EventArgs e)
        {
            this.textBox.Text = (e as FormArgs).Text;
        }

        private void Initialize()
        {
            this.textBox = new TextBox();
            this.someClass = new SomeClass();
        }

        void Form1_Load(object sender, EventArgs e)
        {
            this.Controls.Add(textBox);
        }
    }

    public class SomeClass
    {
        public event EventHandler TextUpdatedEvent = delegate { };

        public void UpdateText(string text)
        {
            if (TextUpdatedEvent != null)
            {
                TextUpdatedEvent(this, new FormArgs() { Text = text });
            }
        }
    }

    public class FormArgs : EventArgs
    {
        public string Text { get; set; }
    }
}

If you do it this way, you can update the form text like this:

someClass.UpdateText("changing the text on the form");

Upvotes: 1

Pass Form1 instance to the other class via constructor or property. Then you can access outputTextbox from the other class.

public class OtherClass
{
    private Form1 form1;
    public OtherClass(Form1 form1)
    {
        this.form1 = form1;
    }

    private void ChangeText()
    {
        form1.outputTextBox.AppendText("hello world");
    }
}

Instantiate OtherClass from Form1.cs and pass its instance. In Form1.cs:

OtherClass obj = new OtherClass(this);

Upvotes: 0

Jan Petter Jetmundsen
Jan Petter Jetmundsen

Reputation: 585

You are trying to access the function from the class instead of the object. Use

Form1 myForm = new Form1();
...
myForm.updateTextBox("whatever");

Also be aware of thread issues here. What triggers the outside code? Is it another ui action, then all is well. Does it come from another thread, then you´ll have to handle this.

Upvotes: 0

Related Questions