I have written a method trial for button btnTrial1:
public void trial(object sender, EventArgs e, Button btn, TextBox txt, Label lbl)
{
}
In my application, i am generating more buttons and textboxes and labels dynamically through code and naming them sequentially like btnTrial2, txtTrial2, lblTrial2 then btnTrial3, txtTrial3, lblTrial3 and so on. Now i want to set trial as EventHandler for btnTrial2 then for btnTrial3 and so on.
So now when i call the method trial from btnTrial1, my parameters should be:
Public void (sender, e, btnTrail1, txtTrial1, lblTrial1)
But when i call the method trial from btnTrial2, my parameters should be:
Public void (sender, e, btnTrail2, txtTrial2, lblTrial2)
and so on...
btnTrial1.YourEvent += (sender, args) => trial(sender, args,
btnTrial1, txtTrial1, lblTrial1);
btnTrial2.YourEvent += (sender, args) => trial(sender, args,
btnTrial2, txtTrial2, lblTrial2);
You mention "generating them dynamically" - that is fine, but if you are in a loop you will also need to watch out for the infamous "captured variable / loop" problem - notably, the variables "captured" must be inside the loop; for example:
for(int i = 1 ; i <= 10 ; i++) {
var btnTrial = ...
var txtTrial = ...
var lblTrial = ...
btnTrial.YourEvent += (sender, args) => trial(sender, args,
btnTrial, txtTrial, lblTrial);
}
(if, for example, btnTrial was declared outside the loop, bad things would happen)
Why not make your trial function
public void trial(object sender, EventArgs e)
{
}
And then based on the sender, use different controls:
public void trial(object sender, EventArgs e)
{
Button btn;
TextBox txt;
Label lbl;
if (sender == btnTrial1){
btn = btnTrail1;
txt = txtTrial1;
lbl = lblTrial1;
}
if (sender == btnTrial2){
btn = btnTrail2;
txt = txtTrial2;
lbl = lblTrial2;
}
if (sender == btnTrial3){
btn = btnTrail3;
txt = txtTrial3;
lbl = lblTrial3;
}
//rest of the method
}
Two suggestions:
Refactor trial() to return an EventHandler (or ideally EventHandler<T>):
public EventHandler GetTrialEventHandler(Button btn, TextBox txt, Label lbl)
{
return (sender, args) =>
{
// Do something with btn, txt, lbl
};
}
Rather than name your form elements btnTrial1, btnTrial2, etc, why not just make lists of the elements (or a list of sets of Button+TextBox+Label)? Then you just have to enumerate over the list(s) to set up your event handlers, rather than hard-code for each.
To each button You can add:
this.button.Click += new System.EventHandler(this.button_Click);
method like this:
private void button_Click(object sender, EventArgs e)
{
String name = (sender as Control).Name;
int number = Int32.Parse(name.Substring(name.Length-1));
trial(sender, e, this.Controls["btnTrial"+number], this.Controls["txtTrial"+number], Controls["lblTrial"+number]);
}
Related
by default, we add events in this way:
item1.Click += Click1;
item2.Click += Click2;
.....
private void Click1(){
.....
}
private void Click2(){
}
however, is there a way to combine it into one method, like:
item1.Click += Click(1);
item2.Click += Click(2);
....
private void Click(int num){
if(num==1){
....
}
else if(num==2){
....
}
}
(btw, newbie in C#)
Event handlers have sender argument usually which can be used to identify the actual sender:
void Click(object sender, EventArgs e)
{
var btn= (Button)sender; // you can access properties which you might have set to identify which button it is
.....
}
button1.Click+=Click;
button2.Click+=Click;
button3.Click+=Click;
button4.Click+=Click;
It this is not sufficient and you want to pass some extra data to the handler, you could use a lambda :
void Click(object sender, EventArgs e, int extraInfo)
{
var btn= (Button)sender;
.....
}
button1.Click+=(s, e) => Click(s, e, 1);
button2.Click+=(s, e) => Click(s, e, 2);
button3.Click+=(s, e) => Click(s, e, 3);
button4.Click+=(s, e) => Click(s, e, 4);
Unsubscribing will be an issue with anonymous methods but if the handler owner and the control have the same lifespan inside a page then it might not matter as they will get GC-ed together anyway
You can also compare the sender by reference:
item1.Click += Click;
item2.Click += Click;
....
void Click(object sender, EventArgs e)
{
Control c = sender as Control; // c = null if sender is not Control
if (sender == item1)
Debug.Print(c.Name + ", " + c.Text + ", " + c.Tag);
else if (sender == item2)
Debug.Print(c.Name + ", " + c.Text + ", " + c.Tag);
}
That's how events always worked. You can use the same event handler for multiple events as long as the signature is the same. The base EventHandler type accepts a sender which is the object that fires the event and an EventArgs-derived parameter which is probided when the code fires the event.
For example, you can have a single click handler for multiple buttons :
void myHandler(object sender,EventArgs e)
{
var theButton=(Button)sender;
.....
}
button1.Click+=myHandler;
button2.Click+=myHandler;
button3.Click+=myHandler;
button4.Click+=myHandler;
Imagine you wanted to create a calculator with 10 digits. You could read the Text or Tag property of the sender to retrieve the digit :
void myDigitsHandler(object sender,EventArgs e)
{
var theButton=(Button)sender;
//You could store a number to the tag
int digitFromTag=(int)theButton.Tag;
//Or you could parse the text
int digitFromText=int.Parse(theButton.Text);
.....
}
I am creating new picture boxes and their click event handlers programmatically. But I can't use them seperately because I can't name them properly. I need to name the event handlers like box1_click, box2_click...
What is the simplest way to name new event handlers separately numbered with an integer?
My attempt:
for(i=1; i<11; i++)
{
boxes[i] = new PictureBox();
boxes[i].Name = "box" + i;
boxes[i].Click += new EventHandler( ??? );
}
There are multiple ways to do that.
You could either add the event handler as a lambda expression that calls each specific handler by reflection like this (make sure to define your handlers as public methods):
MethodInfo method = this.GetType().GetMethod("B_Click" + i.ToString());
boxes[i].Click += (o,e) =>
{
if (null != method)
{
method.Invoke(this, new object[2] { o, e });
}
};
...
public void B_Click1(object sender, EventArgs e)
public void B_Click2(object sender, EventArgs e)
etc...
Another option is to create a delegate event handler using Delegate.CreateDelegate instead of a lambda expression like this:
MethodInfo method = this.GetType().GetMethod("B_Click" + i.ToString());
EventHandler handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, method);
boxes[i].Click += handler;
...
public void B_Click1(object sender, EventArgs e)
public void B_Click2(object sender, EventArgs e)
etc...
Probably the best option is to define one handler for all PictureBoxes and cast sender object as the clicked PictureBox like this:
boxes[i].Click += B_Click;
...
private void B_Click(object sender, EventArgs e)
{
PictureBox clickedPictureBox = sender as PictureBox;
if (clickedPictureBox != null)
{
string name = clickedPictureBox.Name; // for example get picture box name
}
}
You should always use the same event handler. Inside the handler you would decide upon the sender parameter what has to be done.
My code makes 5 labels appear with a random .Left location, you can see it.
I want the particular label to disappear when I click on it, but I don't know how to tell it to my click void.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label [] kubeliai = new Label [5];
int poz = 100;
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < kubeliai.Length; i++)
{
kubeliai[i] = new Label();
Controls.Add(kubeliai[i]);
Random pos = new Random();
kubeliai[i].Top = 50;
kubeliai[i].Left = poz;
poz += pos.Next(50, 200);
kubeliai[i].BackColor = Color.Red;
kubeliai[i].Height = 20;
kubeliai[i].Width = 20;
kubeliai[i].Click += new EventHandler(kubelio_clickas);
}
}
void kubelio_clickas (object sender, EventArgs e)
{
}
}
The instance of "clicked" label is in sender parameter:
void kubelio_clickas (object sender, EventArgs e)
{
Label clickedLabel = sender as Label;
if (clickedLabel != null) {
clickedLabel.Visible = false;
}
}
Because in .NET Event Handlers by default use object as type of sender you have to cast it to Label first.
I want the particular label to disappear when I click on it
Just set the label's .Visible property to false:
void kubelio_clickas (object sender, EventArgs e)
{
if (sender is Label)
((Label)sender).Visible = false;
}
The object sender is a reference to the object which fired the event. So basically, the sender is the object you are looking for.
You just need to set it invisible:
((Label)sender).Visible = false;
So I create dynamic labels in a loop, labels for a list of file folders in a directory.
i want, when you click on the label, the files within the label will display in a listbox. but i cannot get my event handler working, is it necessary to give my label a name as shown, i feel like i need the name for the event, but if the name is dynamic, the event name needs to be too and i cannot do that. also, i will need access to the labels properties within the event, so thats why i created an overloaded method, but regardless, clicking on the label does not do either of my event handlers. please advise, i'd appreciate it. here is whats in my loop and my event handlers
string str = lstMovieFolders[i];
Label lbl = new Label();
lbl.Name = "lbl" + str;
lbl.Location = new Point(25, intStartPoint);
lbl.Text = str;
lbl.Size = new Size(x, y);
lbl.Click += new EventHandler(lbl_Click);
grp.Controls.Add(lbl);
intStartPoint += 30;
public static void lbl_Click(object sender, EventArgs e)
{
MessageBox.Show("HELLOS");
}
public static void lbl_Click(object sender, EventArgs e, Label lbl)
{
MessageBox.Show("HELLO");
}
You can use sender parameter to get the current Label that triggers the event.You do not need an overload
public static void lbl_Click(object sender, EventArgs e)
{
var label = sender as Label;
if(label != null)
{
string text = label.Text;
}
}
I managed to create textboxes that are created at runtime on every button click. I want the text from textboxes to disappear when I click on them. I know how to create events, but not for dynamically created textboxes.
How would I wire this up to my new textboxes?
private void buttonClear_Text(object sender, EventArgs e)
{
myText.Text = "";
}
This is how you assign the event handler for every newly created textbox :
myTextbox.Click += new System.EventHandler(buttonClear_Text);
The sender parameter here should be the textbox which sent the even you will need to cast it to the correct control type and set the text as normal
if (sender is TextBox) {
((TextBox)sender).Text = "";
}
To register the event to the textbox
myText.Click += new System.EventHandler(buttonClear_Text);
Your question isn't very clear, but I suspect you just need to use the sender parameter:
private void buttonClear_Text(object sender, EventArgs e)
{
TextBox textBox = (TextBox) sender;
textBox.Text = "";
}
(The name of the method isn't particularly clear here, but as the question isn't either, I wasn't able to suggest a better one...)
when you create the textBoxObj:
RoutedEventHandler reh = new RoutedEventHandler(buttonClear_Text);
textBoxObj.Click += reh;
and I think (not 100% sure) you have to change the listener to
private void buttonClear_Text(object sender, RoutedEventArgs e)
{
...
}
I guess the OP wants to clear all the text from the created textBoxes
private void buttonClear_Text(object sender, EventArgs e)
{
ClearSpace(this);
}
public static void ClearSpace(Control control)
{
foreach (var c in control.Controls.OfType<TextBox>())
{
(c).Clear();
if (c.HasChildren)
ClearSpace(c);
}
}
This should do the job :
private void button2_Click(object sender, EventArgs e)
{
Button btn = new Button();
this.Controls.Add(btn);
// adtionally set the button location & position
//register the click handler
btn.Click += OnClickOfDynamicButton;
}
private void OnClickOfDynamicButton(object sender, EventArgs eventArgs)
{
//since you dont not need to know which of the created button is click, you just need the text to be ""
((Button) sender).Text = string.Empty;
}