Panel scroll vertically - c#

I have a winform app in which I have a panel control.
I want to be able to scroll inside the panel and place controls vertically more then the current height of the control and then have a scroll which will help me to see all the controls, how can I achieve that?
This is the designer code as well, in case someone wants to take a look at the code:
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.BackColor = System.Drawing.SystemColors.ControlLightLight;
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(267, 365);
this.panel1.TabIndex = 0;
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(456, 410);
this.Controls.Add(this.panel1);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
}

Since you have AutoScroll = true, you shouldn't have to do anything. Any control that you place in the panel that is below the visible boundary will automatically create the appropriate scroll distance in the panel.
If you want to manually override that, set AutoScroll = false and set the size of the canvas yourself using the AutoScrollMinSize property, example:
panel1.AutoScrollMinSize = new Size(0, 1200);
You might want to consider anchoring the panel to the four sides of the form as well, or dock-fill, since it looks like a resizable form. Again, the panel will handle the scrollbar size for you.

Try this out for loading other forms in panels of MDIForm. It works perfectly.
myForm.TopLevel = false;
myForm.AutoScroll = true;
main_panel.Controls.Clear();
main_panel.Controls.Add(myForm);
main_panel.AutoScrollMinSize = new Size(0, myForm.Height);
myForm.Show();

Related

How to get a Label with AutoSize to update its Position while anchored to the Right?

I have a simple System.Windows.Forms.Label in a System.Windows.Forms.Form.
I want to dynamically resize the label to fit text loaded runtime, while keeping it Anchored to the right and bottom of its parent form.
According to the MSDN Documentation:
It is “always true” that the Location Property remains constant (i.e., that the top left position of the Control will never change).
It is “always true” that the Anchor property is respected when AutoSize is true (i.e., that the Location Property—the top-left corner—will be modified so that the Anchored Sides maintain their initial distance from the edges of their parent controls).
From my reading of this, I would expect that the second truth overrides the first when Anchor is anything but AnchorStyles.None.
However, this doesn't seem to bear out in practice.
Consider the following:
// From ExampleForm.Designer.cs
this.label = new System.Drawing.Label();
this.label.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.label.AutoSize = true;
this.label.Location = new System.Drawing.Point(600, 400);
this.label.Size = new System.Drawing.Size(170, 20);
this.label.Text = "[Populated at Runtime]";
this.label.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// ...
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label);
// ...
// Sometime after Form Initialization, this is called
void PopulateLabel() {
var oldRight = label.Right;
label.Text = "Hey here's some new text. It's pretty long so the control will have to resize";
// Without this next line, the Right Anchor distance is not maintained.
// label.Left -= (label.Right - oldRight);
System.Diagnostics.Debug.Assert(label.Anchor.HasFlag(AnchorStyles.Right) && label.Right == oldRight, "The label didn't stay anchored to the right");
}
Obviously I can work around this by tracking the distance manually, as above.
I just wonder if there isn't some way this is “supposed” to work that I'm doing wrong.
The one observation I have to offer is this: it works if the label is not anchored to the bottom.
Do I need to call Suspend/Resume/PerformLayout on the Label? on the Form?
Are the docs wrong?
Am I being foolishly naïve or completely misunderstanding something?
Do I need some sort of intermediary Control for this to work and the docs assume I know this?
To address some possible complications that show up in similar questions (or that I dreamt up):
rightToLeft is false,
Dock is DockStyle.None,
the label's Parent is the form itself, not an intermediary panel or other control.
the Margin seems irrelevant
Anchoring to the Top or Bottom seems irrelevant to Right not working.
System.Windows.Form.Button works as expected. I haven't tested other controls.
Try using a TableLayout, it tends to obey the Control layout properties better. E.g:
public class MyForm : Form {
Label label = new Label() { BackColor = Color.Blue, ForeColor = Color.White };
public MyForm() {
TableLayoutPanel panel = new TableLayoutPanel() { BackColor = Color.Green };
panel.ColumnCount = 1;
panel.RowCount = 1;
panel.Controls.Add(label, 0, 0);
panel.Dock = DockStyle.Bottom;
panel.AutoSize = true;
panel.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
panel.Controls.Add(label);
//this.label.Anchor = AnchorStyles.Right | AnchorStyles.Top;// | AnchorStyles.Bottom; // ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//this.label.Dock = DockStyle.Fill;
this.label.Dock = DockStyle.Right;
this.label.AutoSize = true;
this.label.Margin = Padding.Empty;
//this.label.Location = new System.Drawing.Point(600, 400);
//this.label.Size = new System.Drawing.Size(170, 20);
this.label.Text = "[Populated at Runtime]";
//this.label.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
// ...
//this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
//this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
//this.Controls.Add(this.label);
this.Controls.Add(panel);
// ...
Button btn = new Button { Text = "Change text" };
btn.Click += delegate {
PopulateLabel();
};
Controls.Add(btn);
}
// Sometime after Form Initialization, this is called
void PopulateLabel() {
var oldRight = label.Right;
label.Text = "Hey here's some new text. It's pretty long so the control will have to resize";
// Without this next line, the Right Anchor distance is not maintained.
// label.Left -= (label.Right - oldRight);
//System.Diagnostics.Debug.Assert(label.Anchor.HasFlag(AnchorStyles.Right) && label.Right == oldRight, "The label didn't stay anchored to the right");
}
}

Winform Designer border glitch in Windows 10

i have a PictureBoxin a Form, Dock Property of PictureBox is set to Fill.
now to keep only borders of the form, i set ControlBox property to false and FormBorderStyle to SizableToolWindow.
in Windows 7, it looks like below
but in Windows 10 same code looks like below
can anybody explain why this white border appears at top? i tried removing padding, margin & Rebuild Solution. none of that helped!
in windows 10 (Visual Studio 2015, Designer) form looks normal (without white top border)
.Net target framework version: v4.6
P.S: Image taken from here
Update: here's the Windows Form Designer generated code
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Preview_Image));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(284, 261);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Preview_Image
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 261);
this.ControlBox = false;
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Name = "Preview_Image";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}

Transparent child form in parent form with transparency and background

I have a form with background image and transparency key.
In that form I put child form which must be fully transparent to show bakground of the parent form. If I set another transparency key to child form - it do not get transparency at all, and if i set transparency key of parent form - child cut through parent form's background image.
I need to use form - not user control so thats an issue. And i dont want to set dublicate background image to child form.
Im working visually. Here's code from designer:
That is my parent
//
// Main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.DarkRed;
this.BackgroundImage = global::NWN_Tsuki.Properties.Resources.Book;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.ClientSize = new System.Drawing.Size(800, 665);
this.Controls.Add(this.tableLayoutPanel1);
this.Controls.Add(this.CloseBtn);
this.DoubleBuffered = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.KeyPreview = true;
this.MaximumSize = new System.Drawing.Size(800, 665);
this.MinimumSize = new System.Drawing.Size(800, 665);
this.Name = "Main";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Header";
this.TransparencyKey = System.Drawing.Color.DarkRed;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Main_KeyDown);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Main_MouseDown);
this.tableLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
That is how I insert child into parent's panel:
private void LeftPanel_Paint(object sender, PaintEventArgs e) {
ToC toc = new ToC();
toc.TopLevel = false;
toc.AutoScroll = true;
this.LeftContent.Controls.Add(toc);
toc.FormBorderStyle = FormBorderStyle.None;
toc.Dock = DockStyle.Fill;
toc.Show();
}
Here is my Child:
//
// ToC
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Silver;
this.ClientSize = new System.Drawing.Size(428, 396);
this.Controls.Add(this.Ch7Btn);
this.Controls.Add(this.Ch6Btn);
this.Controls.Add(this.Ch5Btn);
this.Controls.Add(this.Ch4Btn);
this.Controls.Add(this.Ch3Btn);
this.Controls.Add(this.Ch2Btn);
this.Controls.Add(this.Ch1Btn);
this.Controls.Add(this.label1);
this.Name = "ToC";
this.Text = "ToC";
this.TransparencyKey = System.Drawing.Color.Silver;
this.ResumeLayout(false);
Thats the situation when child is Silver inside parent now matter that it has transparency key Silver.
If i set this.BackColor = System.Drawing.Color.DarkRed; to Child - it will pierce parent's background.
Here's some images of what i mean.
Child with other then parent transparency key
Child with same as parent transparency
I am not sure why your child form's background is piercing the main form.
There are two things you might try.
try setting both to a different color which your are most likely not going to use. Like Color.Magenta. (Altough I understand that you might have tried this already)
You should be able to set the Back color to transparent here
Button1.BackColor = Color.Transparent;

Winforms control positioning

I need to add controls programmatically to a custom control and position them in a certain layout. I thought it would be easy enough create a replica at design time inside a panel and then use the generated code to build them in another panel at runtime.
The dimensions such as widths, height & size are not working as expected between runtime vs design time. Why is this?
Eg below has 2 design time panels. The panel on the left contains design time controls and the one on the right runtime controls.
this.dateTimePicker1.Size = new System.Drawing.Size(219, 26); sets the width = 219
However, at runtime dtp2.Size = new System.Drawing.Size(219, 26); is too long and I have to use dtp1.Width = 150; instead. Why 150 and not 219?
RunTime Control Code:
private void BuildControls()
{
//
// dateTimePicker1
//
DateTimePicker dtp1 = new DateTimePicker();
dtp1.Location = new System.Drawing.Point(21, 35);
dtp1.Name = "dateTimePicker1";
//dtp1.Size = new System.Drawing.Size(219, 26);
dtp1.Width = 150; //Not 219 as expected?
dtp1.TabIndex = 1;
panel2.Controls.Add(dtp1);
// dateTimePicker2
//
DateTimePicker dtp2 = new DateTimePicker();
dtp2.Location = new System.Drawing.Point(21, 108);
dtp2.Name = "dateTimePicker2";
dtp2.Size = new System.Drawing.Size(219, 26); //Copying design time is too wide
//dtp1.Width = 150;
dtp2.TabIndex = 2;
panel2.Controls.Add(dtp2);
}
DesignTime Control Code:
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.dateTimePicker1 = new System.Windows.Forms.DateTimePicker();
this.dateTimePicker2 = new System.Windows.Forms.DateTimePicker();
this.label2 = new System.Windows.Forms.Label();
this.panel2 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.dateTimePicker2);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.dateTimePicker1);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(26, 36);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(288, 514);
this.panel1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(17, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(87, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Start Date:";
//
// dateTimePicker1
//
this.dateTimePicker1.Location = new System.Drawing.Point(21, 44);
this.dateTimePicker1.Name = "dateTimePicker1";
this.dateTimePicker1.Size = new System.Drawing.Size(219, 26);
this.dateTimePicker1.TabIndex = 1;
//
// dateTimePicker2
//
this.dateTimePicker2.Location = new System.Drawing.Point(21, 108);
this.dateTimePicker2.Name = "dateTimePicker2";
this.dateTimePicker2.Size = new System.Drawing.Size(219, 26);
this.dateTimePicker2.TabIndex = 3;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(17, 85);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(81, 20);
this.label2.TabIndex = 2;
this.label2.Text = "End Date:";
//
// panel2
//
this.panel2.Location = new System.Drawing.Point(450, 260);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(288, 290);
this.panel2.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(850, 710);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
Your problem is with Autoscaling.
This code:
this.dateTimePicker1.Size = new System.Drawing.Size(219, 26);
May not imply that this.dateTimePicker1.Size is really 219 x 26. Why? Because of this line here, from .designer.cs:
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
After that line an in-memory scaling is performed. This scaling is executed on all child controls right after this line:
this.groupBox1.PerformLayout();
After that, this.dateTimePicker1.Size will change to something closer to 150 in width. You may also notice that designer code does not match what's displayed in the properties pane when control is selected.
Solution, Part 1
Add something to the form to cause a change in the designer file and save it. This will at cause .designer.cs code to match the screen DPI and you won't see any inconsistencies any more. It seems that your DPI setting is higher than the one used when creating the form - if this is accidental, correct your Windows DPI setting to 96 or 100%.
Solution, Part 2
Once your form designer matches your screen DPI, you will see that all the size and location properties have been changed and that there is a new value set on AutoScaleDimensions, write down this value because this is the dimension that matches your screen DPI.
Now, whenever you want your controls' locations and sizes to respect the screen DPI you have to place your control logic in something like this:
// Your referential DPI setting (96DPI in this case)
this.AutoScaleDimensions = new SizeF(6F, 13F);
// TODO: Place your code here
// Setting of your users
this.AutoScaleDimensions = this.CurrentAutoscaleDimensions;
This would cause whatever controls placed in between to be scaled to whatever DPI is currently on the screen.
Note that 6F, 13F are values most people have for 96 DPI setting (default 100% zoom in Windows). This is why I asked to write down your value so you can use that instead.
If you still don't find this obnoxious you can also read up this question which has extra info: Creating a DPI-Aware Application.
Important Note
I forgot to mention something - if you work in a team with source control software, take extra care because whenever you save something in designer it's going to alter every Size and Location to match your own setting (as explained above). This shouldn't cause problems but you should be always aware of this.
try changing
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
to
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;

Align controls to center in a FlowLayout

I have a flowlayout as follows:
I need to center all the controls on the form (In other words, let's say the form's width is 200. btnOpt1 to btnOpt4 should have their Left starting at 100 minus half of the button width, not 0.)
You can do it two ways but with some limitation of each one.
Using Anchor property
Using the layout control with help of Docking and Anchor properties.
Method 1: Anchor Property
Controls are anchored by default to the top left of the form which
means when the form size will be changed, their distance from the top
left side of the form will remain constant. If you change the control
anchor to bottom left, then the control will keep the same distance
from the bottom and left sides of the form when the form if resized.
Turning off the anchor in a direction will keep the control centred in
that direction when resizing.
Example :
public TestForm12()
{
InitializeComponent();
Button btn = new Button();
btn.Width = this.Width - 10;
btn.Height = 20;
btn.Left = (this.ClientSize.Width - btn.Width) / 2;
btn.Top = (this.ClientSize.Height - btn.Height) / 2;
btn.Text = "click me";
this.Controls.Add(btn);
btn.Anchor = AnchorStyles.None;
}
2. Using the layout control
Add TableLayout Control, Set it’s Dock property to Fill.
Add 1 Row with Size Type style Percent 100%
Add 3 Columns Column1(Size Type – Percent(100%)), Column2(Size Type – Absolute(200px)), Column3(Size Type – Percent(100%)).
Now Add Panel Control to Column2 and Set it’s Dock property to Fill
Add Buttons to this control and set their Size as you want and Set Their Anchor Property to AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top
Example - Designer.cs code snippet of the form.
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.tableLayoutPanel1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 3;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.panel1, 1, 0);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 1;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(284, 262);
this.tableLayoutPanel1.TabIndex = 0;
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(45, 3);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(194, 256);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(3, 9);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(188, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// button2
//
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button2.Location = new System.Drawing.Point(3, 38);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(188, 23);
this.button2.TabIndex = 0;
this.button2.Text = "button1";
this.button2.UseVisualStyleBackColor = true;
//
// TestForm11
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "TestForm11";
this.Text = "TestForm11";
this.tableLayoutPanel1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
Hope this help..
I'd go with TableLayoutPanel instead:
Put TableLayoutPanel on your form
Set dock style Fill to panel
Leave only one column inside panel
Create row for every button (and put buttons to table cells)
Set row size type Autosize
Set dock style Fill to every button, except last one
Set dock style Top to last button
BTW in your solution you should iterate over flowLayoutPanel controls instead of form controls. Also consider subtracting horizontal margin and padding from width:
foreach (Control control in flowLayoutPanel.Controls)
{
control.Size = new Size(flowLayoutPanel.Width - control.Margin.Horizontal,
control.Height);
}
But I advise you to use TableLayoutPanel instead.
I solved this by changing the margin values. I am adding my content to a panel though.
C#:
int horizontalMargin = (int)(0.5 * (this.containingPanelOrForm.Width - this.button.Width));
this.btnOptX.Margin = new Padding(horizontalMargin, 0, horizontalMargin, 0);
Or you can use Grid layout instead.
I´m not good in C# but you can also add a panel in flowlayoutpanel with the same width of flowlayoutpanel. Then you can add in the Panel created in running time the button you want and set the dock to left or right. As you wish. Let me show a example in VB.net and C# (used online converts)
VB.net
Dim btn As New Button
btn.Text = "Example"
btn.Name = "Button"
btn.Size = New Size(60,10)
Dim panel As New Panel
panel.Size = New Size(FlowLayoutPanel1.Width, 10) 'size of the flowlayoutpanel + height of button
btn.Dock = DockStyle.Right
FlowLayoutPanel1.Controls.Add(panel)
panel.controls.add(btn)
C#
Button btn = new Button();
btn.Text = "Example";
btn.Name = "Button";
btn.Size = new Size(60, 10);
Panel panel = new Panel();
panel.Size = new Size(FlowLayoutPanel1.Width, 10);
//size of the flowlayoutpanel + height of button
btn.Dock = DockStyle.Right;
FlowLayoutPanel1.Controls.Add(panel);
panel.controls.#add(btn);
Create empty Label with Name = lblEmpty and AutoSize = False. Put this control first in controls list in FlowLayoutPanel1, then add code below.
Example: Assuming three existing labels in FlowLayoutPanel1, the result should be lblEmpty, LabelExisting1, and LabelExisting2, in that order.
Dim MarginLabelEmpty As Integer = ((FlowLayoutPanel1.Width - (LabelExisting1.Width + LabelExisting2.Width)) / 2)
lblEmpty.Width = MarginLabelEmpty
I solved my problem by creating this code.
in your case with Button Controls, create 4 new labels with .Text=""(empty) and put each one at the beginning of each button, naming labels as follows: lblEmpty1, lblEmpty2, lblEmpty3, lblEmpty4.
Then Add the following code:
Dim MarginLeftbtnOptAll As Integer = ((FlowLayoutPanel1.Width - btnOpt1.Width) / 2)
lblEmpty1.AutoSize = False
lblEmpty1.Width = MarginLeftbtnOptAll
lblEmpty2.AutoSize = False
lblEmpty2.Width = MarginLeftbtnOptAll
lblEmpty3.AutoSize = False
lblEmpty3.Width = MarginLeftbtnOptAll
lblEmpty4.AutoSize = False
lblEmpty4.Width = MarginLeftbtnOptAll
This center button, increasing the width of the empty label according to the width of the FlowLayoutPanel1
Throwing the buttons directly to the form or a panel (not FlowLayoutPanel), and setting Anchor = Top (only Top) for all of them, they won't be centered but will always move proportional to the form's (or container's) sides when resizing.
Private Sub FlowLayoutPanel1_SizeChanged(sender As Object, e As EventArgs)` Handles FlowLayoutPanel1.SizeChanged
Dim TotalWidth As Integer = FlowLayoutPanel1.Controls.Count * button.Width
Dim LeftPadding As Integer = (FlowLayoutPanel1.Width - TotalWidth) / 2
Dim GapPadding As Integer = FlowLayoutPanel1.Controls.Count * 5
If TotalWidth <= FlowLayoutPanel1.Width Then
FlowLayoutPanel1.Padding = New Padding(LeftPadding - GapPadding, 0, 0, 0)
End If
End Sub

Categories