1

I have windows application in C# and have a richtextbox in the GUI. I want to reach the richtextbox and append some text to it from inside a class. I thought about creating a static RichTextBox item in the form's code file and then call it like this:

public partial class Form1 : Form
{
    public static RichTextBox box;

    public Form1()
    {
        box = richtextbox_in_gui;
    }
}

public class SomeClass()
{
    Form1.box.AppendText("...");
}

Is that a safe way to do it? Or is there any other way i can do this?

Thanks

yrazlik
  • 10,411
  • 33
  • 99
  • 165
  • What did you try yourself? The code you posted doesn't even compile. – Femaref Nov 17 '13 at 18:39
  • @Femaref it compiles, i just did not write the whole code here. What is wrong with the code? – yrazlik Nov 17 '13 at 18:40
  • You can't have a field definition in the constructor. – Femaref Nov 17 '13 at 18:41
  • @Femaref that's right edited the question – yrazlik Nov 17 '13 at 18:42
  • Better to have your classes worry about data and let the UI code worry about display. Muddling the two will lead to an unmaintainable mess. – Ed S. Nov 17 '13 at 18:48
  • @EdS. that is right, but here is the scenario in my program: I am doing a client server application using sockets and have 2 separate projects for client and server. When client clicks a button on its own gui, i want to append some text to server's gui showing that client has clicked the button. So even though i do not use the gui of the server, i have to make some changes on it – yrazlik Nov 17 '13 at 18:56
  • That doesn't mean you need to pass around references to UI controls – Ed S. Nov 17 '13 at 19:04

1 Answers1

4

You don't want a static field, as that would be for all instances of Form1; it should be a normal field.

However, public fields are not a good idea, as the class can be manipulated from the outside. You'll want to encapsulate the behaviour, so the best idea is a method:

public partial class Form1 : Form
{
    public Form1()
    {
        box = richtextbox_in_gui;
    }

    public void AppendText(string text)
    {
      this.textbox.AppendText(text);
    }
}

public class SomeClass
{
  private Form1 form;

  public SomeClass(Form1 form)
  {
    this.form = form;
  }

  public void AppendHelloWorld()
  {
    this.form.AppendText("Hello World");
  }
}

Now, if you have a reference to the form object, you can use the AppendText method:

Form1 form = new Form1();
var some = new SomeClass(form);
some.AppendHelloWorld();

This style of coding is called Dependency Injection.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • Well, i have some issues now :) It sometimes works fine, but sometimes i get an error saying that "Cross-thread operation not valid: Control ' accessed from a thread other than the thread it was created on." What can be the reason for that? I googled it but did not understand what is going on and why is not happening? – yrazlik Nov 19 '13 at 18:11
  • 1
    You are accessing the UI from a thread that is not the main thread (where the form was instantiated) http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c this question will help you. – Femaref Nov 19 '13 at 22:19