2

I am running server/client communication. Now i want to write something on the server textBox and show it on the client textBox so I am sending a message from the server to the client which takes it using a static method:

    static void Client_MessageReceived(object sender, MessageEventArgs e)
    {
        //Client only accepts text messages
        var message = e.Message as ScsTextMessage;
        if (message == null)
        {
            return;
        }
    }

Now I just want to append the message to the client textBox from this static method. I guess that I will need some kind of invoke but I don't know how to do it, and I cant find anything useful on Google.

5
  • 2
    Well which text box do you want to change? You're in a static method, so there's no context. Commented Jan 18, 2013 at 22:43
  • Ok, i have Client application and it has it's own tbox TextBox which is on Form called Client. So i want to change tbox text. Commented Jan 18, 2013 at 22:46
  • But logically, from the point of view of the method, there could be multiple forms - or none! Why is this a static method? (Is this on the form? There's a lot you haven't told us.) Commented Jan 18, 2013 at 22:49
  • Oh, i am sorry it don't have to be static, posting answer. Commented Jan 18, 2013 at 23:09
  • Yes as Martin pointed out, this will only work if your Client class has a static TextBox field (meaning there can only be one). Then you can update it from your static method. Commented Jan 18, 2013 at 23:10

2 Answers 2

1

Invoke method

public void AppendText(string what, bool debug = false)
     {
         if (debug)
             return;
         if (this.InvokeRequired)
         {
             this.Invoke(
                 new MethodInvoker(
                 delegate() { AppendText(what); }));
         }
         else
         {
             DateTime timestamp = DateTime.Now;
             tbox.AppendText(timestamp.ToLongTimeString() + "\t" + what + Environment.NewLine);
         }
     }

Message received method

 private void Client_MessageReceived(object sender, MessageEventArgs e)
        {
            //Client only accepts text messages
            var message = e.Message as ScsTextMessage;
            if (message == null)
            {
                return;
            }
            AppendText(message.Text, false);

            //Console.WriteLine("Server sent a message: " + message.Text);
        }
Sign up to request clarification or add additional context in comments.

Comments

0
// Program.cs

public static ProgramForm Form;

publi static void Main()
{
    // ...
    Application.Run(Form = new ProgramForm());
    // ...
}

public static void ChangeText(String message)
{
    Form.TextBox1.Text = message;
}

// ProgramForm.cs

private void Client_MessageReceived(object sender, MessageEventArgs e)
{
    if (e.Message != null)
        Program.ChangeText(e.Message);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.