C# address Button using a string - c#

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

Related

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

Clicking a Button in a button array

I have the code below.
public void WepaonEquip(Object sender, System.EventArgs e)
{
if (button[0].BackColor == Color.Beige)
{
button[0].BackColor = Color.OrangeRed;
}
else if (button[1].BackColor == Color.Beige)
{
button[1].BackColor = Color.OrangeRed;
}
else if (button[2].BackColor == Color.Beige)
{
button[2].BackColor = Color.OrangeRed;
}
}
The code in the class containing this chunk of code generates a button array. What I want is that the user will click a button and the colour of the button clicked will change.
However, when the user clicks, lets say, the 3rd button, the first button in the array changes colour, not the one clicked. Any idea as to why this isn't working? I believe the logic of the code works, perhaps I'm missing something.
Set each button in the panel to use the same Click Event handler. In the handler cast sender as a button and change the color
Assuming that WeaponEquip is the click event handler for the buttons it would look something like this:
public void WepaonEquip(Object sender, System.EventArgs e)
{
Button clickedbutton = (Button)sender
clickedbutton.BackColor = Color.OrangeRed;
}

Determining the source of the click event dynamically

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

Enabling all buttons in a repeater except for the clicked one

I have a Repeater Control with various buttons in it.
When the button gets clicked, it needs to disable itself so it cant be clicked again. Working.
However, when I click that button, it needs to enable any other button but it.
So, When I click on it, it needs to disable. When I click on another one, the previous button must enable, and that one must disable.
So for I've tried:
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Button b in e.Item.Controls.OfType<Button>().Select(c => c).Where(b => b != btnLoad))
{
b.Enabled = true;
}
btnLoad.Text = "Currently Viewing";
btnLoad.Enabled = false;
But it isnt working. Depending on where I put it, its either leaving all the buttons enabled (But still changing its text), or not doing anything at all.
What do I need to do to make this work?
Edit: The code is found here:
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
Which is why I use Button btnLoad = (Button)e.Item.FindControl("btnLoad");.
The method is found in :
switch (e.CommandName)
{
case "LoadDoc":
//Above code
break;
}
Assuming you want that code in the Button's Click-event handler:
protected void Button_Clicked(Object sender, EventArgs e)
{
Button thisButton = (Button) sender;
thisButton.Text = "Currently Viewing";
RepeaterItem item = (RepeaterItem) thisButton.NamingContainer;
IEnumerable<Button> buttons = item.Controls.OfType<Button>();
foreach(var btn in buttons)
{
btn.Enabled = btn != thisButton;
}
}
Try a simple if statement in a loop as below
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Control c in e.Item.Controls)
{
if (b is Button)
{
if(b == btnLoad)
{
b.Enabled = false;
}
else
{
b.Enabled = true;
}
}
}

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