Hi all i am having 2 imagebuttons a gridview and a button. Now if i clicked on Image button i will show a grid. Now under button click i would like to capture which image button was clicked if 1st image button is clicked i would like to some values and if 2nd one is clicked i would like to show another
You can discern which button was pressed by comparing the sender parameter:
void MyButton_Click(object sender, EventArgs e)
{
if (sender == MyButton1)
{
// 1st image button was clicked — some values
}
else if (sender == MyButton2)
{
// 2nd one was clicked — show another
}
}
Can't you create 2 events and one function?
ex:
//Hook both OnClick events to these!
private void OnButton1Click(object sender, EventArgs e) { BeenClicked(button1); }
private void OnButton2Click(object sender, EventArgs e) { BeenClicked(button2); }
private void BeenClicked(Button ClickedButton)
{
if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!");
}
Or alternatively you could use:
//Hook both OnClick events to this!
private void OnButtonClick(object sender, EventArgs e)
{
ClickedButton = (Button)sender;
if(ClickedButton.Text == Button1) Console.WriteLine("Hi to you too!");
}
If I understood you right :)
Related
Hey guys first month student here , I need to handle a button event based on which button was clicked before , any simple ideas ? ..(example I've got a listbox with a list of actors and depending on if I click "new actor(first button)" or "modify actor(second button)" I need to either add a new actor or modify the existing actor's stats with the "add button (third button --> needs to know which button was clicked before)".. ??
You can declare a variable of type Button to save the button clicked before.
Note: use Null-conditional operator ?. to avoid potential exception System.NullReferenceException.
Button btn;
private void btnAdd_Click(object sender, EventArgs e)
{
Console.WriteLine(btn?.Name); // print button name
}
private void btnNew_Click(object sender, EventArgs e)
{
btn = btnNew;
}
private void btnModify_Click(object sender, EventArgs e)
{
btn = btnModify;
}
i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}
I have a button which, when clicked, should uncheck/deselect a radio button which was previously checked.
Just like we clear the text from a textbox when a button is clicked, like this:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
Is there a way to uncheck/deselect a radio button that was already checked?
Simple:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Clear();
radioButton.Checked = false;
}
I have a label working as a button. I would like when I press a button the click event to this label to take action. for example
private void Label1_Click(object sender, EventArgs e)
{
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Now I want when I press this button, the label1 click event to be performed
private void button1_Click(object sender, EventArgs e)
{
// I want when I press this button something like this happens
Label1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e);
}
now if you want to show a message of which control was clicked all in one method do the following
private void label1_Click(object sender, EventArgs e)
{
Control control = (Control)sender;
var name = control.Name;
MessageBox.Show(string.Format("I pressed this {0} and showed me this messagebox",name));
}
Two ways to do this.
First:
private void button1_Click(object sender, EventArgs e)
{
Label1_Click(sender, e); // Just call the Label's click handler
}
Second:
// Bind the Label1_Click handler to the button1 Click event, as they both use the same delegate
button1.Click += new EventHandler(Label1_Click);
With the second approach, note that in C# delegates are multi-cast, so both the button1_Click handler and the Label1_Click handler will be called when the button is clicked, in the order they were bound.
private void button1_Click(object sender, EventArgs e)
{
//What the label click do:
MessageBox.Show("I pressed this label and showed me this messagebox");
}
Is that not easier?
Why do you want to do it ?
I think it would be easier for you to just include the lable click functionality with the button click. Maybe even separate each piece in their own method and call them from the button click. Here is how you'd call another click event.
private void button1_Click(object sender, EventArgs e)
{
label1_Click(sender, e);
}
public class MyLabel:Label
{
public void PerformClick()
{
OnClick(new EventArgs());//InvokeOnClick(this,new EventArgs());
}
}
I've got a form I've created with 4 buttons that will modify a listView, each with their own click event. I'm wondering as a way to avoid adding a method call in each of these buttons if it their is an event available for after a button has finished executing its click event?
The reason for this is so I can update the listView which will in turn update a webBrowser
Here's an example which results in "1" and then "all" being displayed to the user when button 1 is clicked, "2" and then "all" when button 2 is clicked etc etc.
Note: you don't want to do it like this - see comments
public Form1()
{
InitializeComponent();
foreach(var b in Controls.OfType<Button>())
b.Click += new EventHandler(AnyButton_Click);
}
void AnyButton_Click(object sender, EventArgs e)
{
MessageBox.Show("all");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("3");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("2");
}