开发者

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.

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:

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

Then I go to connect the Client to the host, all is fine and the appropriate log messages are displayed:

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

Now when I go to send a simple message the Line.Length property shows 3, in开发者_高级运维 particular notice the text:

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

Also, the .Text property contains everything, not just the current line:

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

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:

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

If I press the Send button again (With the same string/message) I notice this in the debugger:

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

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:

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

So if I press the send text button again we see this for the Line property:

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

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).

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:

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

Everything displays correctly:

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

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. The SendMessage in the original code uses the text from txtMessage. I think you are using the text from the wrong control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜