6

我已经开始为一个涉及在较新引擎上重建游戏的小项目制作主服务器程序。主服务器程序当前如下所示:

在此处输入图像描述

带有“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
4

5 回答 5

6

另一个有用的解决方案:

textBox1.SelectionStart = textBox1.Text.Length
textBox1.ScrollToCaret()

它只是将光标放在文本框中文本的末尾并滚动到当前光标位置。完美地为我工作!

于 2017-04-03T08:27:18.507 回答
4

如果您正在使用AppendText,则可以完全摆脱该console_TextChanged方法,因为AppendText它已经为您完成了。

由于某种原因(可能是错误?),当TextBox没有暴露在屏幕上时,AppendText似乎没有滚动到结束。我现在没有很好的解释,需要查看.Net框架源代码。

作为一种解决方法,只需将所有代码移动到MyBase.Shown事件,而不是Load事件。这可以按预期工作,不同之处在于Shown,一旦表单首次在屏幕中呈现,就会引发事件,而不是在Load呈现表单之前触发事件。

于 2014-10-27T12:13:22.797 回答
1

您可以在加载事件中执行此操作,但它更复杂:

<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
End Function

在设置文本后textbox

If (console.IsHandleCreated) Then
    'set focus
    SendMessage(console.Handle, &H7, IntPtr.Zero, IntPtr.Zero) 'WM_SETFOCUS
    'move caret to the end
    SendMessage(console.Handle, &HB1, CType(-1, IntPtr), CType(-1, IntPtr)) 'EM_SETSEL
    'scroll to the end
    SendMessage(console.Handle, &HB6, IntPtr.Zero, CType(console.Lines.Length, IntPtr)) 'EM_LINESCROLL
Else
    MsgBox("console window is not created")
End If
于 2014-10-27T15:12:15.227 回答
1

使用 aRichTextBox而不是 aTextBox您可以使用此代码。

'SENDMESSAGE constants
'move to the last row in a RichTextBox
'you can obtain the same effect using ScrollToCaret but it works only if Focus is on RichTextBox
Private Const WM_VSCROLL As Int32 = &H115
Private Const SB_BOTTOM As Int32 = 7

Private Sub WriteLog(ByVal strLineLog As String)

    If Me.rtbLog.Text = "" Then
        Me.rtbLog.Text = strLineLog
    Else
        Me.rtbLog.AppendText(System.Environment.NewLine & strLineLog)
    End If

    SendMessage(Me.rtbLog.Handle, WM_VSCROLL, SB_BOTTOM, 0)

End Sub
于 2014-10-28T08:07:11.973 回答
0

TextBox1.Select(TextBox1.TextLength - 1, 1) 'select the last chr in the textbox TextBox1.ScrollToCaret() 'scroll to the selected position

于 2022-01-12T12:48:29.380 回答