I'd like to distinguish each of the Event handler.
(I have only one in my code below. I mean dynamic handler will be the best but, any kind of workarounds will be fine too.)
Please help me.
Thanks !
List<Button> VuttonList = new List<Button>();
private void Form1_Load(object sender, EventArgs e)
{
Button Vutton;
int Kount = 10;
for (int i = 0; i < Kount ; i++)
{
Vutton = new Button();
Vutton.Text = ( i + 1 ).ToString() ;
Vutton.Location = new Point(10, 24 * ( i + 1 ) );
Controls.Add( Vutton );
Vutton.Click += new EventHandler(Kommon);
VuttonList.Add( Vutton );
}
}
private void Kommon(object sender, EventArgs e)
{
MessageBox.Show( sender.ToString() );
}
One event handler is enough, you can cast the sender to Button and this way you know which button has been clicked. Also you can set Name property of buttons when you create them or assign Tag property of them and use it later.
for (int i = 0; i < Kount ; i++)
{
Vutton = new Button();
//...set properties
//Also add Name:
Vutton.Name = string.Format("Vutton{0}", i);
//Also you can add Tag
Vutton.Tag = i;
Controls.Add( Vutton );
Vutton.Click += new EventHandler(Kommon);
//... other stuff
}
Then you can use properties of button this way:
private void Kommon(object sender, EventArgs e)
{
var button = sender as Button;
//You can use button.Name or (int)button.Tag and ...
MessageBox.Show(button.Name);
}
Also to layout your buttons, you can use a FlowLayoutPanel or a TableLayoutPanel.
Related
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 have tried creating textboxes dynamically using lists. All i need now is, how can i reset all text boxes that i have created by hitting a reset button.
The following is my code:
public void button2_Click_1(object sender, EventArgs e)
{
int number = Convert.ToInt32(textBox2.Text);
List<TextBox> inputTextBoxes;
inputTextBoxes = new List<TextBox>();
for (int i = 1; i <= number; i++)
{
Label labelInput = new Label();
TextBox textBoxNewInput = new TextBox();
labelInput.Text = "Activity No: " + i;
labelInput.Location = new System.Drawing.Point(30, textBox2.Bottom + (i * 40));
labelInput.AutoSize = true;
textBoxNewInput.Location = new System.Drawing.Point(labelInput.Width+60, labelInput.Top - 3);
inputTextBoxes.Add(textBoxNewInput);
this.Controls.Add(labelInput);
this.Controls.Add(textBoxNewInput);
}
}
The answer is:
private void resetButton_Click(object sender,EventArgs e)
{
for (int i = 0; i <= inputTextBoxes.Length; i++)
{
inputTextBoxes[i].Text = "";
}
}
And you should declare inputTextBoxes is a class member which is same class' of buttons.
Move the following line outside the event handler function (outside the function but inside the class)
List<TextBox> inputTextBoxes;
Then on the reset button click
private void btnReset_Click(object sender, EventArgs e)
{
foreach(TextBox txt in inputTextBoxes)
{
this.Controls.Remove(txt);
}
inputTextBoxes.Clear();
}
Edit: Corrected the class type in foreach loop (from Button to TextBox)
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
I create array:
TextBox[] textarray = new TextBox[100];
Then in cycle set this params, all items array situated in uniformGrid1
textarray[i] = new TextBox();
textarray[i].Height = 30;
textarray[i].Width = 50;
uniformGrid1.Children.Add(textarray[i]);
How create events Click or DoubleClick that all items array?
Sorry my English.
public static void blah()
{
TextBox[] textarray = new TextBox[100];
List<TextBox> textBoxList = new List<TextBox>();
for (int i = 0; i < textarray.Length; i++)
{
textarray[i] = new TextBox();
textarray[i].Height = 30;
textarray[i].Width = 50;
// events registration
textarray[i].Click +=
new EventHandler(TextBoxFromArray_Click);
textarray[i].DoubleClick +=
new EventHandler(TextBoxFromArray_DoubleClick);
}
}
static void TextBoxFromArray_Click(object sender, EventArgs e)
{
// implement Event OnClick Here
}
static void TextBoxFromArray_DoubleClick(object sender, EventArgs e)
{
// implement Event OnDoubleClick Here
}
EDIT:
A better / recommended way of event registration as per #Aaronaugh: advice:
textarray[i].Click += TextBoxFromArray_Click;
textarray[i].DoubleClick += TextBoxFromArray_DoubleClick;
Just add in your click or double click event handler. For example, to trap double click events:
textarray[i] = new TextBox();
textarray[i].Height = 30;
textarray[i].Width = 50;
textarray[i].MouseDoubleClick += this.OnMouseDoubleClick;
uniformGrid1.Children.Add(textarray[i]);
For the above to work, you class will need a method like:
void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Do something
}
Create a click event handler and then use it to subscribe to the click events of your textboxes, like so:
textarray[i].Click += new EventHandler(textbox_Click);
...
void textbox_Click(object sender, EventArgs e)
{
// do something
}
If the actions you want to take are the same for each textbox, then one click handler will suffice.
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;
}