Detecting Ctrl + click button and click button [duplicate] - c#

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
}

Related

How would one add code to a control that was added programmatically to a windows form application in c#? [duplicate]

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.

How to make event handler for key down even if form is not visible/closed? [duplicate]

This question already has answers here:
Set global hotkeys using C#
(3 answers)
Closed 3 years ago.
I'm making an app that will control my rgb lights and I want it to handle event for clicking specific key, but i want to run it as a background process.
I have already tried using this:
private void Form1_Load(object sender, EventArgs e)
{
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
}
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.F9)
{
Debug.WriteLine("F9 down");
}
}
It works, but only if form window is active and visible, so I'm not satisfied. I'm looking for a solution that will work even if the form is invisible.
When a keydown event happens and a form is not focused / active it will not detect the event. However, you can use Win API to create a key listener of sorts. See this link for more details of "Global Hotkeys".

How distinguish two different buttons in same event click? [duplicate]

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.

c#..Space KeyDown and Space in a textBox [duplicate]

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.

How to add a right click event to an object like button,label,listbox etc [duplicate]

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
}
}

Categories

Resources