Combo box in a scrollable panel causing problems - c#

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?

Related

How do I add a form to the panel properly

I designed a side panel and when I press the buttons, the form inside the panel on the right changes. However, the components in the forms I designed appear in the wrong places. Some are not visible because they are left outside.
I upload some photos. Design Screen and
at Runtime
Where am i doing wrong ? Panel Size and form size are same.
UPDATE:
I created usercontrol instead of form. However, nothing has changed. For example, when I put two labels side by side with 5 px between them in the design, it seems to have much more spacing if I run it. Or the labels I placed at the far right or at the bottom do not appear at runtime.
TelemetriUI telemetriUI = new TelemetriUI();
telemetriUI.TopLevel = false;
telemetriUI.FormBorderStyle = FormBorderStyle.None;
telemetriUI.Dock = DockStyle.Fill;
this.mainPanel.Controls.Add(telemetriUI);
this.mainPanel.Tag = telemetriUI;
telemetriUI.BringToFront();
telemetriUI.Show();

How to create a scroll section that I can add buttons to on runtime?

I am new to the winforms and want to create an application that will have a scroll section which I need to make for the buttons which I will add on runtime.
The user will press an add button to add a button containing data which will then be used for the title, description. I need the button to be added to the scroll section and to be automatically placed under the last button in the section so it is organized and so I can scroll up and down through the section.
Like the image has with the scroll bar and the buttons:
How do I solve the problem?
You can use a FlowLayoutPanel with the following properties set:
AutoScroll = true;
FlowDirection = FlowDirection.TopDown;
WrapContents = false;
Once you do that, you can add buttons (or any control) to the Controls collection and it should flow from top to bottom and cause vertical scrollbars to appear once you fill the space.
Note, that it is quite difficult (or virtually impossible) to make a WinForms app look as nice as the one you provided an example of. You would probably be better off using WPF. But my answer should at least solve your particular problem with WinForms.

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

Winforms: FlowLayoutPanel with Docking

This is in winforms. I am creating a User control that is basically a FlowlayoutControl filled with other User Controls. I need each of the controls added to be docked to the top of the previous (from left to right). Unfortunately it looks like the flowlayoutcontrol ignores any of the docking properties. Is there any way to dock the controls inside there? I need it to fill the item from left to right, but the items should be laid out like a list view. Theres really no code i can provide due to the fact that its a matter of figuring out what approach to take.
Getting the FlowLayoutPanel to dock right is tricky. From the original question, you want something like a list view. It's important to know that ONE of the items in your list (the widest one) defines a "virtual column" in the FlowLayoutPanel. The rest of the items will follow it. You can prove this in the VS designer by dragging one of the items to the right. The 'virtual column' will follow it, and if your other items are anchored they will follow the virtual column.
Note that you can't anchor the control that is defining the column. It has nothing to anchor to and strange things will happen.
Do do all this programatically, handle the Layout event on your FlowLayoutPanel and put code similar to the code below. It's important that in the designer all the items in your list are not docked and and have their anchoring set to 'none'. I spent a day on this yesterday and doing that in the designer is what made the code below work.
flowLayoutPanel.Controls[0].Dock = DockStyle.None;
flowLayoutPanel.Controls[0].Width = flowLayoutPanel.DisplayRectangle.Width - lowLayoutPanel.Controls[0].Margin.Horizontal;
for (int i = 1; i < flowLayoutPanel.Controls.Count; i++)
{
flowLayoutPanel.Controls[i].Dock = DockStyle.Top;
}
FlowLayoutPanel.FlowDirection Property indicates the flow direction of the FlowLayoutPanel control.
FlowLayoutPanel.WrapContents Property indicates whether the FlowLayoutPanel control should wrap its contents or let the contents be clipped.
The docking properties of the FlowLayoutPanel are for the panel itself (like if you wanted the FlowLayoutPanel docked to the left of the form, etc.), not the container of controls inside of it.
Try playing with the DefaultPadding and DefaultMargin properties, these apply to the spacing of the controls it contains

scroll bar TableLayoutPanel c#

I have a TableLayoutPanel that has several TableLayoutPanels inside it. The amount changes dynamically and will almost always be too many to be able to fit inside the form.I need it to have a scroll bar so I can view the entire component.
I have tried setting the autoscroll property on the main panel to true and docking it and/or setting a maximum size. What the controler does instead, is to try and fit ALL of the Panel inside the form therefore changing the size of its inside components and squeezing them all together instead of creating a scroll bar to scroll through my Panel.
Do you guys know what I might be doing wrong?
Thanks.
Jose
PS: I am using VS 2010
I had the same issue one day and found out that the problem was that I had a "MinimumSize" set to the TableLayoutPanel. That caused the control to keep a minimum height no matter what the Dock and/or Anchor constraints, preventing the AutoScroll feature to work properly. Since that feature is based on a simple check on the Y coordinates of all children controls of a control against that control's height, the scrollbar was not appearing despite the fact the the TableLayoutPanel's child controls were "disapearing" out of sight due to its parent control clip area.
So in other words, check the MinimumSize property of TableLayoutPanel and make sure it's empty.
Maybe this will help.
if it still doesn't work, try put a panel and then put the tableLayoutPanel into that panel. Set to true the autoScroll property of the panel.
I had the same thing happen on my project. All my controls were squished inside the TableLayoutPanel. In my case, I was rendering many of the controls as ColumnStyle.Percent. Switching this to ColumnStyle.Autosize was the fix I needed.
I assume you've set these properties as well, but just in case my TableLayoutPanels also use the following settings:
AutoSize = true;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
AutoScroll = true;
Late answer, but I've been fighting with a complex layout recently and was looking for a solution to get my scrolling working on a screen with far too many fields. Honestly, better design should avoid this problem 99% of the time but sometimes your hands are just tied.
The Problem
The problem seems to be that if you're nested too deeply with multiple grids, groups, and panels they stop reporting properly to parent controls that their contents have overflown the size of the current screen. Actually, the problem is probably that the controls are all set to dock fill and do fit within their parent control, so the top level control doesn't know that the contents of its great-great-great-great-grandchild controls don't fit.
Solution
The trick to fix this seems to be manually forcing the top most panel to scroll at a certain minimum size:
MainPanel.AutoSize = true;
MainPanel.AutoScrollMinSize = new Size(400, 500);
TableLayoutPanel scrolling is full of bugs.
The solution to make it work correctly is here.

Categories