Determining the source of the click event dynamically - c#

I have a set of buttons that are added dynamically. As the user keeps clicking the buttons, new buttons are added to the window. I am using a winforms. I am binding the onclick event of all these buttons to the same function. I am using the following code.
System.EventHandler myEventHandle= new System.EventHandler(this.Button_Clicked);
Once a new dynamic button is created I add the event handler with the following code:
b1.Click += myEventHandle;
Now in the function Button_Clicked() I want to get the Button which invoked this event. I want to disable this Button so that it cannot be clicked again and I want the name of the Button that was clicked as I want to do various actions depending on button name. I am newbie in C#.
This is what I have tried so far, but doesn't seem to work:
Button b = sender as System.Windows.Forms.Button;
b.Font = new Font(b.Font, FontStyle.Bold);
Console.WriteLine(""+b.Name);
b.Enabled = false;

Use the sender of the event
if(sender is Button)
{
Button b = sender as Button;
b.Enabled = false;
///something = b.Name;
}

Well, your Button_Clicked method must look like
void Button_Clicked(object sender, EventArgs e) {
Button clickedButton = (Button)sender;//if sender is always a Button
clickedButton.Enabled = false;
}

Related

Checking a radiobutton if a button is clicked

How can I make it so that when I click a button, it checks a radiobutton? I'm trying to make it so that a radiobox is checked when the button is clicked
Did you even tried it yourself?
You just have to set the IsChecked-Property of your Radiobutton in the Click-Eventhandler of the Button.
You can add next line in on click event of the button:
radioButton.Checked = true;
public MainWindow()
{
// This button needs to exist on your form.
//Bind your button click event, this event will be triggered when button is clicked.
myButton.Click += myButton_Click;
}
//Once the button is clicked, now you can set the Checked property of your boolean.
void myButton_Click(object sender, RoutedEventArgs e)
{
radioButton.Checked = true;
}

Adding actions to programmatically added buttons

I am creating a program where it is required to have a list of buttons which relate to different things on the host computer.
I have achieved this as shown in the code below (example code):
Button btn = new Button();
btn.Name = "Button1";
btn.Text = "Item 1";
this.formLayoutProject.Controls.Add(btn);
This will eventually go into a for loop and more buttons will be on the flow layout, but i am starting small scale and working up so i don't get problems later.
This produces the button as expected (formatting will be done here too but haven't got that far).
What I am stuck on is how would I add a click event/right click event context menu onto the button?
I can put the click events in the code for buttons and items which will be fixed on the form and they work but I don't know how to do this dynamically as the buttons will be different on each target machine.
I have searched on Google but I haven't found what im looking for (probably due to the fact im not sure what the keywords are for the search).
How would I accomplish this?
You can add a button click event handler by using += operator on the event of your new btn object:
// Add 10 buttons dynamically.
// Bind to the same method.
for (var i = 1; i <= 10; i += 1)
{
Button btn = new Button();
btn.Name = "Button" + i;
btn.Text = "Item " + i;
this.formLayoutProject.Controls.Add(btn);
// Add handler.
btn.Click += btn_Click;
}
Now you just need to define the handler method:
private void btn_Click(object sender, EventArgs e)
{
// This is the button which was clicked.
var button = (Button)sender;
var buttonName = button.Name;
// Do some stuff.
// To detect certain mouse click events, cast e as a MouseClick.
MouseEventArgs mouseEvents = (MouseEventArgs)e;
// Now use the mouseEvents object to handle specific events based on the button clicked.
if (mouseEvents.Button == MouseButtons.Right)
{
// Right button clicked.
}
else if (mouseEvents.Button == MouseButtons.Left)
{
// Left button clicked.
}
}

How to make a button event handler Method in C#?

I am new to C# and I am making a small tic-tac-toe game using WPF. However, I would like to make my code compact and would like to create a method which will take button click event as argument and modify the button. For the time being, this is what I am trying to do:
Button.IsEnabled = false;
Button.Content = "X";
I want to make a event which will do this job when called inside a button click event and hence I will not need to copy this code for every button.
IN the xaml, add the event handler to the button:
<Button Click="Button_Click" />
(add the same handler to all buttons)
in the code behind, cast the sender to a button:
private void Button_Click(object sender, RoutedEventArgs e)
{
var button = sender as Button;
if (button != null)
{
button.IsEnabled = false;
button.Content = "X";
}
}
Attach the event handler and use the sender:
Button.Click += this.Button_Click;
private void Button_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
if (button != null)
{
button.IsEnabled = false;
button.Content = X;
}
}

C# address Button using a string

I have a Problem with addressing a button. I have many buttons in my program and I have a function which is used by every button.
I'm getting the name of the last clicked button with this:
foreach (Control t in this.Controls)
{
if (t.Focused)
{
ClickedButton = t.Name;
}
}
Then I want to change the Text of the button:
ClickedButton.Text = "Whatever";
But I can't use ClickedButton as the name of the button.
Thank you in advance!
Assuming this is an event, you can just do something like this
Button btn = (Button)sender;
btn.Text = "Whatever";
If you are writing this in your button_click event,
You can Get the button like this :
Button BTN = sender as Button;
BTN.Text = "This Button Has Been Clicked!";
If you're having the clicked button call into the Click event, you should have sender as an argument, which you can cast to a Button and get the name of the control.
Since you have the button reference, you could then also set the control's text.
protected void btnTest_Click(object sender, EventArgs e)
{
Button b = sender as Button;
if ((b != null) && (b.Name == "btnTest"))
{
b.Text = "yay";
}
}

Button id as an event handler

I am creating 7 buttons on the fly
when i create the buttons i am trying to have an event handler than can deal with all clicks in one method via a switch. Ideally i want to pass an id with the button that indicates what was clicked, opposed to this solution of
void pdfButton_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
Console.WriteLine(b.Text);
}
as all of the buttons using this event handler have the same text. I have a unique id associated witht the buttons but no idea how to send them
thanks
You can use the Name or Tag properties.
Put the ID in the Tag property on the button when you create them and then check the ID in your event handler.
Button button = new Button();
button.Tag = 1;
...
void pdfButton_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
switch ((int)b.Tag)
{
...
}
}
First: Bad practice to handle several clicks in one event via switch. But however a solution would be:
Create your own control which inherits the button and ad your ID as an property. So you can access it via:
MyButton b = (MyButton)sender;
switch(b.ID) {
//Code goes here
}
If each button you add has a unique Id, why not just use the ID property of the button?
Button button = new Button();
button.ID= "Button1";
//...
void pdfButton_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
switch(button.ID)
{
case "Button1":
//...
}
}

Categories