one Click event for multiple buttons with Text property - c#

I want to make a click event for a bunch of buttons. The problem is that I want to use the button's Text, and pass it to a function. Now the click event is passed a object sender. When I tried changing that to Button sender, it gave errors. But I don't know how else I can work with the senders Text.
Here is the normal code, which gave a single error:
private void guess_Click(object sender, EventArgs e)
{
guess(sender.Text);
}
I changed it to this, which gave errors:
private void guess_Click(Button sender, EventArgs e)
{
guess(sender.Text);
}
Question:
How can I work with the Button's Text property within this click event, which is a single click_event for multiple buttons?

Step 1: You need to subscribe to the Button Click event of all your buttons to the same EventHandler. so that button click on all your Buttons will fire the same `Event Handler.
Step 2: You need to cast the object sender into Button and then access its Text property to get the Button Text.
Try This:
button1.Click += new System.EventHandler(MyButtonClick);
button2.Click += new System.EventHandler(MyButtonClick);
button3.Click += new System.EventHandler(MyButtonClick);
private void MyButtonClick(object sender, EventArgs e)
{
Button btnClick = (Button)sender ;
guess(btnClick.Text);
}

Cast sender to type button.
Example:
private void guess_Click(object sender, EventArgs e)
{
guess(((Button)sender).Text);
}

You need to cast the sender object to the Button type and use that:
private void guess_Click(object sender, EventArgs e)
{
Button senderBtn = senderBtn as Button;
if(senderBtn != null)
{
guess(senderBtn.Text);
}
}

Related

How to make buttons display their text in a label using one click event

Is it possible to have a click event that displays the text of buttons in a label in c#.
I want to write single code that will work for:
public button1_Click(Object sender, EventArgs e){
label1.Text = button1.Text;
}
public button2_Click(Object sender, EventArgs e){
label2.Text = button2.Text;
}
Is this what you are looking for?
On a button click, you can handle setting text as below
you can add two buttons. Have same click events for both buttons.
Take help of parameter sender to get which button is clicked.
code (sender as Button) gives you all the details of clicked button.
private void button1_Click(object sender, EventArgs e)
{
this.label1.Text = (sender as Button).Text;
}
1) Add a Button and a Label to the blank Form in a new project.
2) Double-click the Button.
This will generate the following Click event method...
private void button1_Click(object sender, EventArgs e) {
}
3) Put the following line of code in the button1_Click method body:
label1.Text = button1.Text;
4) Profit.

How to use a single button click event to call a method with different parameters

I have multiple button click events:
private void button1_Click(object sender, EventArgs e)
{
Procedure(1);
}
private void button16_Click(object sender, EventArgs e)
{
Procedure(16);
}
However, I want to achieve something like this:
private void button[i]_Click(object sender, EventArgs e)
{
Procedure(i);
}
In Winforms, there is a call to InitializeComponent() in the class constructor.
In that method (it will take you to the form designer if you put the mouse cursor on the method and right click > goto definition or F12) you will see how events are hooked up:
button1_Click += button1_Click...
You can simply subscribe the buttons click event to your Procedure method.
button1_Click += CallToProcedure;
How do you work out which button was clicking then? You simply get it off the sender argument in the parameter:
private void CallToProcedure(object sender, EventArgs e)
{
Button btn = sender as Button;
int i = Convert.ToInt32(btn.Name.Replace("button", string.Empty));
Procedure(i);
}
You can store the parameter you're passing to your button (i in your example) in the Button's Tag property then use something like this:
var senderButton = sender as Button;
if (sender != null)
{
Procedure((int) senderButton.Tag);
}
If you can rely on the naming, then simple:
private void button_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
int i = Convert.ToInt32(btn.Name.Replace("button", ""));
Procedure(i);
}
use button.Tag property, is designed for such cases.
read is doc / MSDN

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.

Is there a better way to close buttons on click?

I'm a beginner and have an assignment in which I must program the game of NIM. I begin with 15 "tokens" and at each turn a maximum of three can be removed, or "hidden". So far I am hiding these tokens on click by doing the following.
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
I simply copied and pasted that multiple times and changed the button numbers so that my buttons will close on click. This might be obvious, but is there a more efficient way to do this, instead of having 15 button close methods?
You can use the same click event for every single button, and make use of the sender object, casting it to Button:
private void buttonsToClose_Click(object sender, EventArgs e)
{
((Button)sender).Visible = false;
}
Then just add that handler to every single button you want to close itself on click.
Note, though, that this will throw an InvalidCastException if you or anyone else uses this handler on an object that is not a Button, so if you're actually going to use this code I would add some sort of conditional to check the real type of the sender.
Additionally, you could reuse this for any Control object by casting sender to Control instead, given that Button inherits from Control, and all Control objects have the Visible property. Here's an example, with a conditional to guard against an invalid cast:
private void controlToMakeInvisible_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Control))
{
((Control)sender).Visible = false;
}
}
A final note - it seems from your post like you may have a slight misunderstanding about the way events are created and wired in with objects in Windows Forms. If you go into the Designer, add a click event, and see it pop into your Form code as follows:
private void button1_Click(object sender, EventArgs e)
the name of this method has no bearing on its function. The button1 part of button1_Click doesn't actually have any logical linkage with the Button button1 - it's just the default name assigned by the Designer. The actual assignment of the method button1_Click to the Button.Click event is auto-generated into your Form's Designer.cs method.
The point of this is that if you copy and paste button1_Click and change every incidence of button1 with button2, like so:
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
it's not going to fire when button2 gets clicked. In actual fact, it's never going to fire at all, because the method hasn't actually been connected to any controls/events.
just call your event in a foreach loop.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}
void button_Click(object sender, EventArgs e)
{
((Control) sender).Visible = false;
}
if you change:
Controls.OfType<Button>()
to
Controls.OfType<Control>()
it will set visible to false for any Control. so you can control what item you want the event to be raised for easily.
OfType summary: Filters the elements of an IEnumerable based on a specified type.

Dynamically create buttons and add events to it

I need to dynamically create buttons (one for loop) and add "onClick" and "doubuleClick" events on it.
I did it like this:
Button bt = new Button();
bt.Click += bt_Click;
bt.DoubleClick += bt_DoubleClick;
private void bt_Click(object sender, EventArgs e)
{
label1.Text = this.Text;
}
private void bt_DoubleClick(object sender, EventArgs e)
{
//some code
}
First: My "bt_Click" method gets "main form" text in "label1". In debugger I see that sender is a button. What is wrong with it?
Second: My "bt_DoubleClick" event do not react at all, am I doing something wrong here?
Any help is appreciated.
You should cast sender to Button to get the bt.Text:
Button bt = new Button();
bt.Click += bt_Click;
bt.Text = "click me";
bt.Location = new Point(100,100);
this.Controls.Add(bt);
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (sender as Button).Text;
}
Buttons doesn't react to double click event. You can read it here in detail.
In response to the first question, if I understand you correctly, in this.Text, this refers to the form because the method bt_Click is a member of the Main Form class. I think you might have meant to do:
private void bt_Click(object sender, EventArgs e)
{
label1.Text = (Button)sender.Text;
}
Second: Is this just a case of the bt_Click handler firing twice?
The easiest way to do this it is to use "datagrid".
Datagread has the great support for all events and for organization of items (image, text and so on).
I have made "save" or "open" dialog form to browse content from remote SFTP server, very easy with datagrad, but I had a problem to do it with buttons or labels.

Categories

Resources