vb.net 自动滚动到由后台工作人员更新的多行文本框的底部

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19728585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:16:24  来源:igfitidea点击:

autoscroll to bottom of multiline textbox being updated by backgroundworker

vb.nettextboxscrollbackgroundworker

提问by almg

I have a background worker control that is set to perform a task, and update a multiline text box on my main UI using a delegate procedure. this is all working perfectly, however once the updating scrolls off the bottom of the text box, the scroll bars appear, but the continuous refreshing causes the text box to stay locked at the top. Ideally, I would like the text box to auto-scroll itself to the bottom to show the latest entry in real-time. What would be the best way to implement this?

我有一个后台工作控件,该控件设置为执行任务,并使用委托过程更新主 UI 上的多行文本框。这一切都很好,但是一旦更新从文本框底部滚动,滚动条就会出现,但持续刷新会导致文本框保持锁定在顶部。理想情况下,我希望文本框自动滚动到底部以实时显示最新条目。实现这一点的最佳方法是什么?

I have tried using the scrolltocaret()method, with and without a SelectionStart = txtlog.Text.Lengthcommand preceding it. perhaps I'm putting it in the wrong place?

我曾尝试使用该scrolltocaret()方法,在SelectionStart = txtlog.Text.Length它前面有或没有命令。也许我把它放在错误的地方?

some code samples below:

下面的一些代码示例:

Delegate code:

委托代码:

Delegate Sub updateresults_delegate(ByVal textbox As TextBox, ByVal text As String)

Private Sub updatelog_threadsafe(ByVal textbox As TextBox, ByVal text As String)
            If textbox.InvokeRequired Then
                Dim mydelegate As New updateresults_delegate(AddressOf updatelog_threadsafe)
                Me.Invoke(mydelegate, New Object() {textbox, text})
                'Me.txtlog.SelectionStart = txtlog.Text.Length
                'Me.txtlog.ScrollToCaret()
            Else
                textbox.Text = text
            End If
        End Sub

main backgroundworker activity:

主要后台工作者活动:

For i As Integer = val1 To val2
'generate an IP address from split host parts and current value of i
                host = s1(0) & "." & s1(1) & "." & s1(2) & "." & i
                Try 'attempt to ping the IP
                    Dim reply As PingReply = pingsender.Send(host, timeoutval, buffer, options)
                    If reply.Status = IPStatus.Success Then
                        name = System.Net.Dns.GetHostEntry(host)'get DNS entry
                        resulttext += String.Format("{1} - {2}: reply: Bytes={3} time{4} TTL={5}{0}", vbCrLf, name.HostName, reply.Address.ToString, reply.Buffer.Length, getms(reply.RoundtripTime), reply.Options.Ttl) 'print out success text
                    Else
                        resulttext += String.Format("      {1}: Ping failed. {2}{0}", vbCrLf, host, reply.Status.ToString) 'print out fail text
                    End If
                    updatelog_threadsafe(txtlog, resulttext) 'send text to textbox

            System.Threading.Thread.Sleep(1000)
        Catch ex As Exception

        End Try
    Next

I guess my main question is: I'm pretty certain that the textbox.scrolltocaret()is the correct method to use for what I want, but where is the best place for me to put it? I've tried it in the delegate, the main backgroundworker, as well as before & after the runworkerasync()method. none of these worked, and now I'm stumped!

我想我的主要问题是:我很确定这textbox.scrolltocaret()是我想要的正确方法,但是我把它放在哪里最好?我已经在委托、主要后台工作人员以及方法之前和之后尝试过runworkerasync()。这些都不起作用,现在我很难过!

回答by LarsTech

Try it this way:

试试这个方法:

'textbox.Text = text
textbox.AppendText(text)

The code you commented out wasn't running on the GUI thread, and as M Granja pointed out, AppendText will automatically scroll to the appended text in the box, so no need to call ScrollToCaret.

您注释掉的代码没有在 GUI 线程上运行,正如 M Granja 指出的那样,AppendText 将自动滚动到框中的附加文本,因此无需调用 ScrollToCaret。

回答by peter

xxx.SetFocus ' xxx = the name of the textbox

xxx.SetFocus ' xxx = 文本框的名称

SendKeys "^{END}" ' pop to last line

SendKeys "^{END}" ' 弹出到最后一行