简体   繁体   English

C#Windows窗体-更新toolStripLabel文本将自动滚动面板返回顶部

[英]C# Windows Forms - Updating toolStripLabel text returns auto-scrollable panel to top

I have the following form set up: 我设置了以下表格:

http://i.imgur.com/BKBGbGw.png (I can't post images due to this account being new) http://i.imgur.com/BKBGbGw.png (由于此帐户是新帐户,我无法发布图片)

Whenever I update the text of the underlined ToolStripLabel the scrollbar returns to top, as shown below: 每当我更新带下划线的ToolStripLabel的文本时,滚动条就会返回顶部,如下所示:

http://i.imgur.com/OVPzDdS.png http://i.imgur.com/OVPzDdS.png

This is the code which edits the ToolStripLabel.Text: 这是编辑ToolStripLabel.Text的代码:

private void OnTimedEvent(object source, EventArgs e)//requests data
{
    try
    {
        if (commTool != null)
        {
            pollTimer.Stop();
            commTool.Poll();
            writeRequests(commTool.PollCount.ToString());
            if (!stopTimer)
            {
                pollTimer.Start();
            }
            if (errorRate < 0.25)
            {
                setColour(Color.Green);
            }
            else if (errorRate > 0.25 && errorRate < 0.5)
            {
                setColour(Color.GreenYellow);
            }
            else if (errorRate > 0.5 && errorRate < 0.75)
            {
                setColour(Color.Yellow);
            }
            else
            {
                setColour(Color.Red);
            }
        }
        else
        {
            Console.WriteLine("Error MainMenu.cs, OnTimedEvent(): commTool = null");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error MainMenu.cs, OnTimedEvent(): " + ex.ToString());
    }
}

public void writeRequests(string pollCount)
{
    requests = commTool.PollCount;
    if (statusBar.InvokeRequired)
    {
        SetTextCallback d = new SetTextCallback(writeRequests);
        Invoke(d, new object[] { pollCount });
    }
    else
    {
        tsLblRequests.Text = "Requests: " + pollCount;
    }
}

Any advice or solutions are much appreciated. 任何建议或解决方案均不胜感激。

I added checkbox and now I can reproduce this behaviour as well. 我添加了复选框,现在我也可以重现此行为。 First I thought that it happens because checkbox got focused when check state changed and, of course, panel will automatically scroll to show the focused control. 首先,我认为这是因为复选框在检查状态更改时变得集中,并且面板当然会自动滚动以显示集中的控件。 But then I discovered that it will only scroll if I changed ToolStripLabel text as well, without checking InvokeRequired property. 但是后来我发现,只有在更改ToolStripLabel文本的情况下,它才会滚动,而无需检查InvokeRequired属性。

I see that you already invoking when changing text of tsLblRequests, but might be there is some other labels, which are requiring invoke. 我看到您在更改tsLblRequests的文本时已经在调用,但是可能还有其他一些标签需要调用。 I think that might be the case. 我认为可能是这样。

If it's not the case, then you can try to adjust scroll position to show specific control with panel1.ScrollControlIntoView(label1) . 如果不是这种情况,则可以尝试使用panel1.ScrollControlIntoView(label1)调整滚动位置以显示特定控件。

If ScrollControlIntoView method is not flexible enough, then might be VScrollBar control will work better for you. 如果ScrollControlIntoView方法不够灵活,则VScrollBar控件可能会更好地为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM