Define style for all labels - c#

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.

Related

Custom Component on Windows Forms not loading correctly

I am new to Windows Forms and am trying to create a custom Panel which has a header bar with two labels. I want to be able to set the text of those labels from the designer when this panel is added to another form.
Here is the code for my Panel:
public partial class TitledPanel : Panel
{
public string Title { get; set; } = "";
public string TitleNumber
{
get { return numberedLabel.Text; }
set { numberedLabel.Text = value; }
}
public TitledPanel()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
using (SolidBrush brush = new SolidBrush(BackColor))
e.Graphics.FillRectangle(brush, ClientRectangle);
e.Graphics.DrawRectangle(
new Pen(Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(229)))), ((int)(((byte)(229)))))), 0, 0, ClientSize.Width - 1, ClientSize.Height - 1);
}
private void InitializeComponent()
{
this.titleLabel = new System.Windows.Forms.Label();
this.mainHeaderPanel = new System.Windows.Forms.Panel();
this.numberedLabel = new System.Windows.Forms.Label();
this.mainHeaderPanel.SuspendLayout();
this.SuspendLayout();
//
// titleLabel
//
this.titleLabel.AutoSize = true;
this.titleLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 26F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
this.titleLabel.Location = new System.Drawing.Point(75, 27);
this.titleLabel.Margin = new System.Windows.Forms.Padding(32,32,32,32);
this.titleLabel.Name = "titleLabel";
this.titleLabel.Size = new System.Drawing.Size(193, 30);
this.titleLabel.TabIndex = 2;
this.titleLabel.Text = "";
//
// mainHeaderPanel
//
this.mainHeaderPanel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(229)))), ((int)(((byte)(229)))), ((int)(((byte)(229)))));
this.mainHeaderPanel.Controls.Add(this.numberedLabel);
this.mainHeaderPanel.Controls.Add(this.titleLabel);
this.mainHeaderPanel.Dock = System.Windows.Forms.DockStyle.Top;
this.mainHeaderPanel.ForeColor = System.Drawing.SystemColors.ControlText;
this.mainHeaderPanel.Location = new System.Drawing.Point(64, 140);
this.mainHeaderPanel.Margin = new System.Windows.Forms.Padding(64, 140, 64, 0);
this.mainHeaderPanel.Name = "mainHeaderPanel";
this.mainHeaderPanel.Size = new System.Drawing.Size(1126, 80);
this.mainHeaderPanel.TabIndex = 4;
//
// numberedLabel
//
this.numberedLabel.AutoSize = true;
this.numberedLabel.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(4)))), ((int)(((byte)(103)))), ((int)(((byte)(198)))));
this.numberedLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 28F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
this.numberedLabel.ForeColor = System.Drawing.Color.White;
this.numberedLabel.Location = new System.Drawing.Point(23, 26);
this.numberedLabel.Name = "numberedLabel";
this.numberedLabel.Size = new System.Drawing.Size(31, 32);
this.numberedLabel.TabIndex = 3;
this.numberedLabel.Text = "0";
var path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(-1, 0, numberedLabel.Width, numberedLabel.Height);
this.numberedLabel.Region = new Region(path);
//
// TitledPanel
//
this.AutoSize = true;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.mainHeaderPanel);
this.Name = "BeforeYouStartForm";
this.Size = new System.Drawing.Size(1254, 839);
this.Text = "GetStarted";
this.mainHeaderPanel.ResumeLayout(false);
this.mainHeaderPanel.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.Label titleLabel;
private System.Windows.Forms.Panel mainHeaderPanel;
private System.Windows.Forms.Label numberedLabel;
}
When I put this panel on a form and run the app, it looks like it should. However the fields Title and TitleNumber show up in the designer with the text Object reference not set to an instance of an object. next to them. Also the panel does not load in the designer, when I add it to a form, it looks like the default Panel would, and it only loads when I actually run the app.
How can I get the TitledPanel to load in the designer and how can I get Title and TitleNumber to show correctly in the designer.
Thanks

How to generate a button exactly NEXT TO another button?

I created separate class with custom button, which i want to be poped when this "newButton" is clicked.
How do i do it manually? so if there is multiple "newButtons" and i want to hide previous apeared Custom Buttons
My problem is that i dont know how to seek locations of the new buttons because it is in flowLayout which puts them automatically.
Button newButton = new Button();
newButton.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(50)))), ((int)(((byte)(175)))), ((int)(((byte)(241)))));
newButton.FlatAppearance.BorderSize = 2;
newButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
newButton.Font = new System.Drawing.Font("Arial", 10.2F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
newButton.ForeColor = System.Drawing.Color.White;
newButton.Location = new System.Drawing.Point(158, 288);
newButton.Size = new System.Drawing.Size(150, 38);
newButton.UseVisualStyleBackColor = true;
newButton.Text = k.grp;
newButton.Click += (sender, e) => newButton_Click(sender, e);
if (checkinNames.Contains(newButton.Text))
{ }
else
flowLayoutPanel5.Controls.Add(newButton);
checkinNames.Add(k.grp);
You can't achieve it by specific property directly in FlowLayoutPanel. All visual controls placed in it will automatically adjust their positions according to the size of the FlowLayoutPanel.
Here is a workaround via setting button border, backcolor, text and enable.
private void HideButton(Button button)
{
button.BackColor = this.BackColor;
button.Text = string.Empty;
button.TabStop = false;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
button.Enabled = false;
}
Hope this can help you.

Create and display label via code

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);

Dynamically added winforms control Not displaying?

I have this custom control which is basically a panel:
class ResultPanel : Panel {
Label scoreValueLabel = new Label();
public ResultPanel() : base(){
scoreValueLabel.AutoSize = true;
scoreValueLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
scoreValueLabel.Location = new System.Drawing.Point(265, 99);
scoreValueLabel.Name = "scoreValueLabel";
scoreValueLabel.Size = new System.Drawing.Size(49, 25);
scoreValueLabel.TabIndex = 10;
scoreValueLabel.Text = "+10";
Controls.Add(scoreValueLabel);
}
}
And I'm trying to add it to a panel in an event handler:
private void ResultsReceivedHandler(object sender, List<QuestionResult> results) {
ResultPanel resultPanel = new ResultPanel();
allResultsPanel.Controls.Add(new ResultPanel());
resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
resultPanel.BorderStyle = BorderStyle.FixedSingle;
resultPanel.Location = new Point(0, 155);
resultPanel.Name = "questionResultPanel";
resultPanel.Size = new Size(325, 148);
resultPanel.TabIndex = 0;
}
I know that an instance of ResultPanel can be displayed in allResultsPanel because I have added(using designer view) a ResultPanel to allResultsPanel that has the same size as this one at the top of allResultsPanel and that displays.
allResultsPanel is just a normal Panel btw, and its big enough to fit the control because its height is 800.
So why can i see the control added through the design view but not one added dynamically?
While setting up resultPanel:
ResultPanel resultPanel = new ResultPanel();
resultPanel.Anchor = ((AnchorStyles.Top | AnchorStyles.Left) | AnchorStyles.Right);
resultPanel.BorderStyle = BorderStyle.FixedSingle;
resultPanel.Location = new Point(0, 155);
resultPanel.Name = "questionResultPanel";
resultPanel.Size = new Size(325, 148);
resultPanel.TabIndex = 0;
You are adding another new panel to the allResultsPanel
allResultsPanel.Controls.Add(new ResultPanel());

Align textbox after dynamic labels

This is my code:
for (int i = 0; i < gBoxes.Length; i++)
{
var LabelID = new Label();
gLabels[i] = LabelID;
LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
LabelID.Name = "label" + i;
LabelID.Text = gColumns[i];
LabelID.Location = new System.Drawing.Point(12, StartLoc);
this.Controls.Add(LabelID);
iPanel.Controls.Add(LabelID);
var BoxID = new TextBox();
gBoxes[i] = BoxID;
BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BoxID.Name = "textbox" + i;
BoxID.Text = gContent[i];
BoxID.Location = new System.Drawing.Point(12, StartLoc);
BoxID.Size = new System.Drawing.Size(240, 19);
this.Controls.Add(BoxID);
iPanel.Controls.Add(BoxID);
StartLoc += 25;
}
Which works fine, however, the labels overlap the boxes. Which would be the best method to place the boxes after the labels, and that the boxes are aligned together.
Result:
Set the Label.AutoSize property to true. (The default value when adding them in the designer is true but the default when adding by code is false.) Also set your TextBox.Location to have a larger x value than your label... you have the starting location of label and textbox at the same x value of 12.
You can also use the AutoSize property to determine how wide the labels are and then place the textboxes accordingly. Add your labels with AutoSize = true. Arrange the text boxes by determining the widest label and resetting the TextBox.Location just to the right of them. Here is an example:
public partial class Form1 : Form
{
public int YPos { get; set; }
List<string> Labels = new List<string>();
List<Label> LabelControls = new List<Label>();
List<TextBox> TextBoxControls = new List<TextBox>();
public Form1()
{
InitializeComponent();
AddRow("medium string", "medium");
AddRow("This is a longer string", "long");
AddRow("l", "little");
Arrange();
}
void AddRow(string label, string value)
{
Labels.Add(label);
var LabelID = new Label();
LabelID.AutoSize = true; // make sure to enable AutoSize
LabelID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
LabelID.Name = "label" + Labels.Count;
LabelID.Text = label;
LabelID.Location = new System.Drawing.Point(12, YPos);
this.Controls.Add(LabelID);
panel1.Controls.Add(LabelID);
LabelControls.Add(LabelID);
var BoxID = new TextBox();
BoxID.Font = new System.Drawing.Font("Arial", 7.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
BoxID.Name = "textbox" + Labels.Count;
BoxID.Text = value;
BoxID.Location = new System.Drawing.Point(12, YPos);
BoxID.Size = new System.Drawing.Size(240, 19);
this.Controls.Add(BoxID);
panel1.Controls.Add(BoxID);
TextBoxControls.Add(BoxID);
// both controls have the same Y location
// and initially will have the same X location
YPos += 25;
}
void Arrange()
{
// determine the widest label sized by the AutoSize
int maxLabelX = 0;
for (int i = 0; i < Labels.Count; i++)
{
maxLabelX = Math.Max(maxLabelX, LabelControls[i].Location.X + LabelControls[i].Size.Width);
}
// move all the text boxes a little to the right of the widest label
for (int i = 0; i < Labels.Count; i++)
{
TextBoxControls[i].Location = new Point(maxLabelX + 10, TextBoxControls[i].Location.Y);
}
}
}
The above code generates properly placed TextBox controls:

Categories