Windows form control resize when change language - c#

Good morning.
I have a user control with a tablelayout panel inside, docked in left, right.
when i set the localizable property to true and i change the language (using vs2013 designer) the tablelayout panel grow out of the control (with a very big size).
i have done many test and i noticed that the size change whenever the control is docked in left, right, no matter if is a tablelayoutpanel or another control.
Can anyone help me to solve this?
Or maybe suggest me a more stable way to localize my sw?
Here is the designer of the control before set localizable to true
partial class LineAndTitlePanel
{
/// <summary>
/// Variabile di progettazione necessaria.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Pulire le risorse in uso.
/// </summary>
/// <param name="disposing">ha valore true se le risorse gestite devono essere eliminate, false in caso contrario.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Codice generato da Progettazione componenti
/// <summary>
/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
/// il contenuto del metodo con l'editor di codice.
/// </summary>
private void InitializeComponent()
{
this.tableLayoutPanelFinalContainerHeader = new System.Windows.Forms.TableLayoutPanel();
this.lineSeparator = new System.Windows.Forms.Panel();
this.labelTitle = new System.Windows.Forms.Label();
this.tableLayoutPanelFinalContainerHeader.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanelFinalContainerHeader
//
this.tableLayoutPanelFinalContainerHeader.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tableLayoutPanelFinalContainerHeader.BackColor = System.Drawing.Color.Transparent;
this.tableLayoutPanelFinalContainerHeader.ColumnCount = 2;
this.tableLayoutPanelFinalContainerHeader.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
this.tableLayoutPanelFinalContainerHeader.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanelFinalContainerHeader.Controls.Add(this.lineSeparator, 1, 0);
this.tableLayoutPanelFinalContainerHeader.Controls.Add(this.labelTitle, 0, 0);
this.tableLayoutPanelFinalContainerHeader.Location = new System.Drawing.Point(0, 17);
this.tableLayoutPanelFinalContainerHeader.Margin = new System.Windows.Forms.Padding(0);
this.tableLayoutPanelFinalContainerHeader.Name = "tableLayoutPanelFinalContainerHeader";
this.tableLayoutPanelFinalContainerHeader.RowCount = 1;
this.tableLayoutPanelFinalContainerHeader.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanelFinalContainerHeader.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 14F));
this.tableLayoutPanelFinalContainerHeader.Size = new System.Drawing.Size(430, 14);
this.tableLayoutPanelFinalContainerHeader.TabIndex = 126;
//
// lineSeparator
//
this.lineSeparator.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lineSeparator.BackColor = System.Drawing.Color.Transparent;
this.lineSeparator.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(12)))), ((int)(((byte)(163)))), ((int)(((byte)(176)))));
this.lineSeparator.Location = new System.Drawing.Point(33, 12);
this.lineSeparator.Margin = new System.Windows.Forms.Padding(0);
this.lineSeparator.Name = "lineSeparator";
this.lineSeparator.Size = new System.Drawing.Size(397, 2);
this.lineSeparator.TabIndex = 0;
//
// labelTitle
//
this.labelTitle.AutoSize = true;
this.labelTitle.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelTitle.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(12)))), ((int)(((byte)(163)))), ((int)(((byte)(176)))));
this.labelTitle.Location = new System.Drawing.Point(0, 0);
this.labelTitle.Margin = new System.Windows.Forms.Padding(0);
this.labelTitle.Name = "labelTitle";
this.labelTitle.Size = new System.Drawing.Size(33, 14);
this.labelTitle.TabIndex = 0;
this.labelTitle.Text = "Title";
this.labelTitle.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// LineAndTitlePanel
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.AutoSize = true;
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.tableLayoutPanelFinalContainerHeader);
this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Margin = new System.Windows.Forms.Padding(0);
this.Name = "LineAndTitlePanel";
this.Padding = new System.Windows.Forms.Padding(0, 0, 0, 5);
this.Size = new System.Drawing.Size(430, 36);
this.tableLayoutPanelFinalContainerHeader.ResumeLayout(false);
this.tableLayoutPanelFinalContainerHeader.PerformLayout();
this.ResumeLayout(false);
}
#endregion
protected System.Windows.Forms.TableLayoutPanel tableLayoutPanelFinalContainerHeader;
private Panel lineSeparator;
protected System.Windows.Forms.Label labelTitle;
}

Related

Winform UserControl update while not visible

I have a tabControl in a Winform application. The user is able to change settings that will impact each tab visually, and I need to get each tabs as images.
I'm trying to understand why/how some controls are updating even if not visible and some other don't.
Is there a way to force an existing UserControl to update/redraw even if not visible so that Control.DrawToBitmap(...) gets a valid image?
I created an example with a Chart and a FlowLayoutPanel to explain what I mean and to make sure I could reproduce the issue I have.
In the example below:
The image created from the FlowLayoutPanel is only correct if it was updated and visible before DrawToBitmap is called.
The image from the Chart is alway up to date even if updated when not visible.
Questions:
How does it work with one control and not the other?
How could I ensure the FlowLayoutPanel has the same behavior as the Chart?
(EDIT) I have tried a few things without success:
Suspend/Resume layout when updating
call Refresh / Update / Invalidate on the layout that do not update as expected
Here is a gif showing that the chart get updated even when not visible but not the FlowLayoutPanel:
Here is the code of this example:
using LiveChartsCore.SkiaSharpView;
using System.Collections.ObjectModel;
namespace WinFormsDrawToBitmapTest
{
public partial class Form1 : Form
{
int legendCount = 1;
ViewModel viewModel;
public Form1()
{
InitializeComponent();
viewModel = new ViewModel();
cartesianChart1.Series = viewModel.Series;
updateLegend(this.flowLayoutPanel1);
}
private async void button1_Click(object sender, EventArgs e)
{
Bitmap bitmap = await Task.Run(() => getImg3());
this.pictureBox1.Image = bitmap;
this.pictureBox1.Invoke((MethodInvoker)delegate
{ this.pictureBox1.Image = bitmap; });
}
private Bitmap getImg3()
{
Bitmap bitmap = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height + this.flowLayoutPanel1.Height);
Bitmap chart = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height);
Bitmap legend = new Bitmap(this.flowLayoutPanel1.Width, this.flowLayoutPanel1.Height);
this.cartesianChart1.Invoke((MethodInvoker)delegate
{
this.cartesianChart1.DrawToBitmap(chart, new Rectangle(new Point(0, 0), this.cartesianChart1.Size));
});
this.flowLayoutPanel1.Invoke((MethodInvoker)delegate
{
this.flowLayoutPanel1.DrawToBitmap(legend, new Rectangle(new Point(0, 0), this.flowLayoutPanel1.Size));
});
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(legend, 0, 0);
g.DrawImage(chart, 0, legend.Height);
}
return bitmap;
}
private void button2_Click(object sender, EventArgs e)
{
Random r = new Random();
viewModel.Data.Add(r.Next(0, 10));
legendCount++;
updateLegend(this.flowLayoutPanel1);
}
private void updateLegend(FlowLayoutPanel flow)
{
flow.Controls.Clear();
Color[] colorList = new Color[]
{
Color.FromArgb(15, 1, 215),
Color.FromArgb(255, 0, 0),
Color.FromArgb(0, 176, 80),
Color.FromArgb(112, 48, 160)
};
for (int i = 0; i < legendCount; i++)
{
//flow.Controls.Add(new LegendLabel($"Label {i}", colorList[i % colorList.Length]));
Button btn = new Button();
btn.Name = $"Button {i}";
btn.TabIndex = 0;
btn.Text = $"Button {i}";
flow.Controls.Add(btn);
}
}
}
public partial class ViewModel
{
public ObservableCollection<double> Data { get; set; }
public List<LineSeries<double>> Series { get; set; }
public ViewModel()
{
Data = new ObservableCollection<double>();
Data.Add(1); Data.Add(2); Data.Add(5); Data.Add(4);
Series = new List<LineSeries<double>>();
Series.Add(new LineSeries<double> { Values = Data, Fill = null });
}
}
}
With the associated .Designer.cs in case it helps
namespace WinFormsDrawToBitmapTest
{
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.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.cartesianChart1 = new LiveChartsCore.SkiaSharpView.WinForms.CartesianChart();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel2.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// panel2
//
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.button1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(1042, 24);
this.panel2.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(167, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(86, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Change Tab1";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(161, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Get Tab1 as Image in Tab 2";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 24);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1042, 585);
this.tabControl1.TabIndex = 2;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.cartesianChart1);
this.tabPage1.Controls.Add(this.flowLayoutPanel1);
this.tabPage1.Location = new System.Drawing.Point(4, 24);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(1034, 557);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// cartesianChart1
//
this.cartesianChart1.Location = new System.Drawing.Point(8, 116);
this.cartesianChart1.Name = "cartesianChart1";
this.cartesianChart1.Size = new System.Drawing.Size(1018, 435);
this.cartesianChart1.TabIndex = 4;
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Location = new System.Drawing.Point(8, 6);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(1018, 100);
this.flowLayoutPanel1.TabIndex = 3;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.pictureBox1);
this.tabPage2.Location = new System.Drawing.Point(4, 24);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(1034, 557);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1028, 551);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1042, 609);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.panel2);
this.Name = "Form1";
this.Text = "Form1";
this.panel2.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Panel panel2;
private Button button1;
private TabControl tabControl1;
private TabPage tabPage1;
private TabPage tabPage2;
private PictureBox pictureBox1;
private FlowLayoutPanel flowLayoutPanel1;
private LiveChartsCore.SkiaSharpView.WinForms.CartesianChart cartesianChart1;
private Button button2;
}
}
Thank you for your help with this.

How to centre align the content of windows form application?

I am developing a simple windows form application. But I am not able to center align the contents of the form. here are the images:
Here's the code of the form:
namespace WindowsFormsApplication1
{
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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(1, 1);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.button1.FlatAppearance.BorderColor = System.Drawing.SystemColors.Highlight;
this.button1.FlatAppearance.BorderSize = 5;
this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.HotTrack;
this.button1.ForeColor = System.Drawing.Color.Coral;
this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
this.button1.Location = new System.Drawing.Point(330, 275);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(97, 123);
this.button1.TabIndex = 1;
this.button1.UseMnemonic = false;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Enabled = false;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.ImageLocation = "";
this.pictureBox1.Location = new System.Drawing.Point(270, 27);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(221, 176);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("OCR A Extended", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(251, 226);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(251, 20);
this.label1.TabIndex = 3;
this.label1.Text = "Please Touch Your Card";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(359, 433);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Highlight;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(752, 502);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.PictureBox pictureBox1;
}
}
I tried using this,
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
But even that did not help.
I suggest you try the TableLayoutPanel, if you add one you shall get a small 2x2 grid. If you right click and say edit rows and columns you can add a third column and the amount of rows you need. for the settings of the panel I'd suggest something along these lines:
column1 : 50%
column2 : autosize
column3 : 50%
This will make it so the middle column is the minimal required size for your controls, and the remainder of the width is divided over the other 2 columns. Don't forget to dock the tableLayoutPanel to the form.
Thought I'd post it as an awnser instead of the comment I left before.
One way to do is to use TableLayoutPanel as #maam27 commented. Another way is to use the Anchor property of the control. By default a control is anchored to the top-left corner of the parent form. So when form resizes, that point remains fixed. You may choose to break the left anchor. Manually center-align your components in designer. After that if your form resizes, then the relative position will remain unchanged.
you take a DIV than assign align inline property for label

How to set the size of an inner control of a user control dynamically

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.

How do I add a Control to a Container in an inherited WinForm

I have a base form in another project that enforces the same look & feel to all our WinForms programs. I inherited from that BaseForm, to create my own template, BaseView. This template of mine has additional controls like a ProgressBar, a Timer and a TableLayoutPanel.
I now want to inherit from my BaseView and use the Designer to add my program-specific controls, however I cannot drop anything like Panels or any other Controls into it. I have tried some suggestions, like making sure the base form's components are public, but to no avail - most of the TLP's properties are still grayed out.
Could someone perhaps give me any suggestions? Many Thanks!
partial class BaseView
{
/// <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.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseView));
this.tableLayoutPanel_Form = new System.Windows.Forms.TableLayoutPanel();
this.panel_Buttons = new System.Windows.Forms.Panel();
this.panel_MainBody = new System.Windows.Forms.Panel();
this.progressBar = new System.Windows.Forms.ProgressBar();
this.timer_ProgressBar = new System.Windows.Forms.Timer(this.components);
this.tableLayoutPanel_Form.SuspendLayout();
this.SuspendLayout();
//
// lblFormId
//
this.lblFormId.AutoSize = false;
this.lblFormId.Location = new System.Drawing.Point(390, 9);
this.lblFormId.Size = new System.Drawing.Size(98, 13);
this.lblFormId.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// btnClose
//
this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnClose.Location = new System.Drawing.Point(447, 5);
this.btnClose.Size = new System.Drawing.Size(41, 21);
this.btnClose.TabIndex = 4;
//
// tableLayoutPanel_Form
//
this.tableLayoutPanel_Form.ColumnCount = 1;
this.tableLayoutPanel_Form.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel_Form.Controls.Add(this.panel_Buttons, 0, 2);
this.tableLayoutPanel_Form.Controls.Add(this.panel_MainBody, 0, 1);
this.tableLayoutPanel_Form.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel_Form.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel_Form.Name = "tableLayoutPanel_Form";
this.tableLayoutPanel_Form.RowCount = 3;
this.tableLayoutPanel_Form.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
this.tableLayoutPanel_Form.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
this.tableLayoutPanel_Form.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 30F));
this.tableLayoutPanel_Form.Size = new System.Drawing.Size(496, 322);
this.tableLayoutPanel_Form.TabIndex = 26;
//
// panel_Buttons
//
this.panel_Buttons.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel_Buttons.Location = new System.Drawing.Point(3, 295);
this.panel_Buttons.Name = "panel_Buttons";
this.panel_Buttons.Size = new System.Drawing.Size(490, 24);
this.panel_Buttons.TabIndex = 0;
//
// panel_MainBody
//
this.panel_MainBody.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel_MainBody.Location = new System.Drawing.Point(3, 33);
this.panel_MainBody.Name = "panel_MainBody";
this.panel_MainBody.Size = new System.Drawing.Size(490, 256);
this.panel_MainBody.TabIndex = 1;
//
// progressBar
//
this.progressBar.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.progressBar.Location = new System.Drawing.Point(383, 324);
this.progressBar.Name = "progressBar";
this.progressBar.Size = new System.Drawing.Size(112, 21);
this.progressBar.TabIndex = 27;
//
// timer_ProgressBar
//
this.timer_ProgressBar.Interval = 700;
this.timer_ProgressBar.Tick += new System.EventHandler(this.timer_Tick);
//
// BaseView
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.CancelButton = this.btnClose;
this.ClientSize = new System.Drawing.Size(496, 346);
this.Controls.Add(this.progressBar);
this.Controls.Add(this.tableLayoutPanel_Form);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MinimumSize = new System.Drawing.Size(512, 384);
this.Name = "BaseView";
this.Controls.SetChildIndex(this.btnClose, 0);
this.Controls.SetChildIndex(this.tableLayoutPanel_Form, 0);
this.Controls.SetChildIndex(this.lblFormId, 0);
this.Controls.SetChildIndex(this.lblFormDescription, 0);
this.Controls.SetChildIndex(this.progressBar, 0);
this.tableLayoutPanel_Form.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ProgressBar progressBar;
private System.Windows.Forms.Timer timer_ProgressBar;
public System.Windows.Forms.TableLayoutPanel tableLayoutPanel_Form;
public System.Windows.Forms.Panel panel_Buttons;
public System.Windows.Forms.Panel panel_MainBody;
}
This MSDN article gave me the answer.
"Not all controls support visual inheritance from a base form. The following controls do not support the scenario described in this walkthrough: WebBrowser, ToolStrip, ToolStripPanel, TableLayoutPanel, FlowLayoutPanel, DataGridView. These controls in the inherited form are always read-only regardless of the modifiers you use (private, protected, or public)."
I used a docked Panel and a SplitContainer instead of a TLP, and now I can drop anything I want into them.

CheckedListBox docking not working properly

I created a user control for Windows forms application. All it has is a TableLayoutPanel with four cells. Cell[0,0] and cell[0,1] have labels. Cell[1,0] has a treeview and cell[1,1] has CheckedListBox.
For all four controls, I have set docking to Fill. That freaking CheckedListBox appears smaller than TreeView. Is there any way to get proper docking for the controls?
BTW, I am using .Net 3.5 and VS 2010.
Following is the designer file code for the control:
namespace UserControls
{
partial class LinkedContent
{
/// <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 Component 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.lblContentGroups = new System.Windows.Forms.Label();
this.lblModules = new System.Windows.Forms.Label();
this.tvContent = new System.Windows.Forms.TreeView();
this.chkListBoxModules = new System.Windows.Forms.CheckedListBox();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.ColumnCount = 2;
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.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.lblContentGroups, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.lblModules, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.tvContent, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.chkListBoxModules, 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 = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 7.760532F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 92.23947F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(501, 451);
this.tableLayoutPanel1.TabIndex = 0;
//
// lblContentGroups
//
this.lblContentGroups.AutoSize = true;
this.lblContentGroups.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblContentGroups.Location = new System.Drawing.Point(4, 1);
this.lblContentGroups.Name = "lblContentGroups";
this.lblContentGroups.Size = new System.Drawing.Size(243, 34);
this.lblContentGroups.TabIndex = 0;
this.lblContentGroups.Text = "Content Groups";
this.lblContentGroups.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lblModules
//
this.lblModules.AutoSize = true;
this.lblModules.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblModules.Location = new System.Drawing.Point(254, 1);
this.lblModules.Name = "lblModules";
this.lblModules.Size = new System.Drawing.Size(243, 34);
this.lblModules.TabIndex = 1;
this.lblModules.Text = "Modules";
this.lblModules.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// tvContent
//
this.tvContent.CheckBoxes = true;
this.tvContent.Dock = System.Windows.Forms.DockStyle.Fill;
this.tvContent.Location = new System.Drawing.Point(4, 39);
this.tvContent.Name = "tvContent";
this.tvContent.Size = new System.Drawing.Size(243, 408);
this.tvContent.TabIndex = 2;
//
// chkListBoxModules
//
this.chkListBoxModules.Dock = System.Windows.Forms.DockStyle.Fill;
this.chkListBoxModules.FormattingEnabled = true;
this.chkListBoxModules.Location = new System.Drawing.Point(254, 39);
this.chkListBoxModules.Name = "chkListBoxModules";
this.chkListBoxModules.Size = new System.Drawing.Size(243, 408);
this.chkListBoxModules.TabIndex = 3;
//
// LinkedContent
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "LinkedContent";
this.Size = new System.Drawing.Size(501, 451);
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Label lblContentGroups;
private System.Windows.Forms.Label lblModules;
private System.Windows.Forms.TreeView tvContent;
private System.Windows.Forms.CheckedListBox chkListBoxModules;
}
}
Check to make sure the IntegralHeight Property is not set. It defaults to true.
From above MSDN Link:
When this property is set to true, the control automatically resizes
to ensure that an item is not partially displayed. If you want to
maintain the original size of the ListBox based on the space
requirements of your form, set this property to false.

Categories

Resources