Winforms Dynamic size - c#

I have ma de a winform, that contains a tabControl (3 tabs, maybe more later).
In 2 of my tabs, I got a listBoxView.
The problem is, when I click on fullSize button, the tabControl doesn't change his size. That makes an awful window.
How can I define dynamic size of my tabControl, based on the winforms border size, and a dynamic size of my listBoxView based on tabControl size?
TabControl must adapt to form size, then pages in tabControl must adapt to tabControl size, and then, listBox in the pages must adapt to its pages size.
Here is the form :
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1313, 614);
this.Controls.Add(this.tabControl1);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.dgCSV)).EndInit();
this.tabPage2.ResumeLayout(false);
this.tabPage2.PerformLayout();
this.ResumeLayout(false);
And the tabControl with one of his pages :
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Location = new System.Drawing.Point(13, 13);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1288, 589);
this.tabControl1.TabIndex = 0;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.listBoxFiles);
this.tabPage2.Controls.Add(this.richTextBox1);
this.tabPage2.Controls.Add(this.buttonBottom);
this.tabPage2.Controls.Add(this.buttonFront);
this.tabPage2.Controls.Add(this.buttonDown);
this.tabPage2.Controls.Add(this.buttonUp);
this.tabPage2.Controls.Add(this.label2);
this.tabPage2.Location = new System.Drawing.Point(4, 25);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(1280, 560);
this.tabPage2.TabIndex = 1;
this.tabPage2.UseVisualStyleBackColor = true;
I tried with Parent.width, ClientRectangle, ClientSize.
I'm lost with all this properties and no one is successfull...
After trippino answer :
Indeed, a dock on the tabControl will resize him, but I can't do it on a listBoxView, because one of the element will just take all the page.
And Anchor do not resize elements, it will juste reorganise them to fit in the page.
Still not resizing like this :
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1313, 614);
this.tabControl1.TabIndex = 0;
//
// listBoxFiles
//
this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
this.listBoxFiles.Anchor = System.Windows.Forms.AnchorStyles.Left;
this.listBoxFiles.FormattingEnabled = true;
this.listBoxFiles.ItemHeight = 16;
this.listBoxFiles.Location = new System.Drawing.Point(185, 43);
this.listBoxFiles.Name = "listBoxFiles";
this.listBoxFiles.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.listBoxFiles.Size = new System.Drawing.Size(1040, 244);
this.listBoxFiles.TabIndex = 42;
private void tabPage2_SizeChanged(object sender, EventArgs e)
{
this.buttonAucun.Location = new System.Drawing.Point(this.buttonAucun.Location.X, this.listBoxFiles.Location.Y + this.listBoxFiles.Height + 10);
this.progressBar1.Location = new System.Drawing.Point(this.progressBar1.Location.X, this.buttonAucun.Location.Y + this.buttonAucun.Height + 10);
this.richTextBox1.Location = new System.Drawing.Point(this.richTextBox1.Location.X, this.progressBar1.Location.Y + this.progressBar1.Height + 10);
this.buttonEnregistrer.Location = new System.Drawing.Point(this.buttonEnregistrer.Location.X, this.richTextBox1.Location.Y + this.richTextBox1.Height + 10);
}
Since, buttonEnregistrer is on the bottom of my tabPage, I also tried :
this.buttonEnregistrer.Location = new System.Drawing.Point(this.buttonEnregistrer.Location.X, this.tabPage2.Height -50);
But his will not retake it's original location.
Thank you.

Just use the Dock property of the tabControl setting it to Fill. It should solve your issue.
MSDN Dock Property reference
EDIT AFTER DISCUSSION: to anchor 4 sides you have to use :
this.listBoxFiles.Anchor = ((System.Windows.Forms.AnchorStyles
((((System.Windows.Forms.AnchorStyles.Top
| System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));

Related

Horizontal Scrolling not working with autoscroll in C#

I am trying to set my panel so it will scroll horizontally. I have tried:
MainPanel.HorizontalScroll.Enabled = true;
MainPanel.HorizontalScroll.Visible = true;
MainPanel.VerticalScroll.Enabled = false;
MainPanel.VerticalScroll.Visible = false;
My MainPanel is a parent to 2 sub Panels that are Horizontaly larger than the Panel itself but the horizontal scroll bar still doesn't appear. How can I fix this?
This is the code in the .Designer.cs file of the Parent Form of my Panels:
this.panel0.Dock = System.Windows.Forms.DockStyle.Top;
this.panel0.Location = new System.Drawing.Point(0, 0);
this.panel0.Name = "panel0";
this.panel0.Size = new System.Drawing.Size(828, 28);
this.panel0.TabIndex = 2;
//
// list
//
this.list.Dock = System.Windows.Forms.DockStyle.Fill;
this.list.Location = new System.Drawing.Point(0, 28);
this.list.Name = "list";
this.list.Size = new System.Drawing.Size(828, 444);
this.list.TabIndex = 3;
//
// MainPanel
//
this.MainPanel.AutoScroll = true;
this.MainPanel.Controls.Add(this.list);
this.MainPanel.Controls.Add(this.panel0);
this.MainPanel.Dock = System.Windows.Forms.DockStyle.Fill;
this.MainPanel.Location = new System.Drawing.Point(152, 104);
this.MainPanel.Name = "MainPanel";
this.MainPanel.Size = new System.Drawing.Size(828, 472);
this.MainPanel.TabIndex = 7;

Layout control using FlowLayoutPanel

I want put a bunch of buttons at the top section of my form, and a SplitContainer right below these buttons. All these are done in the code (not thru the designer), so I can manage the number and text of the buttons dynamically. I reckon it is handy to put the button into a FlowLayoutPanel, so they can response with the flow feature when the form gets resized.
My question is how do I set up the SplitContainer layout settings (size, location, anchor, docking etc.), to make sure no matter how I resize the form, the SplitContainer is right below the buttons (no gap), and it fills up the rest of the form. Below is what I've done.
this.Size = new Size(300, 400);
string[] tags = new string[] { "action", "romance", "drama", "sci-fi", "horror" };
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.Size = new Size(this.Width, 50);
flp.Location = new Point(0, 0);
flp.AutoSize = true;
flp.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
flp.FlowDirection = FlowDirection.LeftToRight;
this.Controls.Add(flp);
//flp.Resize += flp_Resize;
foreach (string tag in tags)
{
Button btn = new Button();
btn.Text = tag;
flp.Controls.Add(btn);
}
SplitContainer sc = new SplitContainer();
ListBox lb = new ListBox(); lb.Dock = DockStyle.Fill; lb.Parent = sc.Panel1; lb.Items.Add("test");
PictureBox pb = new PictureBox(); pb.Dock = DockStyle.Fill; pb.Parent = sc.Panel2; pb.BackColor = Color.LightBlue;
sc.Size = new Size(this.Width, this.Height-flp.Height);
sc.Location = new Point(0, flp.Height);
sc.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
this.Controls.Add(sc);
Here is the solution. Thanks to #Fabio.
this.Size = new Size(300, 400);
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.RowCount = 2;
tlp.ColumnCount = 1;
tlp.Dock = DockStyle.Fill;
this.Controls.Add(tlp);
string[] tags = new string[] { "action", "romance", "drama", "sci-fi", "horror" };
FlowLayoutPanel flp = new FlowLayoutPanel();
flp.AutoSize = true;
flp.Dock = DockStyle.Fill;
flp.BackColor = Color.LightCyan;
flp.FlowDirection = FlowDirection.LeftToRight;
tlp.Controls.Add(flp, 0, 0);
foreach (string tag in tags)
{
Button btn = new Button();
btn.Text = tag;
flp.Controls.Add(btn);
}
SplitContainer sc = new SplitContainer();
ListBox lb = new ListBox(); lb.Dock = DockStyle.Fill; lb.Parent = sc.Panel1; lb.Items.Add("test");
PictureBox pb = new PictureBox(); pb.Dock = DockStyle.Fill; pb.Parent = sc.Panel2; pb.BackColor = Color.LightBlue;
sc.Dock = DockStyle.Fill;
tlp.Controls.Add(sc,0,1);

Why flowlayoutPanel is extending horizontally?

I have set this flowLayoutPanel, the controls inside arrange well, till the last arrives to the bottom border of the panel, then the controls start arranging on the right side (forming another column) keepping the vertical flow. I just want one column.
this.panel.Anchor =
((System.Windows.Forms.AnchorStyles)
(((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)| System.Windows.Forms.AnchorStyles.Right)));
this.panel.AutoScroll = true;
this.panel.BorderStyle = BorderStyle.None;
this.panel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
this.panel.Location = new System.Drawing.Point(0, 184);
this.panel.Name = "myPanel";
this.panel.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.panel.Size = new System.Drawing.Size(300, 371);
this.panel.TabIndex = 9;
Use
this.panel.FlowDirection = System.Windows.Forms.FlowDirection.LeftToRight;
instead of
this.panel.FlowDirection = System.Windows.Forms.FlowDirection.TopDown;
if you want only one column than please add below code to your application just after control added to your flowlayoutpanel
this.panel.SetFlowBreak(<<YOUR_ADDED_CONTROL_NAME>>, true);
Example
Button btn1 = new Button();
btn1.Text = "TEST";
btn1.Height = 30;
btn1.Width = 100;
this.panel.Controls.Add(btn1);
this.panel.SetFlowBreak(btn1, true);

Drawing dynamic panels C#

Good afternoon.
There was a problem with the rendering of dynamically created panel, all panels initially were 100px and I their renders (y + = 100), but now the production has changed a bit, these panels can be of different sizes, and the distance between them actually remains unchanged 100rh ...
Pliz tell me how to make, that they are somehow drawn at equal (10px) distances from each other. Read that you can somehow make a method SetBounds, but did not understand.
http://pastebin.com/TnSuFTti
for (int i = data_list.Count - 1; i >= 0; i--)
{
Panel panel = new Panel(); //создание блока сообщения и наложение картинки
panel.Name = i.ToString();
panel.MouseEnter += new EventHandler(panel_MouseEnter);
Label textBox_name = new Label();
Label textBox_date = new Label();
Label textBox_msg = new Label();
panel.Width = 308;
Bitmap btm_msg = new Bitmap(
Properties.Resources.NotificationCenterWindow_msg_box);
panel.BackgroundImage = btm_msg;
panel.BackgroundImageLayout = ImageLayout.Stretch;
panel.Location = new Point(5, 5);
panel.Controls.Add(textBox_date);
textBox_date.Name = "textBox_date" + i.ToString();
textBox_date.Location = new Point(232, 8);
textBox_date.Size = new System.Drawing.Size(70, 15);
textBox_date.BorderStyle = BorderStyle.None;
textBox_date.MinimumSize = new System.Drawing.Size(72, 15);
textBox_date.TextAlign = ContentAlignment.MiddleRight;
textBox_date.BackColor = Color.DarkGray;
textBox_date.Anchor = AnchorStyles.Right | AnchorStyles.Top;
textBox_date.ForeColor = SystemColors.ScrollBar;
panel.Controls.Add(textBox_name);
textBox_name.Size = new System.Drawing.Size(100, 15);
textBox_name.MinimumSize = new System.Drawing.Size(100, 15);
textBox_name.Location = new Point(5, 8);
textBox_name.BorderStyle = BorderStyle.None;
textBox_name.BackColor = Color.DarkGray;
textBox_name.ForeColor = SystemColors.ScrollBar;
panel.Height = textBox_name.Height + 19;
panel1.Controls.Add(panel);
panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
y += 100;
}
panel.Location = new Point(5, y);
//.....
y = panel.Bottom + 10;
Just set the position based on the previous panel...
// This will put the panel to the right of an existing one
panel_b.Left = panel_a.Left + panel_a.Width + 10;
.
// This will put the panel below an existing one
panel_b.Top= panel_a.Top+ panel_a.Height+ 10;

C# Buttons not repositioning themselves

I have included 4 buttons inside a panel. The panel is docked to the main window.
When I resize the main window, it doesn't reposition the 4 buttons with respect to the newly modified window size. I am using VS 2010 Designer view to accomplish this.
Here is the entire code generated from designer.cs
private void InitializeComponent()
{
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.TreeDDC = new System.Windows.Forms.TreeView();
this.BtnPointCtrl = new System.Windows.Forms.Button();
this.BtnLogic = new System.Windows.Forms.Button();
this.BtnComm = new System.Windows.Forms.Button();
this.BtnSystem = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.statusStrip1.SuspendLayout();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1});
this.statusStrip1.Location = new System.Drawing.Point(0, 445);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(639, 22);
this.statusStrip1.TabIndex = 0;
this.statusStrip1.Text = "statusBar";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(61, 17);
this.toolStripStatusLabel1.Text = "Status Bar";
//
// TreeDDC
//
this.TreeDDC.Dock = System.Windows.Forms.DockStyle.Left;
this.TreeDDC.Location = new System.Drawing.Point(0, 0);
this.TreeDDC.Name = "TreeDDC";
this.TreeDDC.Size = new System.Drawing.Size(97, 445);
this.TreeDDC.TabIndex = 1;
//
// BtnPointCtrl
//
this.BtnPointCtrl.Location = new System.Drawing.Point(28, 93);
this.BtnPointCtrl.Name = "BtnPointCtrl";
this.BtnPointCtrl.Size = new System.Drawing.Size(202, 86);
this.BtnPointCtrl.TabIndex = 2;
this.BtnPointCtrl.Text = "???";
this.BtnPointCtrl.UseVisualStyleBackColor = true;
//
// BtnLogic
//
this.BtnLogic.Location = new System.Drawing.Point(28, 282);
this.BtnLogic.Name = "BtnLogic";
this.BtnLogic.Size = new System.Drawing.Size(202, 86);
this.BtnLogic.TabIndex = 4;
this.BtnLogic.Text = "??";
this.BtnLogic.UseVisualStyleBackColor = true;
//
// BtnComm
//
this.BtnComm.Location = new System.Drawing.Point(307, 93);
this.BtnComm.Name = "BtnComm";
this.BtnComm.Size = new System.Drawing.Size(202, 86);
this.BtnComm.TabIndex = 3;
this.BtnComm.Text = "??";
this.BtnComm.UseVisualStyleBackColor = true;
//
// BtnSystem
//
this.BtnSystem.Location = new System.Drawing.Point(307, 282);
this.BtnSystem.Name = "BtnSystem";
this.BtnSystem.Size = new System.Drawing.Size(202, 86);
this.BtnSystem.TabIndex = 5;
this.BtnSystem.Text = "???";
this.BtnSystem.UseVisualStyleBackColor = true;
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.BtnSystem);
this.panel1.Controls.Add(this.BtnComm);
this.panel1.Controls.Add(this.BtnPointCtrl);
this.panel1.Controls.Add(this.BtnLogic);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(97, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(542, 445);
this.panel1.TabIndex = 6;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(639, 467);
this.Controls.Add(this.panel1);
this.Controls.Add(this.TreeDDC);
this.Controls.Add(this.statusStrip1);
this.Name = "MainForm";
this.Text = "MainForm";
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
But I believe the only code of interest will be the following:
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Controls.Add(this.BtnSystem);
this.panel1.Controls.Add(this.BtnComm);
this.panel1.Controls.Add(this.BtnPointCtrl);
this.panel1.Controls.Add(this.BtnLogic);
Any help will be greatly appreciated.
Use the Anchor property of the buttons...
Read it Working with Anchoring and Docking
The Panel does not itself repositioning the controls when it resizing, Use the Anchor (Top, Bottom, Left, Right) property that the controls reposition when parent Panel resizes...
Or Use TableLayOutPanel.
Docking the panel will only resize the panel iteelf, not reposition the controls within the panel. To have elements within a container change their position after resizing the container, look into the Anchor property. There is also TableLayoutPanel which can, as its name suggests, layout controls in a table format.
Please Dock a FlowLayoutPanel inside your panel and palce the buttons inside that FlowLayoutPanel. Set the Layout Direction property. Now when you resize the main window your buttons will also be resized.

Categories

Resources