Consider System.Windows.Forms.StatusStrip. I have added a StatusStrip to my Windows Forms application, but I am having a few problems.
I would like to have a label anchored at the left and a progressbar anchored on the right in the StatusStrip, but I can't find a way to set these properties.
I then thought that I may need to create two StatusStrips and anchor them on either side of the bottom of the form... That didn't pan out; besides that, it just doesn't feel right.
Just set the Spring property on the label control to True and you should be good to go.
What you have to do is set the alignment property of your progressbar to right. Then set the LayoutStyle of the StatusStrip to HorizontalStackWithOverflow.
private void InitializeComponent()
{
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripProgressBar1 = new System.Windows.Forms.ToolStripProgressBar();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripStatusLabel1,
this.toolStripProgressBar1});
this.statusStrip1.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.HorizontalStackWithOverflow;
this.statusStrip1.Location = new System.Drawing.Point(0, 250);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(467, 22);
this.statusStrip1.TabIndex = 0;
this.statusStrip1.Text = "statusStrip1";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(117, 17);
this.toolStripStatusLabel1.Text = "toolStripStatusLabel1";
//
// toolStripProgressBar1
//
this.toolStripProgressBar1.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
this.toolStripProgressBar1.Name = "toolStripProgressBar1";
this.toolStripProgressBar1.Size = new System.Drawing.Size(100, 16);
}
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.ToolStripProgressBar toolStripProgressBar1;
This can be achieved with the default table layout for the statusStrip by simply putting another label between your current label and your progressBar and set the Spring property to true.
Did you tried the Alignment property of ProgresBarToolStripItem set to the Right?
Open your Designer and set
this.toolStripStatusLabel1.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
Related
I am working on a homework c# winforms project and would like to add date and time in top right corner of my main form in a way that in first row I have a date written in one label, and on second row I have time written in second label.
Also I need that those stick in the top right corner if form is resized.
I don't know if it matters, but those label controls are inside panel which is top docked in form, and this panel already contains two controls that are docked left.
example of what I want
I've been playing with anchor and dock properties but I can't get it to work in a way I want.
private void GlavnaForma_Load(object sender, EventArgs e)
{
timerDateTime.Start();
lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}
private void timerDateTime_Tick(object sender, EventArgs e)
{
lblDate.Text = DateTime.Now.ToString("dddd, dd.M.yyyy", new CultureInfo("hr-HR"));
lblTime.Text = DateTime.Now.ToString("HH:mm:ss", new CultureInfo("hr-HR"));
}
Set the anchor to Top, Right like so:
There are several ways to do this.
I would probably make the main form have a table layout panel with one column and three rows. Make the top two rows be absolutely sized and the third row have size type "percent" with a value of 100.0% to take up all remaining room. Then put a label each in the top two rows and justify the labels to the right via setting their "Dock" property to "Right".
All of this can be done in the form designer GUI. The generated code looks like the following:
this.tableLayout = new System.Windows.Forms.TableLayoutPanel();
this.labelDate = new System.Windows.Forms.Label();
this.labelTime = new System.Windows.Forms.Label();
this.tableLayout.SuspendLayout();
this.SuspendLayout();
//
// tableLayout
//
this.tableLayout.ColumnCount = 1;
this.tableLayout.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.Controls.Add(this.labelDate, 0, 0);
this.tableLayout.Controls.Add(this.labelTime, 0, 1);
this.tableLayout.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayout.Location = new System.Drawing.Point(0, 0);
this.tableLayout.Name = "tableLayout";
this.tableLayout.RowCount = 3;
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 24F));
this.tableLayout.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayout.Size = new System.Drawing.Size(800, 450);
this.tableLayout.TabIndex = 0;
//
// labelDate
//
this.labelDate.AutoSize = true;
this.labelDate.Dock = System.Windows.Forms.DockStyle.Right;
this.labelDate.Location = new System.Drawing.Point(742, 0);
this.labelDate.Name = "labelDate";
this.labelDate.Size = new System.Drawing.Size(55, 24);
this.labelDate.TabIndex = 0;
this.labelDate.Text = "26-8-2019";
//
// labelTime
//
this.labelTime.AutoSize = true;
this.labelTime.Dock = System.Windows.Forms.DockStyle.Right;
this.labelTime.Location = new System.Drawing.Point(748, 24);
this.labelTime.Name = "labelTime";
this.labelTime.Size = new System.Drawing.Size(49, 24);
this.labelTime.TabIndex = 1;
this.labelTime.Text = "19:59:58";
Add whatever further content you want to the third row. Maybe add a panel to that row docked to "Fill"
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);
I have made a user control, containing 2 simple controls in it: A checkbox and a combobox. (and some copies of it that contain a checkbox and a textbox, or a checkbox and an IBAN control vs...)
When I use this user control in designer mode, changing the size of user control does not change the size of the inner controls naturally. I have to set the sizes of them in the page that I use the user control in the actual class but designer class. My aim is to let the width of those controls to be changed only by changing the width of the user control. What I mean is:
Let's call our control ucControl, and its inner controls cbCheckBox and cmbComboBox. When I create this user control, I set a static size to all of those controls, and except the size of ucControl, sizes of the rest are not available for size changing from designer.
I want the size of the cmbComboBox to change when the size of the ucControl changes, according to a formula like:
cmbComboBox.Size = new Size(ucControl.Size.Width - cbCheckBox.Size.Width - 15, 20)
How and where should I do it?
What I tried so far:
I tried to use SizeChanged event but it didn't work. (It did not let me to create a void returning event method in the user control, no idea why.)
I tried to set it in the load method, it didn't work.
I tried to set it in the InitializeComponent method in the design class, it didn't work.
The best way to approach this is to use containers and make the control Dock with the Fill option. This way it will resize dynamically for you. You could also anchor it to left and right but I find container to be much more elegant option. The sample below uses a simple TableLayoutPanel with some rows and columns fixed.
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.label1 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 120F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanel1.Controls.Add(this.label2, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.checkBox1, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.textBox1, 1, 1);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 3;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 26F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 26F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle());
this.tableLayoutPanel1.Size = new System.Drawing.Size(412, 198);
this.tableLayoutPanel1.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Dock = System.Windows.Forms.DockStyle.Right;
this.label1.Location = new System.Drawing.Point(62, 3);
this.label1.Margin = new System.Windows.Forms.Padding(3);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Checkbox";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Dock = System.Windows.Forms.DockStyle.Left;
this.checkBox1.Location = new System.Drawing.Point(123, 3);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 20);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Dock = System.Windows.Forms.DockStyle.Right;
this.label2.Location = new System.Drawing.Point(71, 29);
this.label2.Margin = new System.Windows.Forms.Padding(3);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(46, 20);
this.label2.TabIndex = 2;
this.label2.Text = "TextBox";
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(123, 29);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(286, 20);
this.textBox1.TabIndex = 3;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(412, 198);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Form1";
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.TextBox textBox1;
}
Simply anchoring the ComboBox to the Left and Right should achieve what you want.
Here is the UserControl right after the ComboBox was added to it:
Select the ComboBox and drag its Right Edge until it is your desired distance from the Right Edge of the UserControl:
Change the Anchor Property of the ComboBox and turn on the Right Anchor so that you have both the Left and the Right turned on:
Now try resizing the UserControl and see what happens to the ComboBox.
I have one textbox (textBox1) and panel (Panel1) I have code like this
Panel1.Controls.Add(textBox1)
so when I run it I can't see textbox anymore, If I do like this I can see textBox
textBox1.Location = Panel1.Location
can anyone tell me what's problem?
When a textbox (or any control) is part of a panel the top left of the panel is point(0.0);
so when textBox1.Location = Panel1.Location the textbox probably falls out of view in the panel.
try something like this instead/
//
// panel1
//
this.panel1.Controls.Add(this.textBox1);
this.panel1.Location = new System.Drawing.Point(59, 27);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(193, 176);
this.panel1.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
I believe the reason you're not able to see the Textbox has to do with the Panel's properties. Try setting the AutoSize property to true and the AutoSizeMode property to GrowAndShrink.
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.