Key Down not working in listView [duplicate] - c#

This question already has answers here:
C# Windows Forms code not working - Attach Event to button
(2 answers)
Forms not responding to KeyDown events
(3 answers)
Closed 7 years ago.
It is my second day doing c#...don't judge please. I have read other threads but they did not help.
I have this code:
private void listView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
MessageBox.Show("aa");
}
}
which is not working.
What am I doing wrong?

The issue is that you haven't told the ListView to actually use the event. To do this you have to assign the method to the required event. there are two ways to do this. Either select the ListView and open the Properties tab go to events and double click on the one you want. (Visual studio will automatically put the event out for you). Or in the constructor of the form or elsewhere you can manually assign it. In your case it would look like...
listView1.KeyDown += listView1_KeyDown;
Note you don't have to use a name similar to what visual studio would automatically produce. You can name your method whatever you want as long as the method signature matches the event. This is nice if you have multiple list boxes and want to use the same method to handle all of them. For example you could do something like.
listView2.KeyDown += listView1_KeyDown;
I suggest reading up on how events work in c#.

Related

What do C# event parameters do? [duplicate]

This question already has answers here:
RoutedEventArgs.Source vs Sender
(4 answers)
How do C# Events work behind the scenes?
(2 answers)
Closed 5 years ago.
I don't understand what event parameters do in C#. Let's say we have a button called CoffeeButton, and clicking on it takes you to another Page called Coffee using a Frame called myFrame.
This is my code:
private void CoffeButton_Click(object sender, RoutedEventArgs e)
{
MyFrame.Navigate(typeof(Coffee));
}
What does object sender and RoutedEventArgs e do in this case?
Examples would be great!
Normally, "sender" will be a reference to whatever object fired the event. So if, for example, you have more than one Button that all wire into the same button_Click handler function, the sender object would be a reference to whichever actual Button object was clicked.
The EventArgs object that's normally passed in as the second parameter is used for different things depending on the context. Generally, it's used to pass you additional information related to the event that happened. For example, in this case, the RouteEventArgs object provides a RoutedEvent property that you can look at if you need to.

KeyDown event doesnt work [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 would need that (on a WinForm) if the key A is pressed, an event is triggered.
I got this code from the MSDN site:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.A)
{
MessageBox.Show("Key 'a' was pressed.");
}
}
The problem is that this code doesn't work for me, there is no error message, but if I am on the form and press the key A there is no event triggered. I tried to use breakpoints, but it never enters the if.
What is the problem here?
You need to set Form.KeyPreview property to True. This property gets or sets a value indicating whether the form will receive key events before the event is passed to the control that has focus.
Also note that there is a mistake in your code; according to your message you need to verify if e.KeyCode == Keys.A.

Intercept ListView Column disposing

Environment: C# WinForm Application (Framework 4.0)
Objective: read some data (Ex. column width) from a ListView just before the containing form is closed.
I don't wanna read the data EveryTime the column size is changed....
If i check Listview.Columns.Count inside the FormClosing event, i always find that the list is ALREADY empty.
That means Columns List is getting cleared just before FormClosing event.
How can i get column width (Or other columns-related info) just before the form is closed?
What is the event i am looking for? I use C#
Thanks in advance guys...you're the best!
You can use
protected override void OnClosing(CancelEventArgs e)
{
}
Event, this will be called before form_closing event.
I have checked your scenario both in form_closing and onclosing column has the count and you can get the details in both cases . If this does not worked out for you Please post some code so that anyone could help.

C# assign hotkey to a button [duplicate]

This question already has answers here:
How to set hotkeys for a Windows Forms form
(8 answers)
Closed 8 years ago.
Currently I want to add a hotkey to my button in C# windows application.
I went to the event, I didn't see anywhere I can assign hotkeys.
I wanna assign "F6' as the hotkey, which means whenever I press F6 it would trigger the button and run the code in it
You could try to handle the Forms Keypress-event (and route all components keypress-events to that)
Assigning hotkeys via the GUI works just for MenuItems, if I'm not mistaken.
Edit: some code:
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.F6)
button1_Click(this, new EventArgs());
}
Just what I quickly tried. If you register to the forms (or any other controls for that matter) KeyUp-event, you can query the released key and act accordingly. I suppose there are more elegant ways to do this, but this should get you started.

Best way to show/hide .NET application using a keyboard shortcut? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Global keyboard capture in C# application
Hi all,
I am making a .NET application (Window Forms) which is written by C# and I get a problem. How to hide my personal .NET application using a keyboard shortcut and then displaying it back from same keyboard shortcut.
Thanks !
First you should probably clarify which technology you're using: WinForms, WPF?
Anyway, using WinForms You can use the KeyDown event to process such occurrences:
private void OnKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.Shift && e.KeyCode == Keys.M)
{
WindowState = FormWindowState.Minimized;
}
}
Note that the modifier key/s and regular key/s you require to be pressed to carry out the action are obviously changeable.
A further note is that once this action has been executed then the window no longer has 'focus' and so repeating the keystrokes to display the window again will not work - for this to happen you will need to register a hot-key that Windows itself knows about or use a keyboard hook to intercept keystroke input to the system to consume in your application, AFAIK.

Categories