我已经开始制作一个主服务器程序,用于重建一个游戏到一个更新的引擎上的小项目。 目前主服务器程序的代码如下:
问题在于,由于它应该是一个“控制台”,所以应自动滚动到多行文本框的最后一行。
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中也适用于我,因此我尝试了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
AppendText
,因为这会导致无限循环。AppendText
将触发TextChanged
添加另一个AppendText
,然后再次触发TextChanged
,如此循环。这将永远持续下去。 - Sriram SakthivelAppendText
,那么它可以正常滚动。请提供一些简短但完整的示例来重现这个问题。 - Sriram Sakthivel