0

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.

  • 1
    You need an instance of the form if you don't want to use a static method. Ex: `Form1 myform;` `Form1` would be the type, and `myform` would be the instance. – mason Nov 07 '14 at 21:05

4 Answers4

1

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");
James Shaw
  • 839
  • 1
  • 6
  • 16
0

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.

  • I think I may have a threading problem here now you say it. Using that code you provided the program now runs fine, but the text doesn't actually appear in the text box. I quickly tried calling the function on the from the same form as the text box was on and it worked. Thanks for your help so far, if you could point me in the right direction some more that would be great. –  Nov 07 '14 at 21:36
  • So something like public void updateTextBox(String value) { outputTextbox.Invoke((MethodInvoker)delegate { outputTextbox.Text = value; }); } – Jan Petter Jetmundsen Nov 07 '14 at 21:49
0

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);
Abdurrahman Alp Köken
  • 1,236
  • 1
  • 13
  • 12
0

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

ajd.nas
  • 354
  • 2
  • 12