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

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";
}

Related

How can I use a textbox, label and button that was added to a panel to input and show information

Okay so basically I have a calendar display and when you click on anyone of the dates on it, it creates a new panel with a label displaying the date selected. I also made it so when you click on a date and a new panel is made, a label, textbox and button is created and placed onto that new panel as well.
So what I want and have been struggling with is for me to enter something into that textbox then to press the button to submit it and then for it to show on the label.
I think I know what the issue is but I've been stuck at this for hours.
Here is my code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void monthCalendar1_DateSelected_1(object sender, DateRangeEventArgs e)
{
Panel newPanel = new Panel();
this.Controls.Add(newPanel);
newPanel.Visible = true;
newPanel.Size = new Size(564, 831);
newPanel.Location = new Point(0, 190);
newPanel.BringToFront();
Label textLabel = new Label();
textLabel.Size = new Size(500, 500);
textLabel.Font = new Font(textLabel.Font.Name, 25, textLabel.Font.Style);
textLabel.Location = new Point(3, 3);
Label dateLabel = new Label();
dateLabel.Size = new Size(500, 500);
dateLabel.Font = new Font(dateLabel.Font.Name, 25, dateLabel.Font.Style);
dateLabel.Location = new Point(128, 3);
Button Submitbutton = new Button();
Submitbutton.Location = new Point(100, 500);
Submitbutton.Text = "Add Food";
Submitbutton.Size = new Size(400, 100);
Submitbutton.BackColor = Color.Aqua;
Submitbutton.BringToFront();
Submitbutton.Click += Button_Click;
TextBox textBox = new TextBox();
textBox.Location = new Point(100, 650);
textBox.Size = new Size(500, 500);
textBox.BackColor = Color.Aqua;
textBox.Visible = true;
textBox.Text = "Enter food here...";
textBox.BringToFront();
Label inputtedFood = new Label();
inputtedFood.Size = new Size(500, 500);
inputtedFood.Font = new Font(inputtedFood.Font.Name, 25, inputtedFood.Font.Style);
inputtedFood.Location = new Point(100, 600);
inputtedFood.Text = "placeholder";
newPanel.Controls.Add(dateLabel);
newPanel.Controls.Add(textLabel);
newPanel.Controls.Add(Submitbutton);
newPanel.Controls.Add(textBox);
newPanel.Controls.Add(inputtedFood);
String myCalendar = monthCalendar1.SelectionRange.Start.ToShortDateString();
textLabel.Text = "Date:";
dateLabel.Text = myCalendar;
}
private void Button_Click(object sender, EventArgs e)
{
inputtedFood.Text = textBox.Text;
}
private void monthCalendar1_DateChanged_1(object sender, DateRangeEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
I tried the above code and was met with errors that are shown in the post.
Totally agree with both LarsTech and Ňɏssa Pøngjǣrdenlarp, you should be building a UserControl in place of the Panel and placing the TextBox, Button, and Label inside of that.
Your immediate question, though:
So what I want and have been struggling with is for me to enter
something into that textbox then to press the button to submit it and
then for it to show on the label.
Can be accomplished with this simple code:
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
Here's a little example showing it in action:
private void button1_Click(object sender, EventArgs e)
{
FlowLayoutPanel flp = new FlowLayoutPanel();
TextBox textBox = new TextBox();
// ... more code ...
Label inputtedFood = new Label();
inputtedFood.Text = "placeholder";
// ... more code ...
Button Submitbutton = new Button();
// ... more code ...
Submitbutton.Click += (s2, e2) =>
{
inputtedFood.Text = textBox.Text;
};
flp.Controls.Add(textBox);
flp.Controls.Add(Submitbutton);
flp.Controls.Add(inputtedFood);
flowLayoutPanel1.Controls.Add(flp);
}
The output:

Add function to dynamic button in C#

I dynamically created a Form and inside I created a button, but when I try to add my function to the button it gives this error:
Method name expected.
I am using the following code:
{
...
Form newForm = new Form();
newform.Size = new Size(477, 222);
Button btn = new Button();
btn.Size = new Size(121, 23);
btn.Location = new Point(231, 102);
btn.Text = "Text";
btn.Click += new EventHandler(Funct(label1.Text, label12.Text));
newForm.Controls.Add(btn);
}
public void Funct(string stringA, string stringB)
{
StreamWriter write = new StreamWriter(path);
write.WriteLine(stringA + "-" + stringB);
write.Close();
}
What can I do to resolve this issue?
The problem is the Funct does not comply with the signature for the Click event:
Try:
public void Funct(object sender, EventArgs e)
{
...
}
If you want to dynamically pass the strings, you may have to create your own EventArgs class adding the additional properties there.
Hope that helps.
Use lambda, change click event to
btn.Click += (s, e) => { Funct(label1.Text, label12.Text) };

Change dynamic buttons text with textbox

Let me tell you what I want to do.
1.there are two button in my form.One of them let button when I click it.Another button finish to create button.
bool active = false;
private void button1_Click(object sender, EventArgs e)
{
active = true;
}
private void button2_Click(object sender, EventArgs e)
{
active = false;
}
2.I will create button with mousedown event I want to set location of this buttons with coordinate like this.
Button button_create;
private void frm_tr_MouseDown(object sender, MouseEventArgs e)
{
if (active)
{
button_create = new Button();
button_create.Location = new Point(e.X + 5, e.Y - 15);
button_create.Size = new Size(75, 30);
button_create.Text = "Button";
this.Controls.Add(button_create);
button_create.MouseClick += new MouseEventHandler(button_create_MouseClick);
}
}
3.I started mouseclickevent of my created button.When I click this created button I will create new form textbox and button.
TextBox button_text;
void button_create_MouseClick(object sender, MouseEventArgs e)
{
Form frm = new Form();
frm.Show();
button_text = new TextBox();
Button accept = new Button();
accept.Location = new Point(frm.Width / 2, frm.Height / 2);
frm.Controls.Add(button_text);
frm.Controls.Add(accept);
accept.MouseClick += new MouseEventHandler(accept_MouseClick);
}
4.I started mouseclickevent of my last created button.I want to change text of my first created button.
void accept_MouseClick(object sender, MouseEventArgs e)
{
button_create.Text = button_text.Text;
}
5.I will click button1 and I click anywhere of this form new button will create and I also click button2 to finish create new button.I can change my created button with textbox but
If I click button1 and I create a new one button I cant change text of previous created button How can I do that?

How to check click method when button is created from a class?

I am using Tabcontrol and I created a class for all new Tabpages.
For examle when I open a new Tabpage the class creates controls and places them.
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
tp.Controls.Add(bttn1);
So my question is how can I check if this button is clicked?
Also my other question is the same with a Timer tick event.
You can easily attach to the button's Click event from the code:
bttn1.Click += new EventHandler(butt1_Click);
And here's the handler:
void button1_Click(object sender, EventArgs e)
{
// ...
}
Visual Studio will help you when you type the Click +=. After typing +=, hit the Tab key twice to get the handler.
I hope that you have created a UserControl for this or have sub-classed the TabPage class to create your controls. You should expose the Click event of the button from this newly created class through some new event you create:
public class MyTabPage : TabPage
{
private Button bttn1;
public event EventHandler Button1Clicked;
public MyTabPage()
{
bttn1 = new Button();
bttn1.Name = "button1";
bttn1.Text = "Start";
bttn1.Location = new Point(3, 405);
bttn1.Size = new Size(75, 23);
bttn1.Click += bttn1_Click;
this.Controls.Add(bttn1);
}
void bttn1_Click(object sender, EventArgs e)
{
OnButton1Clicked();
}
protected virtual void OnButton1Clicked()
{
var h = Button1Clicked;
if (h != null)
h(this, EventArgs.Empty);
}
}
Now when you create an instance of MyTabPage, you can attach a handler to the Button1Clicked event:
MyTabPage page = new MyTabPage();
page.Button1Clicked += page_Button1Clicked;
tabControl.TabPages.Add(page);
...
void page_Button1Clicked(object sender, EventArgs e)
{
}

C# using an int value after triggering an event

I came to the same question again and again. I need to use the user entered values after a button event, or a doubleclick, or anything. when I do it with the designer, it passes automatically the txt control and its value to the whole program, and I can use it anywhere. But programatically I couldn't solve it.
here's a little example:
private void Form1_Load(object sender, EventArgs e)
{
string blabla = "anything";
Button btn = new Button();
btn.Location = new Point(10, 40);
btn.Text = "Click me";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show(blabla);
}
this doesn't work, so I added a "public" and the script goes:
public string blabla;
private void Form1_Load(object sender, EventArgs e)
{
blabla = "anything";
Button btn = new Button();
btn.Location = new Point(10, 40);
btn.Text = "Click me";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show(blabla);
}
And so I can use my variable with the changed values. This goes well with controls too.
This works, but this makes thousands of public variables in a bigger application. How can I increase the readability by losing these publics? Is there a way to use "ref"? I saw it on the automatic "extract method", I just don't know, how can I use that with events.
Maybe I am on the wrong track in this, if there is a shortcut or other solution, please help.
The important change between the two snippets wasn't the fact that you made the variable public - it's that you changed it from a local variable in the Form1_Load method into an instance variable. It can still be a private instance variable, if you're handling it in the same class.
However, another alternative is to keep it as a local variable but use an anonymous function to handle the event:
private void Form1_Load(object sender, EventArgs e)
{
string blabla = "anything";
Button btn = new Button();
btn.Location = new Point(10, 40);
btn.Text = "Click me";
btn.Click += (sender, args) => {
MessageBox.Show(blabla);
// Other code here, but hopefully not too much...
};
this.Controls.Add(btn);
}
(As noted, you don't want to make the anonymous function too big, for the sake of readability - but it can always call another method with all the appropriate state.)
EDIT: As you're using VS2005, you're only using C# 2 so you can't use lambda expressions. You can use anonymous methods though. The code would then be:
private void Form1_Load(object sender, EventArgs e)
{
string blabla = "anything";
Button btn = new Button();
btn.Location = new Point(10, 40);
btn.Text = "Click me";
btn.Click += delegate {
MessageBox.Show(blabla);
// Other code here, but hopefully not too much...
};
this.Controls.Add(btn);
}
All winforms controls tend to have a Tag property, of type object which you can use to store your own custom data if you wish. Its not particularly good practice, and shows up some other architectural problems but here you go:
private void Form1_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.Location = new Point(10, 40);
btn.Text = "Click me";
btn.Click += new EventHandler(btn_Click);
btn.Tag = "blahblah";
this.Controls.Add(btn);
}
void btn_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
MessageBox.Show(btn.Tag.ToString());
}
You can do it with lambdas.
btn.Click += (sender, e) => { MessageBox.Show(blabla); }
You can do it my making a closure inside the Form1_Load method like this
private void Form1_Load(object sender, EventArgs e)
{
blabla = "anything";
Button btn = new Button();
btn.Location = new Point(10, 40);
btn.Text = "Click me";
btn.Click += (s,e) => MessageBox.Show(blabla);
this.Controls.Add(btn);
}
Even if blabla goes out of scope, the closure will still know that it contained the text "anything" when you click the button.

Categories

Resources