2013-12-20 32 views
-1

我在Console WriteLine文本框中遇到问题。 我给这家将ConsoleWriteLine转换为文本框

Console.WriteLine(string.Format("IP: {0} Port: {1}", ip2, port)); 
StringBuilder sb = new StringBuilder(); 
sb.AppendLine(string.Format("IP: {0} Port: {1}", ip2, port)); 
textBox2.Text = sb.ToString(); 

码虽然消息框工作,

MessageBox.Show(Convert.ToString(ip2+":"+port)); 

这里是我的全部source codes。 我在这里做错了什么?

更新:

另一个solution使用的TextWriter控制台输出重定向到文本框。

+2

你怎么可以在相同的代码位控制台应用程序和文本框(Windows/Web窗体?)? – Andrew

+0

@Andrew,每个应用程序(独立于它的UI)都可以写入控制台。虽然它可能对普通用户不可见,只是在输出窗口中进行调试时或者从cmd.exe运行时 – RononDex

+0

那么你每天都会学到新的东西:) – Andrew

回答

3

你有一个多线texbox? AppendLine()在字符串末尾添加新的行字符,这会导致单行文本框显示新行而不是文本行。

尝试使用Append()代替:

sb.Append(string.Format("IP: {0} Port: {1}", ip2, port));