简体   繁体   English

TextBox和.AppendText()中带有行的奇怪行为-C#

[英]Odd Behaviour with Lines in TextBox and .AppendText() - C#

I'm currently following the great tutorial on Geekpedia to create my own Chat Client and Chat Server . 我目前正在遵循有关Geekpedia的精彩教程来创建自己的聊天客户端聊天服务器

Connecting the clients to the server works fine but I'm experiencing a few issues with the text being displayed in the chat log. 将客户端连接到服务器可以正常工作,但是在聊天日志中显示文本时遇到了一些问题。

The first message that is displayed is in the Server window then the incoming connections are monitored: 显示的第一条消息在“服务器”窗口中,然后监视传入的连接:

图片1

Then I go to connect the Client to the host, all is fine and the appropriate log messages are displayed: 然后,我将客户端连接到主机,一切正常,并显示相应的日志消息:

图片2

Now when I go to send a simple message the Line.Length property shows 3, in particular notice the text: 现在,当我发送一条简单消息时,Line.Length属性显示3,尤其要注意以下文本:

图片3

Also, the .Text property contains everything, not just the current line: 此外,.Text属性包含所有内容,而不仅仅是当前行:

替代文字

Now looking at the Server and Client window you'll notice the text has some how copied itself in the wrong places and the previous message is overwritten: 现在,在“服务器和客户端”窗口中,您会发现文本有一些如何在错误的地方复制自身,并且先前的消息被覆盖:

图片4

If I press the Send button again (With the same string/message) I notice this in the debugger: 如果再次按下发送按钮(使用相同的字符串/消息),我会在调试器中注意到这一点:

图片5

The text in the Server window does not move further down the box, it seems to overwrite what was previously there. “服务器”窗口中的文本不会在框内进一步移动,似乎会覆盖以前的内容。 Also the text in the Client window has gone: 客户端窗口中的文本也消失了:

图片6

So if I press the send text button again we see this for the Line property: 因此,如果再次按下发送文本按钮,我们将在Line属性中看到以下内容:

图片7

I'm at a complete loss, I spent a good few hours debugging the code and even after using a diff tool to compare the original code to mine it's all the same (With exception to certain words in different cases). 我完全不知所措,我花了好几个小时来调试代码,甚至在使用diff工具比较原始代码以挖掘出全部相同代码之后(不同情况下的某些单词除外)。

If I run the original source code and inspect the same Length property I notice that the count is 1 and the .Text property is only one line: 如果运行原始源代码并检查相同的Length属性,我会注意到计数为1,而.Text属性仅为一行:

图片8

Everything displays correctly: 一切正确显示:

图片9

Does anybody have any idea what's going on? 有人知道发生了什么吗? Now I know an option is to simply use the source code itself but I want to "learn by doing" and would like to know why this is happening and how to fix it. 现在,我知道一种选择是仅使用源代码本身,但我想“边做边学”,并想知道为什么会这样以及如何解决它。

Thanks for your time! 谢谢你的时间!

I am just taking a shot in the dark here, but the SendMessage method in your modified code uses the text from the chatWindow control. 我只是在黑暗中拍照,但是修改后的代码中的SendMessage方法使用chatWindow控件中的文本。 The SendMessage in the original code uses the text from txtMessage. 原始代码中的SendMessage使用txtMessage中的文本。 I think you are using the text from the wrong control. 我认为您使用的文字来自错误的控件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM