-
November 7th, 2019, 05:58 AM
#1
[RESOLVED] Finding an item (text) in a List View with Visual Studio 2012 Express
I am using Visual Studio 2012 Express
I have a List View that gets populated with words and their length (column 1) based upon a selection of a combo box - this works fine.
I have a button (code below) that attempts to search for a word in the list view and it is NOT working.
I want to find either the whole word or part of.
An example: I have a list of 50 bible words that populate the list view, I can successfully add a word using another button, so I add a name "sally" (just to test). "sally" gets added to the end of the list (preferred). Now I click on my search button and enter "sally" in the Input Box. When it searches, it highlights the first thing that "might" be sally:
("sa", "al", "all", "ly") and never "sally"!
I have literally searched the web looking and trying about 30 different ways to code this and NONE give me what I want.
May I please get some help with this?
Code:
' ********** THIS IS THE SEARCH BUTTON **********
' Step 1: Establish the variables I will use here in this subroutine.
Dim tmpInt1 As Integer = 0
Dim tmpString1 As String = Nothing
Dim tmpItem As ListViewItem = Nothing
' Step 2: Prompt the user for the word or characters to search for.
retry: tmpString1 = Trim(LCase(InputBox("Enter the word [or part of] to search for:", "Word Search")))
If tmpString1 Is Nothing Then Exit Sub
' Step 3: Determine if there is any illegal characters in the word being search for [!@#$%^&*()_-+=:'",<.>/?|\~`].
For x As Integer = 1 To Len(Trim(tmpString1))
If Mid(tmpString1, x, 1) < Chr(97) Or Mid(tmpString1, x, 1) > Chr(122) Then
MsgBox("You have entered an illegal character into the search box." & Chr(10) & "Re-enter the word you are searching for and click OK.")
GoTo retry
End If
Next
' Step 4: Search for the entered text in the list. If NOT found, inform the user and exit the subroutine.
tmpItem = ListView1.FindItemWithText(tmpString1, True, 0, True)
If Not tmpItem Is Nothing Then
ListView1.Focus()
ListView1.TopItem = tmpItem
tmpInt1 = ListView1.TopItem.Index
ListView1.Items(tmpInt1).Selected = True
Exit Sub
End If
MsgBox("The word or characters you searched for are NOT found in the list.")
-
November 7th, 2019, 01:10 PM
#2
Re: Finding an item (text) in a List View with Visual Studio 2012 Express
What happens if you change that last argument to false rather than true. True tells it to include partial matches false tells it not to.
btw that code looks more like VB6 than VB.Net. You should probably brush up on the correct procedures in VB.Net rather than using the older VB6 statements.
For example Trim(), Mid(), Chr() and Msgbox() are all VB6 statements and even in VB6 you should not use Mid() and Chr() but Mid$() and Chr$() as the ones without the $ are variant versions and the one with the $ are proper string versions which are faster.
Last edited by DataMiser; November 7th, 2019 at 01:15 PM.
Always use [code][/code] tags when posting code.
-
November 9th, 2019, 09:34 PM
#3
Re: Finding an item (text) in a List View with Visual Studio 2012 Express
I'm using Visual Studio 2012 Express and the statements I use are currently available to me and I use them because of that.
I do realize that there are more preferred statements that Visual Studio prefer and you prefer I use: (ie: Strings.Trim, Strings.Mid, MessageBox.Show), but the others work and you type less.
Now, hopefully all of you more experienced coders won't bash me to hard about this, but I came up with a solution that I want to share but cannot because I don't see any code tags available to me in this "Quick Reply" box. Oh well... You're out of luck!!
-
November 10th, 2019, 03:07 AM
#4
Re: Finding an item (text) in a List View with Visual Studio 2012 Express
Originally Posted by oldGMtireman
I'm using Visual Studio 2012 Express and the statements I use are currently available to me and I use them because of that.
I do realize that there are more preferred statements that Visual Studio prefer and you prefer I use: (ie: Strings.Trim, Strings.Mid, MessageBox.Show), but the others work and you type less.
Now, hopefully all of you more experienced coders won't bash me to hard about this, but I came up with a solution that I want to share but cannot because I don't see any code tags available to me in this "Quick Reply" box. Oh well... You're out of luck!!
We'll manage. Thanks.
-
November 10th, 2019, 05:45 AM
#5
Re: [RESOLVED] Finding an item (text) in a List View with Visual Studio 2012 Express
I don't see any code tags available to me in this "Quick Reply" box.
Code tags are available from within Go Advanced. Alternatively, just put [ code ] and [ /code ] around the code (without the spaces).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
November 13th, 2019, 09:46 AM
#6
Re: [RESOLVED] Finding an item (text) in a List View with Visual Studio 2012 Express
Here is what I came up with:
Code:
Dim tmpInt1 As Integer = 0 ' This is a counter to search all of the words in the ListView
Dim tmpString1 As String = Trim(LCase(InputBox("Enter the word [or part of] to search for:", "Word Search"))) ' This is what to look for
Dim tmpString2 As String = Nothing ' This is each word of the ListView
Dim tmpString2 As String = Nothing ' This string makes things a little easier
' If you enter consecutive items in the InputBox, the code will return the first item with ALL of those matching characters and not just the
' first character. It will then highlight and bring into focus the found item. If the item is not found, a message is displayed. Adjust this code
' to your liking. I hope I have helped someone
For tmpInt1 = 0 To (ListView1.Items.Count - 1)
tmpString2 = Trim(ListView1.Items.Item(tmpInt1).SubItems(1).Text)
For z As Integer = 1 To ((Len(tmpString2) - tmpInt2) + 1)
tmpString3 = Strings.Mid(tmpString2, z, tmpInt2)
If tmpString3 = tmpString1 Then
ListView1.Focus()
ListView1.Items.Item(tmpInt1).EnsureVisible()
ListView1.Items.Item(tmpInt1).Selected = True
Exit Sub
End If
Next
Next
MsgBox("The word or characters you searched for are NOT found in the list.")
Last edited by oldGMtireman; November 13th, 2019 at 09:48 AM.
Reason: to assist another VS2012 coder
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
|