How to centre align the content of windows form application? - c#

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

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 do I fix broken/disappearing controls

I can only guess the problem lies within the following code, but I'm not 100% sure. The problem only occurs when I try to move the CtrlComputer object in the panel in the RoomDesigner form. The aim of the code is to get the CtrlComputer object to move as one when clicked. Unfortunately without the foreach loop, moves the label1 separately, but still makes an grey box around it when dragged. See here for a clearer picture. I'm not sure why dragging objects around would enlarge the custom control itself.
CtrlComputer
public CtrlComputer()
{
InitializeComponent();
label1.MouseMove += new MouseEventHandler(textbox_MouseMove);
CompButton.MouseMove += new MouseEventHandler(textbox_MouseMove);
this.MouseMove += new MouseEventHandler(textbox_MouseMove);
void textbox_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
return;
foreach (Control control in this.Controls)
{
var location = control.Location;
location.Offset(e.Location.X - previousLocation.X, e.Location.Y - previousLocation.Y);
control.Location = location;
}
}
CtrlComputer.Designer
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.CompButton = new WindowsFormsApplication1.DoubleClickButton();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Lime;
this.label1.Location = new System.Drawing.Point(7, 20);
this.label1.Margin = new System.Windows.Forms.Padding(0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 39;
this.label1.Text = "label1";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// CompButton
//
this.CompButton.BackColor = System.Drawing.Color.Transparent;
this.CompButton.BackgroundImage = global::SchoolAdmin.Properties.Resources.memeio;
this.CompButton.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.CompButton.Location = new System.Drawing.Point(0, 0);
this.CompButton.Name = "CompButton";
this.CompButton.Size = new System.Drawing.Size(62, 57);
this.CompButton.TabIndex = 40;
this.CompButton.UseVisualStyleBackColor = false;
//
// CtrlComputer
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.BackColor = System.Drawing.Color.Transparent;
this.Controls.Add(this.label1);
this.Controls.Add(this.CompButton);
this.Name = "CtrlComputer";
this.Size = new System.Drawing.Size(65, 60);
this.ResumeLayout(false);
this.PerformLayout();
}
CtrlComputer - http://pastebin.com/GJ3aXJFb

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.

Error when trying to get an int from textbox

I am new to C# and programming in general. I was able to create the required program in Console but want to get one working with Forms as well. I am running into an issue when trying to get int from textboxes.
On Debug I am getting error:
Error 3 'int' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) D:\Dropbox\Classwork\C_Sharp\InProgress\PaintDeterminator\Paint Determinator Form\Paint Determinator Form\Form1.cs 30 57 Paint Determinator Form
Here is the code I've written so far.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Paint_Determinator_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int LengthtextBox;
int WidthtextBox;
int HeighttextBox;
int paint;
int answer;
LengthtextBox = int.Parse(LengthtextBox.Text);
WidthtextBox = int.Parse(WidthtextBox.Text);
HeighttextBox = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (LengthtextBox * WidthtextBox * HeighttextBox) / paint;
MessageBox.Show( answer.ToString() );
}
}
}
namespace Paint_Determinator_Form
{
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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.WidthtextBox = new System.Windows.Forms.TextBox();
this.HeighttextBox = new System.Windows.Forms.TextBox();
this.LengthtextBox = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(454, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" +
"ields below!";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 91);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Width";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(28, 139);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(38, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Height";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(28, 183);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Length";
//
// WidthtextBox
//
this.WidthtextBox.Location = new System.Drawing.Point(175, 83);
this.WidthtextBox.Name = "WidthtextBox";
this.WidthtextBox.Size = new System.Drawing.Size(100, 20);
this.WidthtextBox.TabIndex = 5;
//
// HeighttextBox
//
this.HeighttextBox.Location = new System.Drawing.Point(175, 131);
this.HeighttextBox.Name = "HeighttextBox";
this.HeighttextBox.Size = new System.Drawing.Size(100, 20);
this.HeighttextBox.TabIndex = 6;
//
// LengthtextBox
//
this.LengthtextBox.Location = new System.Drawing.Point(175, 183);
this.LengthtextBox.Name = "LengthtextBox";
this.LengthtextBox.Size = new System.Drawing.Size(100, 20);
this.LengthtextBox.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(349, 402);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "Paint";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(511, 447);
this.Controls.Add(this.button1);
this.Controls.Add(this.LengthtextBox);
this.Controls.Add(this.HeighttextBox);
this.Controls.Add(this.WidthtextBox);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox WidthtextBox;
private System.Windows.Forms.TextBox HeighttextBox;
private System.Windows.Forms.TextBox LengthtextBox;
private System.Windows.Forms.Button button1;
}
Why do you name your ints like your textboxes? It is really a bad practice and confusing at the uttermost level. As you can see, the compiler thinks that you are using the int variables instead of the textboxes and complains that an int type have no property called Text.
So simply change the name of the ints inside the click method
private void button1_Click(object sender, EventArgs e)
{
int l;
int w;
int h;
int paint;
int answer;
l = int.Parse(LengthtextBox.Text);
w = int.Parse(WidthtextBox.Text);
h = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (l * w * h) / paint;
MessageBox.Show( answer.ToString() );
}
Said that, I suggest to use Int32.TryParse to convert the data typed by your user in a valid integer. The Parse method will throw an exception if your user types something that cannot be translated to an integer, instead TryParse returns false without a costly exception
For example
int l;
if(!Int32.TryParse(LengthtextBox.Text, out l))
{
MessageBox.Show("Please type a valid number for Length");
return;
}
When the Int32.TryParse returns, the out parameter (l) contains the
32-bit signed integer value equivalent of the number contained in your
textbox, if the conversion succeeds, or zero if the conversion fails
You're declaring locally scoped variables that will replace your actual textboxes. Yo ushould use local variables that have different names like:
int length = int.Parse(LengthtextBox.Text);
int width = int.Parse(WidthtextBox.Text);;
int height = int.Parse(HeighttextBox.Text);;
I updated your code, you were using variables with the same names as your actual text boxes... That's not a good idea:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Paint_Determinator_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int length;
int width;
int height;
int paint;
int answer;
length = int.Parse(LengthtextBox.Text);
width = int.Parse(WidthtextBox.Text);
height = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (length* width* height) / paint;
MessageBox.Show( answer.ToString() );
}
}
}
namespace Paint_Determinator_Form
{
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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.WidthtextBox = new System.Windows.Forms.TextBox();
this.HeighttextBox = new System.Windows.Forms.TextBox();
this.LengthtextBox = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(454, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" +
"ields below!";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 91);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Width";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(28, 139);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(38, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Height";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(28, 183);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Length";
//
// WidthtextBox
//
this.WidthtextBox.Location = new System.Drawing.Point(175, 83);
this.WidthtextBox.Name = "WidthtextBox";
this.WidthtextBox.Size = new System.Drawing.Size(100, 20);
this.WidthtextBox.TabIndex = 5;
//
// HeighttextBox
//
this.HeighttextBox.Location = new System.Drawing.Point(175, 131);
this.HeighttextBox.Name = "HeighttextBox";
this.HeighttextBox.Size = new System.Drawing.Size(100, 20);
this.HeighttextBox.TabIndex = 6;
//
// LengthtextBox
//
this.LengthtextBox.Location = new System.Drawing.Point(175, 183);
this.LengthtextBox.Name = "LengthtextBox";
this.LengthtextBox.Size = new System.Drawing.Size(100, 20);
this.LengthtextBox.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(349, 402);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "Paint";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(511, 447);
this.Controls.Add(this.button1);
this.Controls.Add(this.LengthtextBox);
this.Controls.Add(this.HeighttextBox);
this.Controls.Add(this.WidthtextBox);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox WidthtextBox;
private System.Windows.Forms.TextBox HeighttextBox;
private System.Windows.Forms.TextBox LengthtextBox;
private System.Windows.Forms.Button button1;
}
Remove those definitions from button1_Click() method as you use the same names as the class variables inside this method:
int LengthtextBox;
int WidthtextBox;
int HeighttextBox;
And change the name of the variables:
int length;
int width;
int height;
length= int.Parse(LengthtextBox.Text);
width= int.Parse(WidthtextBox.Text);
height= int.Parse(HeighttextBox.Text);
paint = 350;
answer = (length* width* height) / paint;
In your situatuation it's better to rename ints, but you are ablealso to use this in simmilar situation
LengthtextBox = int.Parse(this.LengthtextBox.Text);
WidthtextBox = int.Parse(this.WidthtextBox.Text);
HeighttextBox = int.Parse(this.HeighttextBox.Text);
I think if you are new in programing it's good to know about this too, instead just renaming variables without understanding.

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