This question already has answers here:
How do I handle click events on multiple similar buttons in WPF?
(3 answers)
Click event for button in loop C# WPF
(3 answers)
How would you know which element of an ItemsControl sends an event in MVVM?
(5 answers)
How to find out which button I pressed?
(1 answer)
Closed 5 years ago.
On WPF, if I have only one button click event shared for two or more (52 being more precise) is there a way to distinguish which button the event come from?
private void Button_Card_Click(object sender, RoutedEventArgs e)
{
// for testing
// it works for each button, but which one has been clicked?
MessageBox.Show("Clicked");
}
First button object with event set up
Second button object with event set up
sender should be the clicked button, but also look at RoutedEventArgs.Source and .OriginalSource
I would also look into using Commands and CommandParameter to indicate which was clicked.
Related
This question already has answers here:
C# - Add button click events using code
(3 answers)
Closed 3 years ago.
I very new to C# and I would like to add a Button to a form programmatically. When this button is pressed I want it to run some code (like a normal button woukd work). But, everywhere I have searched only shows how to add the button and not how to add code to the button.
One way to do it is to give the button a name property, then in your code you "refer" to the button by it's name like so:
void btnButton_Click(object sender, EventArgs e)
{
// your code goes here
}
where btnButton_Click is the name of the button.
This question already has answers here:
How to enable button in Form 1 from Form 2 using public get set?
(5 answers)
Closed 4 years ago.
I want to disable the button from other form from current form and enable the button again after clicking reset button.
Add a public method where you control the button state as disabled or enabled in this form,then transfer this form as parameter to other form
This question already has answers here:
Best way to implement keyboard shortcuts in a Windows Forms application?
(9 answers)
Closed 7 years ago.
I am making a program to record the position of a mouse when someone presses the Space button.
This works fine, however when I put the cursor in any textBox in the form the code becomes useless of course because the space gets typed in the textBox. I tried to change the focus() or try other keys like LeftWin ... but none worked!
Any advice on how can I detect the Space button (or any other key) all the time in a form?
private void lebel1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
//bla bla
}
}
The event is not firing because the Textbox is handled the event, with that being said, the event will not be propagated to the parent controls (Bubbling events).
You could use a KeyPress, which means that the event will be fired from the most outer control and will be propagated to their children. (Tunnel events)
You could learn more about here.
This question already has answers here:
How to create a right click event handler in c#
(4 answers)
Closed 7 years ago.
I'm a beginner, so this may be easy. But i could not find it. I want to add a right click event to a windows form. but when i look properties/events menu of the form, I could not find any right click event. Is there a menu or code example for that? i prefer to use it auto constructed like double clicking for click event, so it is better to me if you can show me how can i find that property if it exists.
Thanks for helping.
its a mousedown event,
private void btn_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
// do your stuff
}
}
This question already has answers here:
How to detect the currently pressed key?
(12 answers)
Closed 8 years ago.
For the button, how to detect whether it is ctrl + click a button, in the click event handler?
I am using VS 2010 using C#.
Are we talking about winforms, wpf or asp application? I will make a quick assumption that it's winforms, so here goes:
Form Form1;
button.Click += click;
private void click(object s, EventArgs e)
{
if(Form1.ModifierKeys == Keys.Control)
... // Whatever you need here
}