-
Apr 6th, 2019, 02:01 PM
#1
Thread Starter
Member
[RESOLVED] Need help for a specific navigation to items in a List Box - VB 2010
Hi!
I created a listbox that adds files with specific extensions. The files are of type: *.zip, *.rar, and *.7zip. I have many files in a directory of each of these extensions. Assuming the selected item in my listbox is of type *.7zip, I wanted it when I pressed key 1 on the keyboard, I navigated directly to the first item of type *.rar. And as soon as the first item of type * .rar was selected in my listbox, if I pressed the key 1 again, I navigated straight to the first item of type * .zip. Is it possible to create something like this?
My code to create the listbox is:
Code:
Public Class Form1
Dim directory = Application.StartupPath & "\zipfiles"
Dim zip1() As System.IO.FileInfo
Dim zip2() As System.IO.FileInfo
Dim zip3() As System.IO.FileInfo
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
zip1 = dirinfo.GetFiles("*.7zip", IO.SearchOption.AllDirectories)
zip2 = dirinfo.GetFiles("*.rar", IO.SearchOption.AllDirectories)
zip3 = dirinfo.GetFiles("*.zip", IO.SearchOption.AllDirectories)
For Each file1 In zip1
ListBox1.Items.Add(file1)
Next
For Each file2 In zip2
ListBox1.Items.Add(file2)
Next
For Each file3 In zip3
ListBox1.Items.Add(file3)
Next
End Sub
End Class
I thank you all for any help you can give.
Last edited by vbnewbieuser; Apr 8th, 2019 at 10:37 AM.
-
Apr 6th, 2019, 07:34 PM
#2
Re: Need help for a specific navigation to items in a List Box - VB 2010
You can handle the KeyDown event of the control and then do whatever you want. You would get the selected file path, determine what the extension is, determine what extension you want to select next, find the first item with that extension and select it. One option to speed up the finding part might be to remember the indexes of first item with each extension when you first populate the list and then use those values directly later to set the SelectedIndex. Otherwise, you would need a loop or LINQ query to find the desired item each time. A LINQ query using First (or FirstOrDefault if there may be no match) would be the simplest option in that case.
-
Apr 7th, 2019, 10:58 AM
#3
Thread Starter
Member
Re: Need help for a specific navigation to items in a List Box - VB 2010
Originally Posted by jmcilhinney
You can handle the KeyDown event of the control and then do whatever you want. You would get the selected file path, determine what the extension is, determine what extension you want to select next, find the first item with that extension and select it.[...]
Sir., first of all, I apologize for the delay to return here. As a technician, I am required to respond to emergency calls and I had to hurry out of my house, leaving the PC on almost now. I really appreciate your suggestion. By the way, you helped me a lot, I thinking about a method that is not yet ideal, here as pseudo code to explain my idea:
Code:
'IF THE USER PRESS THE KEY 1
For i As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(i).ToString().Contains(".zip") Then
ListBox1.SelectedIndex = i
Else
Next
'END IF THE USER PRESS THE KEY 1
But I need to create a kind of "counter", so that the user can always use the same key and restart the process.
However, your help was very important, Sir. jmcilhinney , thank you very much!
-
Apr 7th, 2019, 06:14 PM
#4
Re: Need help for a specific navigation to items in a List Box - VB 2010
You don't need a counter because there's nothing to count. All you need is as list of the extensions in the order you want to select them. When you detect the relevant key, you simply get the extension of the current selected item, find that extension in the list, get the next extension in the list and then select the first item with that extension. As I said before, you can either search for that item on demand or you can remember the relevant indexes when you load the data.
Actually, thinking about it further, it's actually even easier than that, assuming that you have all the same extensions grouped, as appears to be the case. Based on this code:
vb.net Code:
For Each file1 In zip1 ListBox1.Items.Add(file1) Next For Each file2 In zip2 ListBox1.Items.Add(file2) Next For Each file3 In zip3 ListBox1.Items.Add(file3) Next
You could start by declaring these fields:
vb.net Code:
Private firstRarIndex As Integer Private firstZipIndex As Integer
You could then load the data like so:
vb.net Code:
ListBox1.Items.AddRange(zip1) firstRarIndex = ListBox1.Items.Count ListBox1.Items.AddRange(zip2) firstZipIndex = ListBox1.Items.Count ListBox1.Items.AddRange(zip3)
You could then navigate like so:
vb.net Code:
Select Case ListBox1.SelectedIndex Case Is <= firstRarIndex ListBox1.SelectedIndex = firstRarIndex Case Is <= firstZipIndex ListBox1.SelectedIndex = firstZipIndex Case Else ListBox1.SelectedIndex = 0 End Select
That assumes that there will always be at least one file of each type. If that may not be the case then you can modify the code to allow for that.
-
Apr 8th, 2019, 10:35 AM
#5
Thread Starter
Member
Re: Need help for a specific navigation to items in a List Box - VB 2010
Originally Posted by jmcilhinney
You don't need a counter because there's nothing to count. All you need is as list of the extensions in the order you want to select them. When you detect the relevant key, you simply get the extension of the current selected item, find that extension in the list, get the next extension in the list and then select the first item with that extension.[...]
The question about an "counter" was a crazy idea I had, which would be to count identify each type of file. For example: if the user pressed the key 1 once and a variable integer type "countkeypress" is equal to zero, then 1 is added to the countkeypress variable. And if this variable equals 1, selects the first listbox item of type *.rar. If this variable is equal to 2, select the first listbox item of type *.zip and so on. But your suggestion is much better.
Amazing, Sir! It was very clear to me. I really appreciate your help. I'll put it as resolved
Thank you very much for your help!
Best Regards.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|