0

I have the following form set up:

https://i.stack.imgur.com/CuwCP.png (I can't post images due to this account being new)

Whenever I update the text of the underlined ToolStripLabel the scrollbar returns to top, as shown below:

https://i.stack.imgur.com/9yZu3.png

This is the code which edits the 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.

Martin Meli
  • 452
  • 5
  • 18
  • Could you add .NET Framework version and Document Outline screenshot? (while designing the form go to View -> Other Windows -> Document Outline) – Anton Kedrov Aug 26 '16 at 08:55
  • .NET Framework version: 4.6.01055 Document Outline: http://i.imgur.com/IHKYNiD.png (pnlRoomSettings.Controls are copied to Panel1 and Panel1.AutoScroll is set to true) – Martin Meli Aug 26 '16 at 09:10
  • I created a form with scrollable Panel and ToolStrip with ToolStripLabel under the Panel, but updating ToolStripLabel text (even from another thread) doesn't affect scroll-bar at all. Could you make a minimal reproducible example and upload it somewhere? – Anton Kedrov Aug 26 '16 at 09:39
  • I forgot to mention there is a checkbox at the top-right corner of the panel, I removed its contents due to confidentiality. When this checkbox has its CheckedChanged, this problem occurs. I can reproduce this example later on if necessary. – Martin Meli Aug 26 '16 at 11:15

1 Answers1

0

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.

I see that you already invoking when changing text of tsLblRequests, but might be there is some other labels, which are requiring invoke. 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).

If ScrollControlIntoView method is not flexible enough, then might be VScrollBar control will work better for you.

Anton Kedrov
  • 1,767
  • 2
  • 13
  • 20