Generate a label on button click in WinForms - c#

I would like a label to be generated every time a button is clicked on my UserControl - below is the method called whenever the button is clicked:
private void button1_Click(object sender, EventArgs e)
{
}
I would be grateful for a code snippet inside the method that will generate a label with my choice of text, location and tooltip.

You can do it easily in windows forms application. Following code snippet only generates a label at specific location with hardcoded text and some basic properties settings. You can modify it according to your requirement e.g. you can change label name, location, text accordingly. Hope this will be helpful for you.
private void button1_Click(object sender, EventArgs e)
{
var lblnew = new Label
{
Location = new Point(50, 50),
Text = "My Label", //Text can be dynamically assigned e.g From some text box
AutoSize = true,
BackColor = Color.LightGray,
Font = new Font("Microsoft JhengHei UI", 9.75F, FontStyle.Regular, GraphicsUnit.Point, (byte)0)
};
//this refers to current form you can use your container according to requirement
Controls.Add(lblnew);
}
Or you can also use simplified initialization as follows;
private void button1_Click(object sender, EventArgs e)
{
var lblnew = new Label
{
Location = new Point(50, 50),
Text = "My Label", //Text can be dynamically assigned e.g From some text box
AutoSize = true,
BackColor = Color.LightGray,
Font = new Font("Microsoft JhengHei UI", 9.75F, FontStyle.Regular, GraphicsUnit.Point, (byte)0)
};
//this refers to current form you can use your container according to requirement
Controls.Add(lblnew);
}

Related

Validator new textbox c#

How to create a validator from the textbox class or create new textbox, rather than dragging the textbox from the toolbox
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
TextBox textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size (300,30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
TextBox textboxPassword = new TextBox();
textboxPassword.Location = new Point(420, 80);
textboxPassword.Size = new Size(300, 30);
textboxPassword.Name = "text_pass";
this.Controls.Add(textboxPassword);
TextBox textboxMail = new TextBox();
textboxMail.Location = new Point(420, 110);
textboxMail.Size = new Size(300, 30);
textboxMail.Name = "text_mail";
this.Controls.Add(textboxMail);
}
Add the texbox width and height
If I understand your question correctly. You want to create a textbox and check whether valid text is entered in this textbox or not. For example, the following code can be used for textboxUsername:
public TextBox textboxUsername;
private void Form1_Load(object sender, EventArgs e)
{
createTextpass();
// Creating and setting the properties of TextBox1
textboxUsername = new TextBox();
textboxUsername.Location = new Point(420, 50);
textboxUsername.Size = new Size(300, 30);
textboxUsername.Name = "text_user";
this.Controls.Add(textboxUsername);
textboxUsername.TextChanged += new EventHandler(usernameTextbox_TextChanged);
....
}
//If textboxUsername contains the # character, an alarm will be displayed
protected void usernameTextbox_TextChanged(object sender, EventArgs e)
{
if (textboxUsername.Text.Contains('#'))
MessageBox.Show("inavlid char in userName");
}

C# .NET UserControl Retain Child Control Properties on Cut-And-Paste

so I managed to build my own simple user control. Basically it is a custom Button containing a child Label control on top of it. The button works as how it should be during runtime.
However during design time I got an issue whenever I cut-and-paste that button (let's say I want to move it from Panel1 to Panel2 by cut-and-paste).
The button itself retains its properties such as background color, etc, but the child Label inside it is reinitialized everytime we paste it, so the text and color inside that label changed back to its default value.
The value of the labels text is set by "Text" property which overrides Text property of the UserControl such as follows :
private String _text = "Button";
[Browsable(true), Description("Sets the text displayed on the button"), Category("Display Settings")]
public override String Text {
get => _text;
set {
_text = value;
lb_Text.Text = _text;
}
}
Is there a way to retain child control properties during cut-and-paste in the Designer view?
Below is the code generated for InitializeComponent() section of the UserControl, which will be called whenever it is added to a form. Details aside, I acknowledged that default text and color values is re-initialized there, so I'm not sure how we replace those during cut-and-paste.
private void InitializeComponent()
{
this.lb_Text = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lb_Text
//
this.lb_Text.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(127)))), ((int)(((byte)(127)))), ((int)(((byte)(127)))), ((int)(((byte)(127)))));
this.lb_Text.Dock = System.Windows.Forms.DockStyle.Fill;
this.lb_Text.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold);
this.lb_Text.ForeColor = System.Drawing.Color.Black;
this.lb_Text.Location = new System.Drawing.Point(0, 0);
this.lb_Text.Name = "lb_Text";
this.lb_Text.Size = new System.Drawing.Size(200, 50);
this.lb_Text.TabIndex = 1;
this.lb_Text.Text = "Button";
this.lb_Text.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// MomentaryButton
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = global::HMIControls.Properties.Resources.Button_Normal;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.Controls.Add(this.lb_Text);
this.Name = "MomentaryButton";
this.Size = new System.Drawing.Size(200, 50);
this.ResumeLayout(false);
}

How can I create a button programmatically in C# window app?

In the Form1_Load method what code should I write to create a simple button?
private void Form1_Load(object sender, System.EventArgs e)
{
}
So that on Load the button would show.
As you said it is Winforms, you can do the following...
First create a new Button object.
Button newButton = new Button();
Then add it to the form inside that function using:
this.Controls.Add(newButton);
Extra properties you can set...
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
Your issue you're running to is that you're trying to set it on Form_Load event, at that stage the form does not exist yet and your buttons are overwritten. You need a delegate for the Shown or Activated events in order to show the button.
For example inside your Form1 constructor,
public Form1()
{
InitializeComponent();
this.Shown += CreateButtonDelegate;
}
Your actual delegate is where you create your button and add it to the form, something like this will work.
private void CreateButtonDelegate(object sender, EventArgs e)
{
Button newButton= new Button();
this.Controls.Add(newButton);
newButton.Text = "Created Button";
newButton.Location = new Point(70,70);
newButton.Size = new Size(50, 100);
newButton.Location = new Point(20, 50);
}
on your eventload form put this code
private void Form1_Load(object sender, EventArgs e)
{
Button testbutton = new Button();
testbutton.Text = "button1";
testbutton.Location = new Point(70, 70);
testbutton.Size = new Size(100, 100);
testbutton.Visible = true;
testbutton.BringToFront();
this.Controls.Add(testbutton);
}
It's simple :
private void Form1_Load(object sender, System.EventArgs e)
{
Button btn1 = new Button();
this.Controls.add(btn1);
btn1.Top=100;
btn1.Left=100;
btn1.Text="My Button";
}

Get the text from clicked Hyperlink

I created some Hyperlinks using the code below:
public Class_List()
{
InitializeComponent();
StackPanel myStackPanel = new StackPanel();
TextBlock txt = new TextBlock();
txt.Foreground = Brushes.Black;
txt.FontFamily = new FontFamily("03SmartFontUI");
txt.FontSize = 25;
txt.Margin = new Thickness(0, 5, 0, 5);
Run run = new Run(className);
Hyperlink link = new Hyperlink(run);
link.Click += Link_Click;
txt.Inlines.Add(link);
}
Now, I want to get the text of the hyperlink and store it on string s:
private void Link_Click(object sender, RoutedEventArgs e)
{
string s = (sender as Hyperlink).Inlines.ToString();
Class_Page class_page = new Class_Page();
NavigationService.Navigate(class_page);
}
However instead of the hyperlink text, I got
System.Windows.Documents.InlineCollection
You're getting that type because you are actually accessing the entire collection of Inlines rather than the Inline you're looking for. The fastest way to access the Run's text you're using as the first Inline in the Hyperlink's InlineCollection is to do this:
((sender as Hyperlink).Inlines.FirstInline as Run).Text;

Simple document using FastReport.Net does not show text

I tried to make a simple document using FastReport. So I started with placing a button on the form and writing the following code in order to run it when the button is clicked:
private void button1_Click(object sender, EventArgs e)
{
Report report = new Report();
ReportPage page1 = new ReportPage();
page1.Name = "Page1";
report.Pages.Add(page1);
page1.ReportTitle = new ReportTitleBand();
page1.ReportTitle.Name = "ReportTitle1";
TextObject text1 = new TextObject();
text1.Name = "Text1";
text1.Text = "REPORT TITLE TEXT";
text1.HorzAlign = HorzAlign.Center;
text1.Font = new Font("Tahoma", 14, FontStyle.Bold);
page1.ReportTitle.Objects.Add(text1);
report.Show();
}
Unfortunately, when I ran the application and I pressed the button, a blank page was shown, without any text. What is wrong with the code? Does it lack some element?
You should set width and height for text object:
text1.AutoWidth = true;
text1.Height = 100;

Categories

Resources