0

I am facing a really strange issue and after googling and searching in StackOverflow i absolutly have no clue how to fix that because it only happens sometimes.

My Problem:

I have a flowLayoutPanel (flp) and only a the vertical scrollbar if controls inside my flp doesnt fit it's size. I don't use a horizontal scrollbar. Code for that:

private void setFlowcontrolScrollbar(flowLayoutPanel fc)
{
    int height = 0;

    foreach (FilePanel fp in fc.Controls)
    {
        height += fp.Height;
    }

    if (height > fc.Height)
    {

        fc.VerticalScroll.Visible = true;
        fc.VerticalScroll.Enabled = true;
    }
    else
    {
        fc.VerticalScroll.Visible = false;
        fc.VerticalScroll.Enabled = false;
    }

    fc.HorizontalScroll.Visible = false;
    fc.HorizontalScroll.Enabled = false;
}

This method is called after the flp was resized or new items were added to it. Controls in my flp are resized only in horizontal direction.

Now this works fine, i can scroll if there are to many controls in my flp. But let's say i have lot's of controls in my flp (=> vertical scrollbar enabled,visible) and now i delete some controls so i would not need a scrollbar anymore. Functional everything works fine, i cant scroll or click on a scrollbar. But sometimes there is a visual bug. The scrollbar doesn't disapear and i can do strange things like in this picture:

enter image description here

If i also call void setFlowcontrolScrollbar after removing a control this happens almost all the time when i remove a control and scrollbar is not needed any more:

enter image description here

In both pictures i resized my form a bit to the right so you can see how my controls overlap with the scrollbar. How do i fix that? Is there a better way to only activate vertical scroll?

IMPORTANT EDIT:

Bug only occurs if i use drag'n'drop to remove lines (controls) from my flp and only if i touched/hovered over the scrollbar while draging. If i drag it out on the left side and drop it somewhere else the scrollbar disappears correct.

Note: Found a better solution to get rid of horizontal scroll.

panel.HorizontalScroll.Maximum = 0;
panel.AutoScroll = false;
panel.VerticalScroll.Visible = false;
panel.AutoScroll = true;

(Source: How do I disable the horizontal scrollbar in a Panel , answer from "Kbv Subrahmanyam")

Community
  • 1
  • 1
ysfaran
  • 5,189
  • 3
  • 21
  • 51
  • 1
    Don't manipulate scrollbars, don't check for height. Just set `AutoScroll` property of `FlowLayoutPanel` to `true`. – Reza Aghaei Oct 27 '16 at 09:05
  • That was my first attempt too. Problem with that: If i resized my window really fast or maximized and "un"-maximized it again the horizontal scrollbar appeared and i could scroll with it into white space. The reason for that(i guess) is that the Scrollbar uses DisplayedRectangle and not ClientRectangle? DisplayedRectangle is not updated if i resize very fast.. I need to resize a bit again so the horizontal scrollbar disapears. – ysfaran Oct 27 '16 at 09:37
  • If the screenshots are from a real execution without manipulating images, then it seems the scrollbars doesn't belong to the container of those list items. – Reza Aghaei Oct 27 '16 at 09:42
  • These are real screenshot from real executions. No Manupulation. The white big box is my FlowLayoutPanel (FLP). Lines in my FLP are Custom UserControls. So in my first picture i have one FLP with five UserControls. Usually the scrollbar is aligned right. But when i remove some lines(Controls), so i usually wont need a scrollbar, the scrollbar doesn't disapear it has fix position then(not clickable or smthn just visually there). I just resized my form a bit to the right. – ysfaran Oct 27 '16 at 11:32
  • If i would resize it back to the left again, the overlapping area of the Scrollbar and UserControls would be white again. In other words: the scrollbar only disapears if antoher Control is overlapping it. I would need something like: void redrawFlowcontrol(); – ysfaran Oct 27 '16 at 11:32
  • Clearly you are solving this problem the wrong way. Layout can be bi-stable with *two* solutions. Caused by the scrollbars removing available space from the panel. It is important to call the panel's SuspendLayout() method before you start resizing. That suppresses excessive painting and avoids layout flipping back-and-forth between the two solutions. If that is not practical for some reason then the only good workaround is to make the panel bigger. – Hans Passant Oct 27 '16 at 12:16
  • @Hans Passant Thanks a lot it worked!! Can i set your comment as answer? – ysfaran Oct 27 '16 at 13:33
  • Oh not it still doesnt work.. it only happens if i drag two or more files out of my flp and when my mouse touched the scrollbar while draging.. – ysfaran Oct 27 '16 at 13:52

0 Answers0