RichTextBox不显示字符串 [英] RichTextBox Not Showing String

查看:135
本文介绍了RichTextBox不显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TCP服务器和客户端设置,但是当我从服务器向客户端发送数据并将其转换为字符串时,它不会让我在richTextBox中显示该字符串.它使我可以在消息框中显示它,但不能在文本框或Richtextbox中显示.

这是我在客户端的接收代码.

I have a TCP server and client setup but when i send data from the server to the client and convert it to a string it will not let me display the string in a richTextBox. It lets me show it in a message box but not a textbox or richtextbox.

Here is my receiving code on the client side.

public void OnReceive(IAsyncResult ar)
        {
            String content = String.Empty;
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;
            int bytesRead;
            if (handler.Connected)
            {
                try
                {
                    bytesRead = handler.EndReceive(ar);
                    if (bytesRead > 0)
                    {
                        state.sb.Remove(0, state.sb.Length);
                        state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
                        content = state.sb.ToString();
                        richTextBox1.Text = content;
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                            new AsyncCallback(OnReceive), state);
                    }
                }



谢谢回复.我已阅读了这些链接并尝试了此操作



Thanks for the reply. I have read over those links and tried this

<pre lang="cs">public delegate void SetTextCallback(string s);

if (rich.InvokeRequired)
           {
               SetTextCallback d = new SetTextCallback(SetText);
               Invoke(d, new object[] { text });
           }
           else
           {
               rich.SelectionColor = Color.Blue;
               rich.SelectedText = "\n: " + text;
           }



但这似乎也不起作用.我做错什么了吗?

谢谢



But it does not seem to work either. Is there something that I am doing wrong?

Thank you

推荐答案



异步事件在其他线程上处理,请参见此处 [此处 [
Hi,

asynchronous events get handled on a different thread, see here[^].

That makes it impossible to manipulate Winform Controls as you would usually do. The solution is described here[^].

:)


这篇关于RichTextBox不显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆