FlowLayoutPanel scrollbar doesn't disapear properly sometimes - c#

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:
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:
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")

Related

How to make Winform scrollable in C#

I have create (I am using Visual Studio 2012) winform that have this proprities:
Width: 812
Height: 667
FormBorderStyle: Fixed 3D
In right side of put vScrollBar (that have height: 599px, which is exactly the size in my fixed form). Now I whant that my form height would be heigher then 667px, and with help of scrollbar, you scroll to bottom of form. Let me put it in picture:
Now I whant to create If you scroll with scroll bar textboxs and etc. moves down, so you can see textboxes that are at the moment hidden (they are down of form).
So far I do not have any code.
Is there any tutorial that could help me. Realy thanks for help.
You can add a FlowLayoutPanel and set the these properties to that:
flowLayoutPanel1.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
flowLayoutPanel1.WrapContents = false;
flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
flowLayoutPanel1.AutoScroll = true;
and you can see the scroll, you can use the Panels and design your application.
Or completely different, You can set the this.AutoSize = true; and drag one control to outer of form(big and small the size) and then, you can see the scrollBar of form.
You can simply try this.AutoScroll = true; which will make automatic scrolling

Flickering scrollbars in ScrollableControl

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:
What it looks like while scrolling:
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?
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'.
set on panel scrolling event this code:
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
panel1.Invalidate();
}

Making control docking and scrollbars play nicely

I've got a panel which will sometimes need more vertical screen space than naturally fits, so it needs to be able to vertically scroll. So, it's all set to AutoScroll.
The controls are contained within a TableLayoutPanel and set to dock, so they should resize their width to match. Yet, when the control triggers the scrollbar, it always ends up creating a horizontal scrollbar, even though there's no minimum width constraint on the control that's being violated. It's creating the horizontal scrollbar based on the previous width rather than respecting the dock command and redrawing the control to fit the new width.
Is there a better way round this?
Try this:
Outer panel:{AutoScroll=true, Dock=Fill}
Inner panel:{Dock=Top,Width=customwidth}
Yes, that's an inevitable consequence from the way layout is calculated. Getting rid of the horizontal scrollbar would require multiple passes through the calculation but .NET only makes one pass. For a good reason, layout can be bi-stable, flipping back and forth between two states endlessly.
I don't really understand how a TableLayoutPanel would be useful here or what makes it grow. In general, just don't dock it, give it the size you want to fill the panel. Something like this perhaps:
bool resizingTlp;
private void tableLayoutPanel1_Resize(object sender, EventArgs e) {
if (resizingTlp) return;
resizingTlp = true;
if (tableLayoutPanel1.Height <= panel1.ClientSize.Height) tableLayoutPanel1.Width panel1.ClientSize.Width;
else tableLayoutPanel1.Width = panel1.ClientSize.Width - SystemInformation.VerticalScrollBarWidth;
resizingTlp = false;
}

Set line and page size for ScrollViewer

Is it possible to set the number of logical units the ScrollViewer should scroll when either clicking the arrows of the scrollbar (line up, line down) or inside the scrollbar (page up, page down)?
You can try this:
ScrollBar verticalScrollBar = scrollViewer.Template.FindName("PART_VerticalScrollBar", scrollViewer) as ScrollBar;
verticalScrollBar.SmallChange = 5;
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/577fdd3a-e416-43ec-b2a0-9d36ba8be3f3/
Other than that, I can't find any out-of-the box solution without you rolling your own.
Wpf ScrollViewer Scroll Amount

Combo box in a scrollable panel causing problems

I have a panel with AutoScroll set to true. In it, I am programmatically adding ComboBox controls. If I add enough controls to exceed the viewable size of the panel a scroll bar appears (so far so good). However, if I open one of the combo boxes near the bottom of the viewable area the combo list isn't properly displayed and the scrollable area seems to be expanded. This results in all of the controls being "pulled" to the new bottom of the panel with some new blank space at the top. If I continue to tap on the drop down at the bottom of the panel the scrollable area will continue to expand indefinitely. I'm anchoring the controls to the left, right and top so I don't think anchoring is involved. Is there something obvious that could be causing this?
Update: It looks like the problem lies with anchoring the controls to the right. If I don't anchor to the right then I don't get the strange behavior. However, without right anchoring the control gets cut off by the scroll bar.
Here's a simplified test case I built that shows the issue:
public Form1()
{
InitializeComponent();
Panel panel = new Panel();
panel.Size = new Size(80, 200);
panel.AutoScroll = true;
for (int i = 0; i < 10; ++i)
{
ComboBox cb = new ComboBox();
cb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
cb.Items.Add("Option 1");
cb.Items.Add("Option 2");
cb.Items.Add("Option 3");
cb.Items.Add("Option 4");
cb.Location = new Point(0, i * 24);
panel.Controls.Add(cb);
}
Controls.Add(panel);
}
If you scroll the bottom of the panel and tap on the combo boxes near the bottom you'll notice the strange behavior.
This seems to be a problem specific to the devices you are using. I can't reproduce this behavior at all. Have you tried setting the Dock property of the Panel? How about the other properties that affect scrolling behavior (AutoScrollMargin, AutoScrollMinSize, AutoScrollPosition)? I'd also play with the size of the panel, and maybe use the System.Windows.Forms.Screen class to determine it automatically based on the device.
Try setting autoscroll to false and try using scroll bars normally.
Alternatively, if you are unable to find another reasonable solution, it would be possible to handle the scroll/resize events and manually resize/move your controls (Exactly what anchor does) While this would be quite annoying to do, it would get rid of any problems you may be having.
I too was able to test against a device of Windows CE, under Visual Studio 2005, .Net CF 2.0 as we too have stuff using older equipment too. I have no problems. Might it be any issue of the Windows CE version on the device that is causing the issue? What device is being used (ie: Manufacturer?). I even created a brand new project of the single form, so no other things going on to alter / influence your code.
Does the behavior work while debugging, run-time, or both?

Categories