自动滚动到由backgroundworker更新的多行文本框的底部 [英] autoscroll to bottom of multiline textbox being updated by backgroundworker

查看:59
本文介绍了自动滚动到由backgroundworker更新的多行文本框的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

我尝试使用scrolltocaret()方法,并在其前和后没有SelectionStart = txtlog.Text.Length命令.也许我把它放错了地方?

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

下面的一些代码示例:

授权代码:

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

我想我的主要问题是:我很确定textbox.scrolltocaret()是用于我想要的东西的正确方法,但是我放置它的最佳位置在哪里?我已经在代表,主要的背景工作人员以及之前和之后的工作中尝试过.在runworkerasync()方法之后.这些都不起作用,现在我很困惑!

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.Text = text
textbox.AppendText(text)

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

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.

这篇关于自动滚动到由backgroundworker更新的多行文本框的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆