How to pass button object to function - c#

I have several buttons to click, and all the same function (that I want to create), they just differ by controller's name. For example:
private void markX()
{
buttonName.Text = "X";
buttonName.ForeColor = System.Drawing.Color.Red;
}
How can I pass the button object that that is modified in the function into the function's parameters?

Make it a Click event handler, attach it to each button, and use the sender parameter as the button to change.
void button_Click(Object sender, EventArgs e)
{
var button = sender as Button;
if(button != null)
{
button.Text = "X";
button.ForeColor = System.Drawing.Color.Red;
}
}

You don't need to pass the name of the button, you just need to pass an object of type button as an argument to your method.
private void markX(Button b)
{
b.Text = "Text";
b.Foreground = System.Drawing.Color.Red;
}

Use "sender":
private void Button_click(object sender, EventArgs e)
{
((Button)sender).Text = "X";
}
sender holds instance of event caller.

The click handler for the button click event has the following objects.
Object sender
EventArgs e
This will give you the name of the Button that was clicked, and just pass that to your function.
((Button)sender).Tag
Here is some sample code
private void Button_Clicked(Object sender, EventArgs e)
{
string name = ((Button)sender).Tag;
markX(name);
}

Related

I got a multiple buttons event handler here But I need to target only one button if it was clicked how do I do that?

private void btnCodeAkas_Click(object sender, EventArgs e)
{
tmrTimerAkas.Start();
tmrTimerTwoAkas.Start();
}
But I need to target only one button if it was clicked how do I do that?
well you can cast your sender to Control object so you can get the name of the button then you write your conditional logic.
private void button1_Click(object sender, EventArgs e)
{
var control = sender as Control;
if (control.Name == "button1")
{
Console.WriteLine($"Clicked {control.Name} button");
}
else if (((Control)sender).Name == "button2")
{
Console.WriteLine($"Clicked {control.Name} button");
}
}

Is there any way to use a button sender to send the text (on a button) directly into a listbox

i have tried 2 solutions but neither of them work, the first on is to directly send it into the listbox but neither of them work
1.To send it directly into the listbox
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
lbItems = b.Text;
}
2.The other method i tried was to send it into a a texbox first then send the the string from the texbox into the listbox, but the code breks after one button
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
lbItems = b.Text;
if (txtPre.Text == " ")
{
}
else
{
lbItems.Items.Add(txtPre.Text);
txtPre.Clear();
}
}
lbItems.Items.Add((sender as Button).Text);

Programmatically add Click EventHandler to Button

I'm creating Buttons programmatically with a method and am wanting to attach a Click event handler. However, that data currently comes from a string parameter which can't be used with += RoutedEventHandler.
public Button CreateButton(string Display, string Name, string ClickEventHandler)
{
Button Btn = new Button
{
Content = Display,
Name = "Btn_" + Name
};
Btn.Click += new RoutedEventHandler(ClickEventHandler);
return Btn;
}
void Btn_save_Click(object sender, RoutedEventArgs e)
{
throw new NotImplementedException();
}
// later
Button MyButton = CreateButton("Save", "save", "Btn_save_Click");
Error is RoutedEventHandler expects a Method and not a String. Is there a different approach to programmatically binding events that allows this sort of behaviour?
Thanks
From what I understand you wish to pass the method that should be executed when Click event is triggered. You could do something along the lines of:
Button button = CreateButton("Save", "save", (s, e) => SomeOnClickEvent(s, e));
Button button2 = CreateButton("Create", "create", (s, e) => SomeOtherOnClickEvent(s, e));
public Button CreateButton(string display, string name, Action<object, EventArgs> click)
{
Button b = new Button()
{
Content = display,
Name = $"Btn_{name}"
};
b.Click += new EventHandler(click);
return b;
}
void SomeOnClickEvent(object sender, EventArgs e)
{
}
void SomeOtherOnClickEvent(object sender, EventArgs e)
{
}
I am not entirely sure what you are trying to accomplish with this.
Here is an example of how to create an event at run time.
public void CreateButton()
{
Button Btn = new Button();
Btn.Click += new EventHandler(btn_Clicked);
}
private void btn_Clicked(object sender, EventArgs e)
{
// Your Logic here
}

Indirect reference to a button

I'm having this code:
private void b9_Click(object sender, EventArgs e)
{
b9.Enabled = false;
color = 8;
}
The problem is that i'm having a lot of buttons for disabling. Is there a chance i can use something like:
this.Enabled=false;
Probably that is what you want
private void OnClick(object sender, EventArgs e)
{
if( sender is Button )
{
Button button = (Button)sender;
button.Enabled = false;
}
}
Use this routine for every button you need to disable on click.
It is known as single event handler for multiple controls. Just put following event handler for your buttons as many as you like.
public void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.IsEnable = false;
// If you want to access text in the button
... = button.Content as object;
}
private void OnClick(object sender, EventArgs e)
{
Button btn = sender as Button; // if sender is not a Button, btn will be null
if (btn != null)
{
btn.Enabled = false;
}
}
If you want to apply the same behaviour to any clickable control, you can use Control class instead of Button. Button inherits from Control and the property Enabled is defined in Control class.
private void OnClick(object sender, EventArgs e)
{
Control ctrl = sender as Control; // if sender is not a Control, ctrl will be null
if (ctrl != null)
{
ctrl .Enabled = false;
}
}
Also, if you want to go one step further, you can create a method that disables the clicked control. Something like this:
private void DisableControl(object sender)
{
Control ctrl = sender as Control;
if (ctrl != null)
{
ctrl.Enabled = false;
}
}
Then, you can call this method from the Click even handler like this:
private void OnClick(object sender, EventArgs e)
{
DisableControl(sender);
}

Get instance of control whose event I am in

Is there a way to get an instance of the control whose event I am in?
private void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox1.Text = "hi";
ThisControl.Text = "hi";
}
Sort of so that these two lines would do the same thing? Like the "This" keyword but for the event control rather than the class.
The object sender parameter is a reference to the control that fired the event. Therefore, you could do something along the lines of:
private void textBox1_TextChanged(object sender, EventArgs e)
{
((TextBox)sender).Text = "hi";
// Or
TextBox txtBox = sender as TextBox;
txtBox.Text = "hi";
}

Categories

Resources