Sending the button name to a method.
-
Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }
-
Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }
the sender is the buttun that sent the object so you can do
button x = (button)sender;
PS please put code in code blocks makes it easier to read!
-
Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }
You already have this information - in the sender parameter. All you need do is cast it to a Button and then use the name from that, e.g.
string name = ((Button)sender).Name;
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }
Hi, on top of that all, you don't have to use the Name property (if the Form got designed with Visual Designer it won't like you change a Control's Name). You could use its Text property instead, or store something in the Tag property. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
-
You already have this information - in the sender parameter. All you need do is cast it to a Button and then use the name from that, e.g.
string name = ((Button)sender).Name;
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
the sender is the buttun that sent the object so you can do
button x = (button)sender;
PS please put code in code blocks makes it easier to read!
-
Hi, on top of that all, you don't have to use the Name property (if the Form got designed with Visual Designer it won't like you change a Control's Name). You could use its Text property instead, or store something in the Tag property. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
Yeah, I prefer to use the Tag too, that way I can associate an object with the control, much cleaner.
-
Hi, I'm doing a calendar like program and I'm using buttons as time slots. And I need to simply call a method that will be called by any button that is clicked inside this time slot and when the method is called it will read the button's name. example btn800Am_clicked - will open timeslot btn900Am_clicked - will open timeslot btn1000Am_clicked - will open timeslot private void timeslot_Click(object sender, EventArgs e) { calculate(x.name); } private void calculate(string name) { messagebox.show(name); }
private void btn0_Click(object sender, EventArgs e) { Button btn = (Button)sender; check(); if (btn.Name == btn0.Name) { txtcalculate.Text = txtcalculate.Text + "0"; } if (btn.Name == btn1.Name) { txtcalculate.Text = txtcalculate.Text + "1"; } if (btn.Name == btn2.Name) { txtcalculate.Text = txtcalculate.Text + "2"; } } //make all the button event to be btn0_Click
Padmanabhan
-
private void btn0_Click(object sender, EventArgs e) { Button btn = (Button)sender; check(); if (btn.Name == btn0.Name) { txtcalculate.Text = txtcalculate.Text + "0"; } if (btn.Name == btn1.Name) { txtcalculate.Text = txtcalculate.Text + "1"; } if (btn.Name == btn2.Name) { txtcalculate.Text = txtcalculate.Text + "2"; } } //make all the button event to be btn0_Click
Padmanabhan