2013-12-20 30 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));