【发布时间】:2014-12-22 13:54:10
【问题描述】:
我已经开始为一个涉及在较新引擎上重建游戏的小项目制作主服务器程序。 主服务器程序目前如下所示:
带有“Found 4 installed processor(s)”的大文本框是一个“控制台”,它输出使用主服务器发送到客户端/游戏服务器和从客户端/游戏服务器发送的原始事件消息。不能输入,管理员(唯一可以访问主服务器程序这个界面的人)只能从文本框中复制;他们不能删除/添加任何东西。
问题是因为它应该是一个“控制台”,它应该自动向下滚动到多行文本框的最后一行。
Stack Overflow 上有很多问题都涉及到这个问题(例如this one),但是当我将代码放在console_TextChanged 中时,我无法让它工作(文本框不会向下滚动)子程序。
我试过这个:
Private Sub console_TextChanged(sender As Object, e As EventArgs) Handles console.TextChanged
console.AppendText(Text)
console.Select(console.TextLength, 0)
console.ScrollToCaret()
End Sub
它不起作用,但它确实会导致程序中的一个错误,即每行都会多次附加程序的标题:
[net 11:32:22.243] System Started.Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5Server Network | Crysis Wars 1.5
过去,一些 C# 解决方案在 Visual Basic .Net 中也适用于我,因此我尝试了 some of the ones on Stack Overflow,但我也无法让这些解决方案发挥作用。
这真的是自动滚动多行文本框的正确方法吗?如果是,为什么它对我不起作用?
完整(相关)代码:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
console.Text = GetNetTime() + "System Started."
WriteToConsole("Working Area: " + CStr(My.Computer.Screen.WorkingArea().Width) + "*" + CStr(My.Computer.Screen.WorkingArea().Height))
WriteToConsole("Found " + CStr(Environment.ProcessorCount) + " installed processor(s)")
Dim i As Integer = 0
While (i < Environment.ProcessorCount)
WriteToConsole("Processor " + CStr(i) + ": " + My.Computer.Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\" + CStr(i)).GetValue("ProcessorNameString"))
WriteToConsole(" Family: " + My.Computer.Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\" + CStr(i)).GetValue("Identifier"))
WriteToConsole(" Manufacturer: " + My.Computer.Registry.LocalMachine.OpenSubKey("Hardware\Description\System\CentralProcessor\" + CStr(i)).GetValue("VendorIdentifier"))
i += 1
End While
WriteToConsole("Starting networking services")
End Sub
Private Sub console_TextChanged(sender As Object, e As EventArgs) Handles console.TextChanged
console.AppendText(Text)
console.Select(console.TextLength, 0)
console.ScrollToCaret()
End Sub
Function GetNetTime()
Return "[net " + CStr(DateTime.UtcNow.Hour) + ":" + CStr(DateTime.UtcNow.Minute) + ":" + CStr(DateTime.UtcNow.Second) + "." + CStr(DateTime.UtcNow.Millisecond) + "] "
End Function
Function WriteToConsole(ByVal input As String)
console.AppendText(Environment.NewLine & GetNetTime() + input)
Return -1
End Function
【问题讨论】:
-
你是什么意思不起作用。你不应该在
TextChanged事件中调用AppendText,因为它会导致不间断循环。AppendText将触发TextChanged添加另一个AppendText进而触发TextChanged等等。这将需要永远。 -
@SriramSakthivel 这将解释为什么程序的标题被附加到每一行。我在哪里/如何正确地做到这一点?编辑添加当我使用该代码时文本框不会滚动。
-
我无法重现该问题。如果你只是单独使用
AppendText,那是有效的(我的意思是正确滚动)。发布一些简短但完整的示例来重现问题。 -
@SriramSakthivel 添加了代码,但是我仍然遇到程序标题在每行末尾附加几次的问题。我怎样才能克服这个障碍?
标签: vb.net winforms textbox visual-studio-2015