FlowLayoutPanel FlowDirection issue - c#

I'm trying to add x Buttons to a FlowLayoutPanel which is docked (DocType fill) in a Panel.
I have set the FlowDirection to Topdown as I want something like:
However, the FlowLayoutPanel has a horizontal scrollbar, not a vertical one:
I add the buttons like this:
flowLayoutPanel1.Controls.Add(new Button { Text = "Chat", Width = flowLayoutPanel1.Width - flowLayoutPanel1.Margin.Left - flowLayoutPanel1.Margin.Right});
I also tried:
flowLayoutPanel1.Controls.Add(new Button { Text = "Chat", Dock = DockStyle.Fill});
And also:
flowLayoutPanel1.Controls.Add(new Button { Text = "Chat", Anchor = AnchorStyles.Left | AnchorStyles.Right});
I tried this and also looked here. Both don't work for me.
Thanks for your help.

after some digging here I found it out.
I had to set the FlowLayoutPanel.WrapContents Property to false.

Related

How to keep the SplitBar when panel collapsed in SplitContainer?

I found that when a panel collapse in splitcontainer, the splitbar is hided either.
splitContainer1.Panel2Collapsed = true;
I would like to use double click the split bar to toggle panel2.
How to keep the split bar when one panel is collapse?
Don't collapse it:
sc.Panel2MinSize = 0;
sc.SplitterDistance = sc.ClientSize.Width - sc.SplitterWidth;

Cannot remove spacing between controls in TableLayoutPanel?

There's some spacing between the Buttons I add to my TableLayoutPanel. I removed the border in the Button and set the Margin and Padding to 0 in the Panel. But I continue getting that spacing.
tableLayoutPanel.RowCount is set to 8 and the Rows collection I've added 8 rows with Size Type Absolute.
Am I missing something? Here's the code:
private void FillSelectLayout()
{
tableLayoutPanelSelect.Controls.Clear();
tableLayoutPanelSelect.RowStyles.Clear();
tableLayoutPanelSelect.RowCount = 8;
for (int i = 0; i < 8; i++)
{
Button buttonSelector = new Button();
buttonSelector.Height = 64;
buttonSelector.Width = 100;
buttonSelector.FlatStyle = FlatStyle.Flat;
buttonSelector.FlatAppearance.BorderSize = 0;
buttonSelector.BackColor = Color.Yellow;
tableLayoutPanelSelect.Controls.Add(buttonSelector, 0, i);
}
}
Here's how it's displayed:
To remove the space between buttons in cells, it's enough to set dock property of them to fill and then remove default margins of buttons:
var b = new Button();
b.Dock = DockStyle.Fill;
b.Margin = new Padding(0);
Note:
Usually it's better to set Dock property of controls which you host in cells to Fill. This way your controls will follow TableLayouPanel sizing rules which you set for columns and rows.
TableLayoutPanel use Margin property of control to set the location of control in cell. So If you don'n want to set Dock and you prefer to set the Size manually, it's enough to set the Margin only.
I .. set the Margin and Padding to 0 in the Panel.
Why didn't you remove the Margin in the Buttons instead:
buttonSelector.Margin = new Padding(0);
MSDN:
The Margin property defines the space around the control that keeps
other controls a specified distance from the control's borders.
I faced the same problem while using different control in TableLayoutPanel
You can do this
Go to Design View
Click on the properties
GoTo Columns, When you click text box besides Columns, a button (...) appears on extreme right, click it
A pop up window appears, Select AutoSize (Instead of Absolute or Percentage).
In the same window in Show: select Rows and again select Autosize.
Click okay and you are done.

Unable to scroll to the bottom of WinForm

I am using visual studio 2012 for this. Basically I have a WinForm that I want to expand.
Inside the form designer, I am able to see that my form has a scroll bar, but when I compile the program, the scroll bar does not appear. The controls that are beyond my screen size are clipped off, as opposed to having a scrollbar.
Are there any settings that I have missed out? Currently I set my AutoScroll = true.
Scrollbars show up when a parent control has the AutoScroll set to true and a child control has a MinimumSize such that the client area of the child control is larger than the client area of the parent control.
E.g.
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var sampleForm = new Form() { AutoScroll = true };
Panel panel = new Panel() { BackColor = Color.Red, AutoSizeMode = AutoSizeMode.GrowAndShrink, AutoSize = true };
Button btn = new Button { Text = "Toggle MinSize", AutoSize = true };
panel.Controls.Add(btn);
btn.Click += delegate {
if (panel.MinimumSize == Size.Empty)
panel.MinimumSize = new Size(600,600);
else
panel.MinimumSize = Size.Empty;
};
sampleForm.Controls.Add(panel);
Application.Run(sampleForm);
}
If your child panel correctly calculates its preferred size, then you can override the MinimumSize property and return the PreferredSize.
AutoScroll = true is enough to display scroll on form no other setting is required.
just try other thing add panel in form and set panels AutoScroll = true and then add control to it and check that scroll is working or not ?
Take a look at the properties of the controls within the container for which you want autoscroll to work. One possibility is that you set one or more of those controls Anchor property to Right or something, which can reverse the autoscroll setting behind the scenes to effectively turn it off. Also check the RightToLeft property of the container, and try setting that to the default "no"
Make sure you have set Dock.Fill i.e. Dock property to Fill
Set property AutoScroll = true , AutoSize = true, AutoSizeMode = GrowOnly ,you can also do this by adding a panel to the form and set panel AutoScroll = true.
compare your issue with example here

Wrapping Text on Label and Vertical scrollbar on panel

I have a label inside a panel. When the text exceeds, the label text should wrap. For doing that I have set mylabel's AutoSize = false and MaximumSize = 100,0.
Now since the text is being wrapped, vertical scrollbar should appear on panel. But that's not happening, please specify what I am missing here.
Is it possible this way or should I explicitly add a vertical scrollbar inside the panel?
1) You need to put the label inside the panel
2) AutoSize for label should be TRUE
3) AutoSize for panel should be FALSE
4) AutoScroll for panel should be True
that is it!
You should be setting AutoSize to true to automatically wrap. For the scrollbars check that you set panel.VerticalScroll.Visible = true;
Did you have the properties Scrollable=true or AutoScroll?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.scrollbars.aspx
Try this:
ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (sender, e) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);
It's long time ago about this question.
Solution :
Panel1.AutoScroll = True
Label1.AutoSize = True
Label1.MaximumSize = New Size(Panel1.ClientRectangle.Width - 18, 0)
Impotrant thing is to define MaximumSize for Label width. Height leave 0 (zero). Height will grow with label content.
In this case, maximum width of label would be width of panel - 18px for scroller.

Scrollbar location is moving visible/nonvisible changing controls

I have a form with a scrollable panel and two controls sitting right on top of each other - one visible one not. Based on a certain condition when that form is activated I might swap the visible properties of the two controls. These controls are at the bottom of the scrollable panel. If when I leave that form I leave it scrolled to the bottom, go change the condition that will cause the controls' visibility to swap and go back to that form the visible control will have dropped about 200px down the page leaving a large gap. Anyone know what could be causing this? I tried resetting the scrollbar position to the top on form close but that just causes a smaller gap and sometimes the control to move higher into other controls. Any ideas?
Here is an example that reproduces the problem. If the mouse is moved over the red label, the visibility of button2 is changed to true which causes the scroll jumps back up to Button1.
public class Form123456 : Form {
public Form123456() {
Controls.Add(new UC1());
}
public class UC1 : UserControl {
Button b1 = new Button { Text = "Button1" };
Label lb = new Label { Text = "_", AutoSize = true, BackColor = Color.Red };
Button b2 = new Button { Text = "Button2", Visible = false };
Button b2b = new Button { Text = "x" };
Button b3 = new Button { Text = "Button3" };
public UC1() {
AutoScroll = true;
Dock = DockStyle.Fill;
b1.Location = new Point(0, 200);
b2.Location = new Point(0, 600);
lb.Location = new Point(70, 600);
b2b.Location = new Point(90, 600);
b3.Location = new Point(0, 800);
Controls.Add(b1);
Controls.Add(b2);
Controls.Add(lb);
Controls.Add(b2b);
Controls.Add(b3);
lb.MouseEnter += delegate {
b2.Visible = true;
};
lb.MouseLeave += delegate {
b2.Visible = false;
};
}
}
}
To fix it, one solution is to add this code:
protected override Point ScrollToControl(Control activeControl) {
return this.AutoScrollPosition;
}
Solution from:
Why does clicking in a text box cause an AutoScroll panel to scroll back to the top?
No repro. Sounds to me that you are doing more than just changing the Visible property. Whenever you assign the Location property, you have to add the AutoScrollPosition to compensate for the scroll state. Post code if this doesn't help.
Have you verified the order that you change visibility of the two controls?
The scroll bars on a container with auto scroll set to true will appear and disappear depending on the position of controls that are outside of the visible area of the control. Controls that are invisible do not count.
So in your case if you make both controls invisible at anytime, the scroll bars will disappear. They will come back when one control is made visible. So to make sure you don't have a jump in scroll bar position and controls position you should make sure that at no time are both controls invisible. Another solution is to have a pseudo-visible control on the container. That is a control that has its visibility set to true but it is not actually visible for the user (for example a dot of the color of the background, a label with no text ...). Position this control in the furthest position x,y and the scroll bars will never disappear..

Categories

Resources