ASt*_*her 6 vb.net textbox winforms visual-studio-2015
我已经开始为一个涉及在较新引擎上重建游戏的小项目制作主服务器程序。主服务器程序目前看起来像这样:
带有“发现 4 个已安装处理器”的大文本框是一个“控制台”,它使用主服务器输出发送到客户端/游戏服务器和从客户端/游戏服务器发送的原始事件消息。不能输入,管理员(唯一可以访问主服务器程序这个界面的人)只能从文本框中复制;他们不能删除/添加任何东西。
问题是,因为它应该是一个“控制台”,它应该自动向下滚动到多行文本框的最后一行。
有堆栈溢出覆盖这个(许多问题这一个为例),但我一直没能得到它的工作(文本框不向下滚动)的范围内放置代码时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
Run Code Online (Sandbox Code Playgroud)
它不起作用,但它确实会导致程序中的错误,其中每一行都附加了程序的标题很多次:
[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
Run Code Online (Sandbox Code Playgroud)
过去,一些 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
Run Code Online (Sandbox Code Playgroud)
一种更有用的解决方案:
textBox1.SelectionStart = textBox1.Text.Length
textBox1.ScrollToCaret()
Run Code Online (Sandbox Code Playgroud)
它只是将光标放在文本框中文本的末尾并滚动到当前光标位置。完美地为我工作!
如果您正在使用AppendText
,您可以完全摆脱该console_TextChanged
方法,因为AppendText
已经为您完成了此操作。
由于某种原因(可能是一个错误?),当TextBox
未暴露在屏幕上时,AppendText
似乎没有滚动到结束。我现在没有很好的解释,需要查看.Net框架源代码。
作为解决方法,只需将所有代码移至MyBase.Shown
事件,而不是Load
事件。这按预期工作,不同之处在于,Shown
一旦表单首次在屏幕中呈现,事件就会引发,而不是Load
在呈现表单之前触发事件。
归档时间: |
|
查看次数: |
12152 次 |
最近记录: |