How to give function to Button which is created programmatically - c#

I want to create button programmatically and give a function to it.
Function: when I click on the button, remove/hide the button.
enter image description here

Declare your Function as an event:
protected void YourButtonClickEvent(object sender, EventArgs e)
{
// To hide your button
Button button = sender as Button;
button.visible = false;
// Your code
}
Declare your button and attach the event to it
Button button = new Button();
button.Click += new EventHandler(YourButtonClickEvent);

Related

How to properly assign events to dynamically created buttons in c# Winforms?

My intention is to create buttons at runtime and have a click event handler subscribed to them. Once the dynamically created button is clicked, the click eventhandler is unsubscribed, such that the click event only fires once.
At runtime the desired behaviour only works if I create one button and click it immediately . If I create more than one button, than only the last created button unsubscribes from the click event. Did I miss something?
public partial class Form1 : Form
{
Button b;
int counter;
public Form1()
{
InitializeComponent();
}
// create more buttons
private void button1_Click(object sender, EventArgs e)
{
b = new Button();
b.Size = new Size(50, 50);
b.Click += b_Click; // dynamic button click event
this.Controls["flowLayoutPanel"].Controls.Add(b);
}
// dynamic button click eventhandler
void b_Click(object sender, EventArgs e)
{
b.Text = counter.ToString();
b.Click -= b_Click;
counter++;
}
}
Because b member will reference last created dynamic button, so clicking any buttons will remove click event handler of currently referenced button in b variable, which would be last created.
Use sender to access instance of "current" button and remove click event handler only from "current" button.
void b_Click(object sender, EventArgs e)
{
var button = sender As Button;
button.Text = counter.ToString();
button.Click -= b_Click;
counter++;
}
Don't use private member for dynamic button, but local variable
private void button1_Click(object sender, EventArgs e)
{
var button = new Button();
button.Size = new Size(50, 50);
button.Click += b_Click;
this.Controls["flowLayoutPanel"].Controls.Add(button);
}
If you need to reference collection of created button somewhere, you can access them from the controls of flow panel where buttons were added
var dynamicButtons = .
this.Controls["flowLayoutPanel"].Controls.OfType<Button>().ToList();
Or save them to the dedicated collection (in case flow panel has other buttons)

Set button handle in C#

btnName1 = new Button();
counter++;
//Start setting of Button
btnName1.Location = new Point(47, 35 + a);
btnName1.Size = new Size(132, 59);
btnName1.FlatStyle = FlatStyle.Popup;
btnName1.Text = textBox1.Text;
btnName1.Name = "btn" + counter.ToString();
btnName1.BackColor = Color.White;
btnName1.ForeColor = Color.Black;
panel1.Controls.Add(btnName1);
a += btnName1.Size.Height + 2;
btnName1.Click += BtnName1_Click;
I wrote this code for making a new button. When I click on add button this code runs and by each click on add button we can make new button.
But my problem is this
How can I set click handle for each button? I mean when I click on each button, they show their text to me
and I wrote this code to make the texts different:
btnName1.Text = textBox1.Text;
You didn't post your event code, but as Steve mentioned in his comment (that appears to have since been removed), you can use the sender argument to get the particular button that was clicked. Something like the following should be what you're after:
private void BtnName1_Click(object sender, EventArgs e)
{
//Access the text with: (sender as Button).Text
//Example: Write this button's text to the debug output window
Debug.WriteLine((sender as Button).Text);
}
Just be careful that in my specific example, you're only subscribing a Button to this event.
Prefered way is what you've put so far
btnName1.Click += BtnName1_Click;
...
private void BtnName1_Click(object sender, EventArgs e) {
// Button which has been clicked
Button button = sender as Button;
//TODO: put relevant code here
...
}
However, you can assign to each button its own event handler:
btnName1.Click += (s, e) => {
Button button = s as Button;
//TODO: put relevant code here
...
};

dynamically created button click event is not working in asp.net c#

I am trying to call dynamically created button click event. I this event I want to show one message on clicking dynamically created button.
my Code
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnMain_Click(object sender, EventArgs e)
{
Button btnNew = new Button();
btnNew.ID = "btnClick";
btnNew.Text = "Click";
btnNew.Click += new System.EventHandler(btnNew_Click);
this.form1.Controls.Add(btnNew);
}
protected void btnNew_Click(object sender, EventArgs e)
{
Label lblMeaaseg = new Label();
lblMeaaseg.ID = "txtMessage";
lblMeaaseg.Text = "Hello Shree";
this.form1.Controls.Add(lblMeaaseg);
}
You create the dynamic button in the click event handler of btnMain during the postback caused by btnMain click. After that you see the new button in the browser page, click it and expect its click event handler (btnNew_Click) to fire. Pressing the new dynamic button causes a new postback that is processed by a new instance of the page created on the server by ASP.NET. This new page does not have the dynamic button - there is nothing there connected to btnNew_Click. You have to write code that persists the fact that the dynamic button has been created and recreates this button every time the page is instantiated. So that this button has a chance to feel and respond to its client-side click.

Passing a button to an event handler

I want to pass a button to an event handler outside the scope of the button object.
For example:
void OnClick2(object sender, EventArgs e)
{
button123.text="changed";
}
So I want my click to change the text of a button passed to it. Is there a way to do this other than having the button in scope- can I pass the button object to the event handler somehow?
I am not 100% sure of your question but here is an attempt to answer it.
void ButtonClicked(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
button.Text = "changed";
}
}
This allow you to handle any button click and it will change that buttons text.

Access control properties from its dynamically created Event Handler

I am developing a C# WinForms application. On the FormLoad event, I dynamically create and add to the form 100 buttons without text, but with names like button1, button2 etc. Also, onto these buttons, after their creation, I dynamically link a unique event handler for the ButtonClick event. How can I access the button's properties from within the event handler (more specifically the button name)?
If I use this.Name, I get the name of the form, and not the name of the button.
Later Edit: (for those who might wonder here in search of solutions)
private void function1()
{
Button a = new Button();
a.Name = "button" + (i * j).ToString();
a.Click += new EventHandler(OnFieldButtonClicked);
}
private void OnFieldButtonClicked(object sender, EventArgs e)
{
Button button = (Button)sender;
MessageBox.Show(button.Name);
}
The sender argument is the event handler encapsulates an instance of the object that has triggered the event:
Button button = (Button) sender;
String text = button.Text;

Categories

Resources