Create and display label via code - c#

I want to create a label and display it to the user, but I can not.
I have tried to copy the code for any label in InitializeComponent() ...
(I added a label to Form1 using the toolbox.)
partial class Form1
{
private System.Windows.Forms.Label label1;
private void InitializeComponent()
{
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
}
}
... and then to apply it to my label.
(I removed the label I added earlier.)
public partial class Form1 : Form
{
private Label label;
public Form1()
{
InitializeComponent();
label = new Label();
label.AutoSize = true;
label.Location = new System.Drawing.Point(0, 0);
label.Name = "label";
label.Size = new System.Drawing.Size(0, 0);
label.TabIndex = 0;
label.Text = "Test";
//label.Enabled = true;
label.Visible = true;
//label.Select();
//label.Show();
}
}
But it does not work.
How to do ?

You need to add te label to add it to the forms' list of controls.
So, in your Form1() function, add the follwoing after the label is created:
this.Controls.Add(label);

You forgot vital part, i.e. add label to the form's ControlCollection:
this.Controls.Add(label);

Related

How to create custom message in C#?

I am design a custom message box. This message box will show a list of string. But it's not work. Can you hep me to fix it.
You can see in the picture, I have a list with 7 items. When i click the "Grade Project", the Message box don't show any item.
And this is the result I need
//Main form
private void btnGrade_Click(object sender, EventArgs e)
{
List<string> result = new List<string>();
result.Add("True");
result.Add("False");
result.Add("True");
result.Add("False");
result.Add("True");
result.Add("False");
result.Add("False");
MsgBox.Show(result,"Project 1",MsgBox.Buttons.OK);
}
This is code in msgbox form
//MsgBox form
public partial class MsgBox : Form
{
private static MsgBox _msgBox;
// Header, Footer
private Panel _plHeader = new Panel();
private Label _lblTitle;
private Panel _plFooter = new Panel();
private Panel _plIcon = new Panel();
// Panel
private FlowLayoutPanel _flpButtons = new FlowLayoutPanel();
// button
private List<Button> _buttonCollection = new List<Button>();
// Kết quả
private static DialogResult _buttonResult;
// Message
private List<String> _lblMessage;
private MsgBox()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.BackColor = Color.FromArgb(45, 45, 48);
this.StartPosition = FormStartPosition.CenterScreen;
this.Padding = new System.Windows.Forms.Padding(3);
this.Width = 800;
// Header
_lblTitle = new Label();
_lblTitle.ForeColor = Color.White;
_lblTitle.Font = new System.Drawing.Font("Segoe UI", 18);
_lblTitle.Dock = DockStyle.Top;
_lblTitle.Height = 60;
// Message
_lblMessage = new List<string>();
for (int i = 0; i < _lblMessage.Count; i++)
{
TextBox txt = new TextBox(); //Create Textbox có name txt
txt.Text = _lblMessage[i];
txt.ForeColor = Color.Red;
txt.Font = new System.Drawing.Font("Segoe UI", 30);
this.Controls.Add(txt); //add control txt
}
_flpButtons.FlowDirection = FlowDirection.RightToLeft;
_flpButtons.Dock = DockStyle.Fill;
_plHeader.Dock = DockStyle.Fill;
_plHeader.Padding = new Padding(20);
// _plHeader.Controls.Add(_lblMessage);
_plHeader.Controls.Add(_lblTitle);
_plFooter.Dock = DockStyle.Bottom;
_plFooter.Padding = new Padding(20);
_plFooter.BackColor = Color.FromArgb(37, 37, 38);
_plFooter.Height = 80;
_plFooter.Controls.Add(_flpButtons);
// Add controls vào form
this.Controls.Add(_plHeader);
//this.Controls.Add(_plIcon);
this.Controls.Add(_plFooter);
}
public static DialogResult Show(List<String> message, string title, Buttons buttons)
{
_msgBox = new MsgBox();
_msgBox._lblMessage = message;
_msgBox._lblTitle.Text = title;
_msgBox._plIcon.Hide();
MsgBox.InitButtons(buttons);
_msgBox.ShowDialog();
return _buttonResult;
}
Thank you for your watching.
Something wrong with your codes:
private MsgBox()
{
...
// Message
_lblMessage = new List<string>();
...
}
Your _lblMessage will always be an empty list so you see no message at all.
You can change your codes like this:
private MsgBox(List<String> messages)
{
...
// Message
_lblMessage = messages;
...
}
public static DialogResult Show(List<String> message, string title)
{
_msgBox = new MsgBox(message);
//_msgBox._lblMessage = message;
....
}
And also, you'd better set TextBox position otherwise all the TextBox will overlap with each other.

Winforms: ComboBox height doesn't resize when resolution is changed

I have a basic combobox in a form. Compared to other controls(Button,label, etc) the height of the combobox doesn't change when the resolution is changed.
public partial class Form1 : Form
{
string result;
string fontInformation;
private bool scaleFactorKnown = false;
private SizeF scaleFactor;
public Form1()
{
SizeChanged += Form1_SizeChanged;
InitializeComponent();
label1.Location = new Point(12, 36);
label1.Size = new Size(100, 21);
label1.Scale(scaleFactor);
//
// textBox1
//
textBox1.Location = new Point(133, 33);
textBox1.Size = new Size(100, 21);
textBox1.Scale(scaleFactor);
//
// comboBox1
//
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
// button1
//
button1.Location = new Point(365, 32);
button1.Size = new Size(100, 21);
button1.Scale(scaleFactor);
//
// radioButton1
//
radioButton1.Location = new Point(480, 32);
radioButton1.Size = new Size(100, 21);
radioButton1.Scale(scaleFactor);
//
// checkBox1
//
checkBox1.Location = new Point(586, 33);
checkBox1.Size = new Size(100, 21);
checkBox1.Scale(scaleFactor);
//
// textBox2
//
textBox2.Location = new Point(26, 102);
textBox2.Size = new Size(660, 250);
textBox2.Scale(scaleFactor);
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
if (!scaleFactorKnown)
{
scaleFactor = AutoScaleFactor;
scaleFactorKnown = true;
}
Size controlSize = new Size((int)(comboBox1.Width * scaleFactor.Width),
(int)(comboBox1.Height * scaleFactor.Height)); //use for sizing
//set bounds
comboBox1.Bounds = new Rectangle(comboBox1.Location, controlSize);
}
}
I have tried the method Scale() to scale all other controls, it works for other controls except for combobox. I also tried manually changing the bound but it didn't work. I also tried change the anchor and dock as well.
Expected result: Combobox height(At 150%)=42
Actual result: Combobox
height(At 150%)=28
Would appreciate any help on how to fix this issue.
You have to set the IntegralHeight property of the ComboBox to false:
comboBox1.Location = new Point(250, 33);
comboBox1.Size = new Size(100, 21);
comboBox1.Scale(scaleFactor);
comboBox1.IntegralHeight = false;

Define style for all labels

I'm working in a c# application in winform.
I saw that there is no Style for element (unlike WPF). But is there a way to simply set all the labels to a specific design ?
Actually I do :
public partial class myControl : UserControl
{
private Color LabelColor = Color.Indigo;
private Color LabelFont = new System.Drawing.Font("Arial",
18F,
System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point,
((byte)(0)));
public myControl()
{
InitializeComponent();
//Set design
designLabels();
}
private void designLabels()
{
List<Label> labelsToStyle = new List<Label>();
labelsToStyle.Add(labelName);
labelsToStyle.Add(labelAge);
labelsToStyle.Add(labelSize);
foreach (Label l in labelsToStyle)
{
l.ForeColor = LabelColor;
l.Font = LabelFont;
l.Dock = DockStyle.Fill;
}
}
}
It works but it doesn't display correctly in designer (I have to run application to see my design). And maybe it exists a simplest way ?
As per my comment the easiest is to create a custom control and use that one in your windows.
here how simple it is. Simply override the label and set in constructor the default value you want
public class DesignLabel : Label
{
public DesignLabel()
{
ForeColor = Color.Indigo;
Font = new System.Drawing.Font("Arial", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}
}
then compile once and go to your design view, open your toolbox and you will see the "DesignLabel", simply drag drop on the window and that's it. If you change default in the class it will be changed all over the place.
If you want to change the style already at design time so that you can see in in the Form.cs[design] you need to edit the Form.Designer.cs file! But this you have to do label for label by hand. I saved in this example the Font and Color in the project properties (sorry for the german version):
in my example I have 3 Labels. In the Form.Designer.cs file you can add the properties:
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 15);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
this.label1.Font = Properties.Settings.Default.LabelFont;
this.label1.ForeColor = Properties.Settings.Default.LabelColor;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 68);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(41, 15);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
this.label2.Font = Properties.Settings.Default.LabelFont;
this.label2.ForeColor = Properties.Settings.Default.LabelColor;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(12, 122);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(41, 15);
this.label3.TabIndex = 2;
this.label3.Text = "label3";
this.label3.Font = Properties.Settings.Default.LabelFont;
this.label3.ForeColor = Properties.Settings.Default.LabelColor;
The result looks like this:
DISCLAIMER
I would not recommend this approach! Editing the Form.Desginer.cs file is never a good idea! I would stick to the run time changes. If you want to change all Labels just filter the this.Controls for it and foreach through the collection like this:
this.Controls.OfType<Label>().ToList().ForEach(lbl =>
{
lbl.Font = LabelFont;
lbl.ForeColor = LabelColor;
//lbl.Dock = DockStyle.Fill; // uncommented, because I could see only 1 Label
});
The result will be the same.

Centering OK button within a MessageBox in winforms

I am learning C#, and as a part of it I wrote small app that consists of form with two groups of buttons, two buttons each. Button for closing of app is added as well. And everything works ok. Also there is MessageBox, showing that the app is going to be closed. And here is little problem that bugs me: OK button within that MessageBox is NOT centered horizontally. I guess there is a method to align that button, but what puzzles me is why it's is not centered by default? As illustration here are screenshots:
Here is code as well:
using System;
using System.Drawing;
using System.Windows.Forms;
public class myForm : Form
{
private GroupBox gboxGrp1;
private GroupBox gboxGrp2;
private RadioButton butn1a;
private RadioButton butn1b;
private RadioButton butn2a;
private RadioButton butn2b;
private Button btnClose;
public myForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnClose = new Button();
this.gboxGrp1 = new GroupBox();
this.gboxGrp2 = new GroupBox();
this.butn1a = new RadioButton();
this.butn1b = new RadioButton();
this.butn2a = new RadioButton();
this.butn2b = new RadioButton();
//myForm
this.Text = "My Form";
this.StartPosition = FormStartPosition.CenterScreen;
this.Height = 350;
this.Width = 200;
//btnClose
this.Controls.Add(btnClose);
this.btnClose.Text = "Close";
this.btnClose.Location = new Point(60, 260);
this.btnClose.Click += new EventHandler(btnClose_Click);
//gboxgrp1
this.gboxGrp1.Location = new Point(20, 20);
this.gboxGrp1.Text = "Group Box 1";
this.gboxGrp1.Width = 150;
this.gboxGrp1.Height = 100;
//gboxgrp2
this.gboxGrp2.Text = "Group Box 2";
this.gboxGrp2.Location = new Point(20, 130);
this.gboxGrp2.Width = 150;
this.gboxGrp2.Height = 100;
//Radio buttons
this.butn1a.Text = "Radio 1a";
this.butn1a.Location = new Point(30, 30);
this.butn1a.Size = new Size(90, 15);
this.butn1b.Text = "Radio 1b";
this.butn1b.Location = new Point(30, 60);
this.butn1b.Size = new Size(90, 15);
this.butn2a.Text = "Radio 2a";
this.butn2a.Location = new Point(30, 30);
this.butn2a.Size = new Size(90, 15);
this.butn2b.Text = "Radio 2b";
this.butn2b.Location = new Point(30, 70);
this.butn2b.Size = new Size(90, 15);
//Controls
this.Controls.Add(gboxGrp1);
this.Controls.Add(gboxGrp2);
this.gboxGrp1.Controls.Add(butn1a);
this.gboxGrp1.Controls.Add(butn1b);
this.gboxGrp2.Controls.Add(butn2a);
this.gboxGrp2.Controls.Add(butn2b);
}
private void btnClose_Click(object sender, EventArgs e)
{
MessageBox.Show("Closing Application");
Application.Exit();
}
}
public class MyApp
{
public static void Main()
{
Application.Run(new myForm());
}
}
You can't restyle the default MessageBox as that's completely depends on the windows OS theme. However you could create your own message box by simply creating a new form and then call it by using newMessagebox.ShowDialog();

keep button position on panel resize

I've been messing around with something that should be simple.
I've moved jobs and trying to get some of the basic tools I have used before, but of course I don't have the old source to look at.
We had extended panel to have some standard properties and functions (save, close, save and close).
But I can't get the buttons to be positioned correctly on a resize. I put this ExtPanel on a form but the buttons keep disappearing as I resize, or don't move as expected (frozen on bottom right).
The class
public partial class ExtPanel: UserControl
{
private System.Windows.Forms.Button btnSaveandClose;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnSave;
public ExtPanel ()
{
InitializeComponent ();
}
// misc things this class does...
}
public partial class ExtPanel
{
private void InitializeComponent ()
{
this.btnSaveandClose = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnSave = new System.Windows.Forms.Button();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// btnSaveandClose
//
this.btnSaveandClose.Location = new System.Drawing.Point(899, 689);
this.btnSaveandClose.Name = "btnSaveandClose";
this.btnSaveandClose.Size = new System.Drawing.Size(100, 30);
this.btnSaveandClose.TabIndex = 0;
this.btnSaveandClose.Text = "Save and Close";
this.btnSaveandClose.UseVisualStyleBackColor = true;
this.btnSaveandClose.Click += new System.EventHandler(this.Click_SaveandClose);
this.btnSaveandClose.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(687, 689);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(100, 30);
this.btnCancel.TabIndex = 1;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.Click_Close);
this.btnCancel.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(793, 689);
this.btnSave.Name = "btnSave";
this.btnSave.Size = new System.Drawing.Size(100, 30);
this.btnSave.TabIndex = 2;
this.btnSave.Text = "Save";
this.btnSave.UseVisualStyleBackColor = true;
this.btnSave.Click += new System.EventHandler(this.Click_Save);
this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles) ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
//
// panel1
//
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.btnSave);
this.panel1.Controls.Add(this.btnCancel);
this.panel1.Controls.Add(this.btnSaveandClose);
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(1008, 730);
this.panel1.TabIndex = 0;
//
// ExtPanel
//
this.Controls.Add(this.panel1);
this.Name = "ExtPanel";
this.Size = this.panel1.Size;
this.Click += new System.EventHandler(this.click_this);
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
}
Have you tried anchoring the button?
...I missed the anchoring code.
If you have a button in the lower right-hand corner that you want to stay in the lower right-hand corner when resizing the form, set it's anchor properties to Bottom, Right.
Update:
I loaded your code. You have a panel inside of ExtPanel. If you dock (Fill) that panel then you should be working fine by resizing ExtPanel.

Categories