Question Allowing the Up & Down arrow keys in an ListView

Joined
Feb 14, 2021
Messages
6
Programming Experience
1-3
I have a dialog form that has a list view control it. When the user selects a certain menu option from another form, this dialog form opens and the list view control is populated with file names & dates from a certain folder.
The multi-select option is set to TRUE and I would like the user to be able to select multiple file names within the list view by:

a) selecting one then holding either CONTROL or SHIFT and clicking others
b) selecting one then holding SHIFT and UP or DOWN arrow and selecting others

There are 2 buttons on the dialog form. One of them is named "CANCEL" and the other is named either "DELETE" or "OPEN" The multi-select option is only set to TRUE if the button is named "DELETE".
I use the Key Press event for the list view to trap the keys pressed:


need help here:
Private Sub ListView1_KeyDown(sender As Object, e As KeyEventArgs) Handles ListView1.KeyDown
        ' This code has been tested and works correctly on 1/13/2021

        Select Case Button1.Text

            Case "DELETE"
                If e.KeyCode <> Keys.Delete Then
                    e.Handled = True
                    Exit Sub
                End If

            Case "OPEN"
                If e.KeyCode <> Keys.Enter Then
                    e.Handled = True
                    Exit Sub
                End If

        End Select

        Button1_Click(Nothing, Nothing)

    End Sub

How can I change this to work for me.
 
First things first, don't call an event handler directly like you are at the end of that method. Generally speaking, if you want to execute code from multiple places then you put that code in its own method and then you call that method from multiple places. In your case, that would mean moving the code in Button1_Click to a dedicated method and then calling that method in your ode above as well as in Button1_Click.

In the case of the Click event handler of a Button though, you have another option. You can call the PerformClick event of that Button and then it will actually raise a Click event as though the user clicked it in the UI, which will invoke your event handler.
 
Please post your questions in the most appropriate forum. You have posted this in VS.NET General and that is completely wrong. Firstly, the VS forums are for IDE questions. Language questions being in the VB forums. This is also obviously not a general VB question because it relates to a specific UI framework. Now I need to ask you which one, because the forum the thread is posted in doesn't tell me. I'm guessing Windows Forms based on the code but I shouldn't have to guess. I'll move this thread to the WinForms forum for now and will move it again if you suggest otherwise.
 
As for the question, I just created a new WinForms project targeting .NET Framework 4.8 and the arrow keys already worked with the Shift key as you described so either you already have what you want by default or something else is at play here that you haven't explained.

It occurs to me that what you're actually asking for is to prevent the arrows and Shift key working that way if the text on the Button is "Open". If so then you should have made that crystal clear. The more we have to assume and/or guess, the more likely we waste everyone's time by getting it wrong.
 
No, this does NOT work foe me. Obviously I didn't explain correctly, you don't understand or both, I am sorry I bothered this forum. You must have the ability to delete accounts so delete mine.
 
Or, you could take on board the advice I provided to write better code for yourself and better questions for us, then try explaining this issue more clearly so we can try to help you resolve it. For instance, you could provide us with a list of steps that we could perform to reproduce the issue. The steps should be precise and complete and the best way to achieve that is for you to do what I did and start from a clean slate, creating a new project and isolating the specific functionality at issue. If you see the same issue then you can provide us with the exact steps you performed and we can repeat them to see whether we get the same result. If you don't see the same behaviour then you know that it is something specific to do with your existing project and you need to investigate further. We can't help with that if we know nothing about the project. Instead of giving up at the first sign of criticism, accept the criticism as a means for you to get better, which I would hope you would want, and work with us to help you solve your issue by providing the information we need, rather than acting like you've been hard done by for being asked to provide it.
 

Latest posts

Back
Top