I have created a number of silverlight buttons thus:-
string b = "Button";
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Name = b+i.ToString();
btn.FontSize += 2;
btn.Content = "Click Me " ;
btn.Click += new RoutedEventHandler(btn_Click);
stack.Children.Add(btn);
}
LayoutRoot.Children.Add(stack);
In the button click event I want to get the name of the button that was pressed. I had hoped that
string snd = sender.ToString(); would yield the information but all it gives is System.Windows.Controls.Button. Can anyone please help. Thanks.
You need to cast sender to a button.
public void btn_Click(object sender, EventArgs e)
{
var btn = (Button)sender;
Console.WriteLine(btn.Name);
}
public void btn_Click(object sender, EventArgs e)
{
var b = sender as Button;
if (b != null)
{
var name = b.Name;
// do something with name
}
}
Try this:
(sender as Button).Name
source
Related
The program has a label, two radio buttons, and a set of generated buttons from A-Z. There are two URLs I would like to use that have a text list of names. When You click a lettered button, the program splits the text list and displays them in the label.
The two radio buttons are supposed to switch the URL being used. One URL has male first names, the other url has female first names. However, I am not sure how to get this to work...as I have it now, choosing the radio button does nothing... it will stay on whatever the first URL was.
using System;
using System.Windows.Forms;
using System.Net;
namespace IndexTable
{
public partial class frmIndex : Form
{
public frmIndex()
{
InitializeComponent();
}
private void Button_Click(object sender, EventArgs e)
{
Button btn = sender as Button; // Convert datatype
label1.Text = btn.Text + "\n"; ;
foreach (string c in name)
{
if (c != "" && c.Substring(0, 1) == btn.Text)
{
label1.Text += c + "\n";
//listBox1.Items.Add(c);
}
}
}
string[] name;
private void Form1_Load(object sender, EventArgs e)
{
int top = 10;
int left = 20;
int width = 30;
for (char i = 'A'; i <= 'M'; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
button.Width = width;
button.Text = i.ToString();
this.Controls.Add(button);
button.Click += new System.EventHandler(this.Button_Click);
top += button.Height + 2;
}
left = 50;
top = 10;
for (char i = 'N'; i <= 'Z'; i++)
{
Button button = new Button();
button.Left = left;
button.Top = top;
button.Width = width;
button.Text = i.ToString();
this.Controls.Add(button);
button.Click += new System.EventHandler(this.Button_Click);
top += button.Height + 2;
}
WebClient wc = new WebClient();
string names = wc.DownloadString(url);
name = names.Split('\n');
}
private string url = "http://www.cs.cmu.edu/Groups/AI/areas/nlp/corpora/names/male.txt";
private void btnBoy_CheckedChanged(object sender, EventArgs e)
{
url = "http://scrapmaker.com/data/wordlists/names/male-names.txt";
}
private void btnGirl_CheckedChanged(object sender, EventArgs e)
{
url = "http://scrapmaker.com/data/wordlists/names/female-names.txt";
}
}
}
The CheckedChanged event fires whether the button becomes checked or becomes unchecked. Therefore you need to add a line of code to see what the current check state of the button is before responding:
private void btnBoy_CheckedChanged(object sender, EventArgs e)
{
if (btnBoy.Checked)
url = "http://scrapmaker.com/data/wordlists/names/male-names.txt";
}
EDIT: also you only use the url once, in the form load event, with the line
wc.DownloadString(url)
You'll need to add code to actually use the url again after you change it with the radiobuttons--either in the radiobutton event handler, or the other button event handler for instance.
i tried to do this(mine field) but i cant visible = false any button when im clicked that.
by the way this.visible = false is going to visible form -.-
private void Form1_Load(object sender, EventArgs e)
{
this.Size = new Size(541, 537);
for (int j = 24; j < ClientSize.Height; j += 25)
{
for (int i = 0; i < ClientSize.Width; i += 25)
{
Button btn = new Button();
btn.Width = 25;
btn.Height = 25;
btn.Location = new Point(i, j);
btn.Click += btn_Click;
Controls.Add(btn);
}
}
}
void btn_Click(object sender, EventArgs e)
{
//When i clicked anyone of them that comes be invisible,
}
thx for helping
The control that fired the event is stored in sender - cast it back to a Button.
void btn_Click(object sender, EventArgs e)
{
var button = (Button)sender;
button.Hide();
}
Alternatively, if that's all you're doing in the event, you could define it in a single line like this:
btn.Click += delegate { btn.Hide(); };
I want to attach an event to a button clicked generated at runtime. Till this point I've wrote the code, but can't pass the button's ID to the method. Here is my code
This code does not through any error, another problem is after the click event the controls get washed away. How to prevent this ?
protected void Button1_Click(object sender, EventArgs e)
{
int i = int.Parse(TextBox1.Text);
for (int x = 1; x <= i; x++)
{
Button b = new Button();
b.ID = "btn_" + x.ToString();
b.Text = "btn_" + x.ToString();
b.Click += new System.EventHandler(myEventHandler);
pnlHolder.Controls.Add(b);
}
}
private void myEventHandler(object sender, EventArgs e)
{
txtMain.Text = sender.ToString(); // I want to know which button was pressed
}
try,
txtMain.Text = (sender as Button).Name;
or
txtMain.Text = (sender as Button).Text;
Try this
private void myEventHandler(object sender, EventArgs e)
{
Button b = (Button) sender;
txtMain.Text = b.ID;
//
txtMain.Text = b.Text;
if(b.ID == "button1")
doThis();
else if(b.ID == "button2")
doThat();
}
using System;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
Button bt = new Button();
bt.Text = ""+i;
bt.Click += new EventHandler(bt_Click);
Panel1.Controls.Add(bt);
}
}
public void bt_Click(object sender, EventArgs e)
{
Button selected = sender as Button;
Panel1.Visible = false;
Label lbl = new Label();
lbl.Text = "i am lable";
Panel2.Controls.Add(lbl);
for (int i = 1; i < 30; i++)
{
Button pb = new Button();
pb.Text = selected.Text;
pb.Click += new EventHandler(pb_Click);
Panel2.Controls.Add(pb);
}
}
public void pb_Click(object sender, EventArgs e) // how to trigger it
{
Response.Redirect("http://www.google.com");
}
}
this pb_Click is not trigerring; so guys any idea
Obviously pb_Click won't execute. You are adding controls (buttons) into bt_Click handler will be removed on next submit. You must have to use Page_Load event to add controls dynamically.
You may write/design your code like this: (a trick)
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
Button bt = new Button();
bt.Text = "" + i;
bt.ID = "btn" + i; // Assign unique ID
bt.Click += new EventHandler(bt_Click);
Panel1.Controls.Add(bt);
}
AddButtons();
}
public void bt_Click(object sender, EventArgs e)
{
ViewState["btnId"] = (sender as Button).ID ;
AddButtons();
}
public void AddButtons()
{
if (ViewState["btnId"] == null)
return;
Button selected = Panel1.FindControl(ViewState["btnId"].ToString()) as Button;
Panel1.Visible = false;
Label lbl = new Label();
lbl.Text = "i am lable";
Panel2.Controls.Add(lbl);
for (int i = 1; i < 30; i++)
{
Button pb = new Button();
pb.Text = selected.Text;
pb.Click += new EventHandler(pb_Click);
Panel2.Controls.Add(pb);
}
}
You need to add the button in the page_init method and do not add them in the button click or page_load methods.
Is it possible to create an array of controls? Is there a way to get the index of a control if more than one of the controls in the array share the same event handler?
This is certainly possible to do. Sharing the event handler is fairly easy to do in this case because the Button which raised the event is sent as part of the event args. It will be the sender value and can be cast back to a Button
Here is some sample code
class Form1 : Form {
private Button[] _buttons;
public Form1(int count) {
_buttons = new Button[count];
for ( int i = 0; i < count; i++ ) {
var b = new Button();
b.Text = "Button" + i.ToString()
b.Click += new EventHandler(OnButtonClick);
_buttons[i] = b;
}
}
private void OnButtonClick(object sender, EventArgs e) {
var whichButton = (Button)sender;
...
}
}
Based on Kevins comment:
foreach(Button b in MyForm.Controls.OfType<Button>())
{
b.Click += Button_Click;
}
void Button_Click(object sender, EventArgs e)
{
Button clickedButton = sender as Button;
}