我已经开始为一个涉及在较新引擎上重建游戏的小项目制作主服务器程序。主服务器程序当前如下所示:
带有“Found 4 installed processor(s)”的大文本框是一个“控制台”,它输出使用主服务器发送到客户端/游戏服务器和从客户端/游戏服务器发送的原始事件消息。不能输入,管理员(唯一可以访问主服务器程序这个界面的人)只能从文本框中复制;他们不能删除/添加任何东西。
问题是因为它应该是一个“控制台”,它应该自动向下滚动到多行文本框的最后一行。
Stack Overflow 上有很多问题涵盖了这一点(例如这个),但是当我将代码放在子例程中时,我无法让它工作(文本框不会向下滚动)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 中也适用于我,因此我在 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