1

I've got this UserControl derived from ScrollableControl and I only wish to show a vertical scrollbar. As by no means I can configure to only show the vertical scrollbar, no matter what. It seems like a flaw of winforms. Another problem is that whenever the parent resizes, making the control less wide, the horizontal scrollbar is shown immediately until the OnResize event handler adjusts the width. As a result the horizontal scrollbar flickers, as its contents it temporary wider than the width of the scrollabe control. This makes the components inside the scrollable control redraw unnecessary times as they adjust to the available space. When the control is made wider, the horizontal scrollbar is never shown.

So I googled around and found this: Add vertical scroll bar to panel in .NET

Seems promosing, but now both scrollbars, and the contents of the panel are flickering whenever it is scrolled by this external scrollbar. The problem of resizing the panel to become less width, showing and hiding the horizontal scrollbar, and causing unnecessary redraws is no more though, so thats a win.

This is what the constructor of the control looks like:

        public BarGraphPanel()
        {
            this.HScroll = false;
            this.VScroll = false;
            this.AutoScroll = false;
            this.VerticalScroll.Visible = false;
            this.HorizontalScroll.Visible = false;
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.DoubleBuffered = true;
        }

What it should look like while scrolling:

enter image description here

What it looks like while scrolling:

enter image description here

That the textboxes aren't drawn so nicely while scrolling is something I can accept, but both scrollbars flickering (so temporary showing actually 3 scrollbars!) is madness.

Is there a way of only having one scrollbar on screen (always) without any of the other bars or its contents flickering?

Community
  • 1
  • 1
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • As for the flicker, it's not refreshing. A well-placed `Invalidate()` on the UserControl should do it. – DonBoitnott Feb 12 '14 at 14:12
  • Don't use that Vertical ScrollBar. Just set the AutoScrollMinSize of the panel to get your scrollbar. – LarsTech Feb 12 '14 at 14:16
  • @DonBoitnott That doesn't seem to be enough. While struggling with this all after noon I finally found an answer: http://stackoverflow.com/questions/3341032/during-flowlayoutpanel-scrolling-background-distorts-flickers – Mike de Klerk Feb 12 '14 at 14:16
  • @LarsTech That doesn't solve the redrawing problem when the horizontal scrollbar flickers when the panel is resized less wide. – Mike de Klerk Feb 12 '14 at 14:16
  • Try adding `this.ResizeRedraw = true;` It's not customary to put "Solved" in the title. Your posted answer is good enough, just accept it. – LarsTech Feb 12 '14 at 14:18
  • @LarsTech The constructor of the component, as shownin the question, has this setting already set to true. Adding `this.ResizeRedraw = true;` after that doesn't seem to make a difference. – Mike de Klerk Feb 12 '14 at 14:20

2 Answers2

1

The answer of MajinFro here actually helped me out: During FlowLayoutPanel scrolling, background distorts + flickers

When I do not override CreateParams as suggested, it doesn't work, so that does seem like a essential 'trick'.

Community
  • 1
  • 1
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
1

set on panel scrolling event this code:

private void panel1_Scroll(object sender, ScrollEventArgs e)
{
  panel1.Invalidate();
}
masoud rafiee
  • 427
  • 3
  • 9