白色字符C#文本框

时间:2016-11-29 14:18:27

标签: c# sockets


我是C#的新手,我在C#的文本框中遇到方形白色字符问题。 (见下面的截图)
我已经实现了System.Net.Sockets.支持的聊天客户端服务器程序。每个程序内部都是byte[] array= new byte[10025],它永远不会完全填满,但文本框里面会显示空白字符。你可以帮我删除这个角色吗?
谢谢!

Blank white character squares

阅读客户端代码:
http://pastebin.com/W3Pc3BPG

        try
        {
            while (true)
            {
                serverStream = clientSocket.GetStream();
                int buffSize = 0;
                byte[] inStream = new byte[10025];
                buffSize = clientSocket.ReceiveBufferSize;
                serverStream.Read(inStream, 0, inStream.Length);
                string returndata = System.Text.Encoding.UTF8.GetString(inStream);
                readData = "" + returndata;
                msg();
            }
        } catch(Exception e)
        {
            ctThread.Join();
        }

2 个答案:

答案 0 :(得分:2)

serverStream = clientSocket.GetStream();
int buffSize = 0;
byte[] inStream = new byte[10025];
buffSize = clientSocket.ReceiveBufferSize;
// Make sure you respect the number of bytes that have been read.
// Note that the below code is not the most performant - it's just
// to demonstrate the technique. 
int bytesRead = serverStream.Read(inStream, 0, inStream.Length);
byte[] theData = inStream.Take(bytesRead).ToArray();
string returndata = System.Text.Encoding.UTF8.GetString(theData);
readData = "" + returndata;
msg();

答案 1 :(得分:0)

您可以在文本框上使用以下代码来检查是否为空格。

bool hasAllWhitespace = txtBox1.Text.Length>0 &&
                        txtBox1.Text.Trim().Length==0;

消除空格

if (hasAllwhitespace)
{
    txtBox1.Text.Trim();
}