I want to put on existing button new event handle. I have 9 buttons created in form and want to display each name when is clicked on it, but don't want to put on every button message manually.
How to accomplish that?
You can use the sender propperty of the EventHandler in order to do that. This example uses winforms buttons:
SomeButtonClicked(object sender, EventArgs e)
{
var button = sender as Button;
MessageBox.Show(button.Name);
}
And then you can add this event to multiple buttons i.e.:
button1.Click += new System.EventHandler(SomeButtonClicked);
button2.Click += new System.EventHandler(SomeButtonClicked);
button3.Click += new System.EventHandler(SomeButtonClicked);
You can code something like this
private void MainForm_Load(object sender, EventArgs e) {
...
// Assigning on click event, assuming that all your buttons are on the MainForm
foreach (Control ctrl in Controls) {
Button btn = ctrl as Button;
if (!Object.ReferenceEquals(null, btn))
btn.Click += onButtonClick;
}
...
// On click itself
private void onButtonClick(Object sender, EventArgs e) {
Button btn = sender as Button;
String name = btn.Name; // <- Or whichever property of the button you want
}
Related
I am creating one button on a page dynamically. Now I want to use the button click event on that button.
How can I do this in C# ASP.NET?
Button button = new Button();
button.Click += (s,e) => { your code; };
//button.Click += new EventHandler(button_Click);
container.Controls.Add(button);
//protected void button_Click (object sender, EventArgs e) { }
The easier one for newbies:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
Simply add the eventhandler to the button when creating it.
button.Click += new EventHandler(this.button_Click);
void button_Click(object sender, System.EventArgs e)
{
//your stuff...
}
It is much easier to do:
Button button = new Button();
button.Click += delegate
{
// Your code
};
You can create button in a simple way, such as:
Button button = new Button();
button.Click += new EventHandler(button_Click);
protected void button_Click (object sender, EventArgs e)
{
Button button = sender as Button;
// identify which button was clicked and perform necessary actions
}
But event probably will not fire, because the element/elements must be recreated at every postback or you will lose the event handler.
I tried this solution that verify that ViewState is already Generated and recreate elements at every postback,
for example, imagine you create your button on an event click:
protected void Button_Click(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) != "true")
{
CreateDynamicElements();
}
}
on postback, for example on page load, you should do this:
protected void Page_Load(object sender, EventArgs e)
{
if (Convert.ToString(ViewState["Generated"]) == "true") {
CreateDynamicElements();
}
}
In CreateDynamicElements() you can put all the elements you need, such as your button.
This worked very well for me.
public void CreateDynamicElements(){
Button button = new Button();
button.Click += new EventHandler(button_Click);
}
Let's say you have 25 objects and want one process to handle any one objects click event. You could write 25 delegates or use a loop to handle the click event.
public form1()
{
foreach (Panel pl in Container.Components)
{
pl.Click += Panel_Click;
}
}
private void Panel_Click(object sender, EventArgs e)
{
// Process the panel clicks here
int index = Panels.FindIndex(a => a == sender);
...
}
I have a panel where I have a lot of buttons. Is there a way I can check if any of those buttons are clicked, and if it is, change the text from that specific button?
I've been looking around and I've got till now is this
foreach (Control button in panel1.Controls)
{
if ( button.Click == ??? ) //I can't use '==',
//but I don't know what to do here
{
//changing the text would happen here
}
}
Can anyone help me out here?
You could let all buttons use the same click-event handler:
protected void Button_Clicked(object sender, EventArgs e)
{
((Button) sender).Text = "Insert Text Here";
}
In your constructor:
this.Button1.Click += new System.EventHandler(this.Button_Clicked);
this.Button2.Click += new System.EventHandler(this.Button_Clicked);
The easiest thing you can do is to attach the same event handler to all of your Buttons Click event inside of your Panel.
var buttons = panel1.Controls.OfType<Button>();
foreach(var btn in buttons)
btn.Click += Button_Click;
private void Button_Click(object sender, EventArgs e)
{
var btn = sender as Button;
if(btn != null) btn.Text = "Something else";
}
Or you can do it with lambda statement instead of declaring separate method:
var buttons = panel1.Controls.OfType<Button>();
foreach(var btn in buttons)
btn.Click += (s,e) => ((Button)s).Text = "Clicked";
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;
}
On a C# Winform application. Imagine that I have a Panel with some labels inside.
How can I listen all click events from this labels in Panel click event?
You can do this programmatically by calling the following code in your Form_Load event handler or any other suitable event handler.
foreach (Label label in panel1.Controls.OfType<Label>())
{
label.Click += LabelOnClick;
}
And then perform your operations in the event handler:
private void LabelOnClick(object sender, EventArgs eventArgs)
{
MessageBox.Show("Label In Panel Clicked");
}
Why not add a extra event handler to all the Control of Type Button within a Panel?
Sample Code :
private void SetupButtonClickListenerForPanel1()
{
panel1.Click += ListenForAllButtonClickOnPanel1;
foreach (Control control in panel1.Controls)
{
var tb = control as Button;
if (tb != null)
{
tb.Click += ListenForAllButtonClickOnPanel1;
}
}
}
private void ListenForAllButtonClickOnPanel1(object sender, EventArgs eventArgs)
{
//
Button tb = (Button) sender; // casting will fail if click is on Panel1 itself!
MessageBox.Show(tb.Name);
}
Linq way to add Handler :
private void SetupButtonClickListenerForPanel1()
{
panel1.Click += ListenForAllButtonClickOnPanel1;
foreach (var tb in panel1.Controls.OfType<Button>())
{
tb.Click += ListenForAllButtonClickOnPanel1;
}
}
If your Labels are children of the panel, a listener for the Click Event on the panel will listen all non redefined listeners of his children.
Example Code:
{
//Form init
panel1.Click += new System.EventHandler(this.panel1_Click);
....
}
private void panel1_Click(object sender, EventArgs e)
{
//The clicked label will be sender.
Label l = (Label) sender; //Should be a safe cast. Will crash if sender is not a label
}
At my program i dynamicly add Buttons to my form
{
...
Button bt = new Button();
bt.Text = "bla bla";
bt.MouseClick += new MouseEventHandler(bt_MouseClick);
myPanel.Controls.Add(bt);
...
}
void bt_MouseClick(object sender, MouseEventArgs e)
{
TabPage _tab = new TabPage();
_tab.Text = ??? // I want to get the Button's text ! this.Text returns me the
//main form.Text
}
How can access my dynamic Buttons properties ? How can I understand whick button is
clicked either getting its text.
Thanks.
void bt_MouseClick(object sender, MouseEventArgs e)
{
TabPage _tab = new TabPage();
_tab.Text = ((Button)sender).Text;
}
When an EventHandler delegate is invoked, the sender parameter is the component that raised the event, and the e parameter is a subclass of EventArgs that provides any additional component/event specific information for the event.
Therefore you can establish which button the event fired on by casting the sender parameter to Button:
void bt_MouseClick(object sender, MouseEventArgs e)
{
var button = (Button)sender;
TabPage _tab = new TabPage();
_tab.Text = button.Text;
}