I am wanting to make a wildcard search character (ex. Binary%) so when they click search it finds all the files with the word Binary in the filename and loads them into a list box. My current code is below.
Private Sub _test_TextChanged(sender As Object, e As TextChangedEventArgs) Handles _test.TextChanged
For x As Integer = 0 To _listbox.Items.Count - 1
If _listbox.Items(x).ToString = _test.Text$ Then
_listbox.SelectedIndex = x
Return
End If
Next
End Sub
Any help is welcome!
Thank you!
-Kyvex
What you are asking doesn't really match your code.
... "when they click search it finds all the files" ...
But you have a TextChanged event handler of a TextBox (not a Button)
..."with the word Binary in the filename and loads them into a list box"
But you select only the first item which matches the filter of items already in a ListBox
To get your code to do what it seems to want to do, simply use the Like operator and add the wildcard character (*) after the TextBox.Text
For x As Integer = 0 To _listBox.Items.Count - 1
If _listBox.Items(x).ToString Like _test.Text & "*" Then
_listBox.SelectedIndex = x
Return
End If
Next
Now you can select the first item in the listbox which matches the filter
If you have a multi-select ListBox, you can use this
If _test.Text = "" Then Exit Sub
_listBox.SelectionMode = SelectionMode.MultiSimple
For x As Integer = 0 To _listBox.Items.Count - 1
_listBox.SetSelected(x, _listBox.Items(x).ToString Like _test.Text & "*")
Next
(the filter logic is the same as the first example)
And you can make it case-insensitive
_listBox.SetSelected(x, _listBox.Items(x).ToString().ToUpper() Like _test.Text.ToUpper() & "*")
Related
I have two list boxes on a form I am making. The first list box is linked to a table with various company names. The goal I am after is after double clicking a companies name, the value is inserted in the second list box.
It worked fine until I tried to add code to prevent duplicates from appearing in the second list box, so you couldn't accidentally insert the same company twice. I have tried several different iterations, but with no luck. Anyone able to help with this one? My end goal would be for a msgbox to pop up alerting the user that duplicates are not allowed.
Private Sub ContractorLstbx_DblClick(Cancel As Integer)
Dim found As Boolean
found = False
Dim ID As Long
Dim Contractor As String
For Each newItem In Me.ContractorLstbx.ItemsSelected
For j = 0 To Me.SelectedContractorLst.ListCount - 1
If (Me!ContractorLstbx.ItemData(newItem).Column(1) = Me.SelectedContractorLst.ItemData(j).Column(1)) Then
found = True
Exit For
End If
Next j
If found = False Then
ID = Me.ContractorLstbx.ItemData(newItem)
Me.SelectedContractorLst.AddItem ContractorLstbx!.ItemData(newItem).Column(0) & ";" & Me!ContractorLstbx.ItemData(newItem).Column(1)
End If
found = False
Next newItem
End Sub
This is the full code for your solution. I tried it on test sample and working fine. just copy and paste the code. If you need your comparison to be case sensitive (I mean A <> a) then use Option Compare Binary as in my code below. If it is required to be case insensitive (A = a) just leave the default Option Compare Database or better force it using Option Compare Text
Option Compare Binary
Private Sub ContractorLstbx_DblClick(Cancel As Integer)
Dim found As Boolean
found = False
Dim ID As Long
Dim Contractor As String
For i = 0 To Me.ContractorLstbx.ItemsSelected.Count - 1
For j = 0 To Me.SelectedContractorLst.ListCount - 1
If (Me.ContractorLstbx.Column(1, Me.ContractorLstbx.ItemsSelected(i)) = Me.SelectedContractorLst.Column(1, j)) Then
found = True
Exit For
End If
Next j
If found = False Then
ID = Me.ContractorLstbx.ItemData(Me.ContractorLstbx.ItemsSelected(i))
Me.SelectedContractorLst.AddItem (ContractorLstbx.Column(0, Me.ContractorLstbx.ItemsSelected(i)) & ";" & Me.ContractorLstbx.Column(1, Me.ContractorLstbx.ItemsSelected(i)))
End If
found = False
Next i
End Sub
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a TextBox that includes more words
Example:
I'm pRoFeSSoinaL coder
I want to search for the word pRoFeSSoinaL (that contains uppercase and lowercase letters) and then replace it.
OK let me try and address your problem again based on your image:
And on your comment:
...but i need search about word have More than a change of letters example (execute, ExEcute, eXecute, execUtE........ETC) i need find word No matter how large and small the letters are see this proof
Again, if I have this right, you would like to find all occurrences of a word regardless of it's case. In effect you would like for your application to work a bit like Notepad.
I've put together some code that will use the following methods:
String.ToLower:
Returns a copy of this string converted to lowercase.
TextBox.Select:
Selects a range of text in the text box.
String.Remove:
Returns a new string in which a specified number of characters in the current instance beginning at a specified position have been deleted.
String.Insert:
Returns a new string in which a specified string is inserted at a specified index position in this instance.
String.Substring:
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
I have three Button controls that I've put a bit a code behind. One will find the next occurrence of a word, another will replace the next occurrence of a word and the last one will replace all occurrences of a word.
Now it's not my job to design your application but for ease I have also popped on three TextBox controls. This is how my form looks:
To find the next occurrence of professional this is the code I used:
Private Sub btnFindNext_Click(sender As Object, e As EventArgs) Handles btnFindNext.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
'select next occurrence
txtTextToSearch.Select(position, txtWordToFind.Text.Length)
End If
End Sub
This is a screenshot of how it would look when you click the button:
Clicking the button again will select the next occurrence of the word. It will keep toggling between the two in my example.
To replace the next occurrence of professional this is the code I used:
Private Sub btnReplaceNext_Click(sender As Object, e As EventArgs) Handles btnReplaceNext.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
'remove old word
txtTextToSearch.Text = txtTextToSearch.Text.Remove(position, txtWordToFind.Text.Length())
'insert new word
txtTextToSearch.Text = txtTextToSearch.Text.Insert(position, txtReplaceWith.Text)
End If
End Sub
This is a screenshot of how it would look when you click the button:
Clicking the button again will replace the next occurrence of the word.
To replace all occurrences of the word professional this is the code I used:
Private Sub btnReplaceAll_Click(sender As Object, e As EventArgs) Handles btnReplaceAll.Click
'check word exists
If txtTextToSearch.Text.ToLower().Contains(txtWordToFind.Text.ToLower()) Then
'reset position if at end of text
If txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1) < 0 Then
position = 0
End If
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
While position > 0 AndAlso position < txtTextToSearch.Text.Length
'remove old word
txtTextToSearch.Text = txtTextToSearch.Text.Remove(position, txtWordToFind.Text.Length())
'insert new word
txtTextToSearch.Text = txtTextToSearch.Text.Insert(position, txtReplaceWith.Text)
'set position of next occurrence
position = txtTextToSearch.Text.ToLower().IndexOf(txtWordToFind.Text.ToLower(), position + 1)
End While
End If
End Sub
This is a screenshot of how it would look when you click the button:
My code will need adapting I'm sure but it should give you something to go on. As for the level of detail I have gone to, may not benefit you but may benefit others that visit this question. Hope this helps.
Judging from your screenshot (http://i.imgur.com/77wdZoI.png) you appear to be after case-insensitive string matching.
The simplest way is to use the String.IndexOf(String, StringComparison) overload to perform case-insensitive comparison:
Private Function FindText(ByVal Source As String, ByVal Text As String) As Integer
Return Source.IndexOf(Text, StringComparison.OrdinalIgnoreCase)
End Function
Usage example:
Dim TextToFind As String = "professional"
Dim TextPos As Integer = FindText(TextBox1.Text, TextToFind)
If TextPos > -1 Then 'Match found.
TextBox1.SelectionStart = TextPos
TextBox1.SelectionLength = TextToFind.Length
End If
How can i check for a character after other text within a listbox?
e.g
Listbox contents:
Key1: V
Key2: F
Key3: S
Key4: H
How do I find what comes after Key1-4:?
Key1-4 will always be the same however what comes after that will be user defined.
I figured out how to save checkboxes as theres only 2 values to choose from, although user defined textboxes is what im struggling with. (I have searched for solutions but none seemed to work for me)
Usage:
Form1_Load
If ListBox1.Items.Contains("Key1: " & UsersKey) Then
TextBox1.Text = UsersKey
End If
Which textbox1.text would then contain V / whatever the user defined.
I did try something that kind of worked:
Form1_Load
Dim UsersKey as string = "V"
If ListBox1.Items.Contains("Key1: " & UsersKey) Then
TextBox1.Text = UsersKey
End If
but i'm not sure how to add additional letters / numbers to "V", then output that specific number/letter to the textbox. (I have special characters blocked)
Reasoning I need this is because I have created a custom save settings which saves on exit and loads with form1 as the built in save settings doesn't have much customization.
e.g Can't choose save path, when filename is changed a new user.config is generated along with old settings lost.
Look at regular expressions for this.
Using the keys from your sample:
Dim keys As String = "VFSH"
Dim exp As New RegEx("Key[1-4]: ([" & keys& "])")
For Each item As String in ListBox1.Items
Dim result = exp.Match(item)
If result.Success Then
TextBox1.Text = result.Groups(1).Value
End If
Next
It's not clear to me how your ListBoxes work. If you might find, for example, "Key 2:" inside ListBox1 that you need to ignore, you will want to change the [1-4] part of the expression to be more specific.
Additionally, if you're just trying to exclude unicode or punctuation, you could also go with ranges:
Dim keys As String = "A-Za-z0-9"
If you are supporting a broader set of characters, there are some you must be careful with: ], \, ^, and - can all have special meanings inside of a regular expression character class.
You have multiple keys, I assume you have multiple textboxes to display the results?
Then something like this would work. Loop thru the total number of keys, inside that you loop thru the alphabet. When you find a match, output to the correct textbox:
Dim UsersKey As String
For i As Integer = 1 To 4
For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
UsersKey = c
If ListBox1.Items.Contains("Key" & i & ": " & UsersKey) Then
Select Case i
Case 1
TextBox1.Text = UsersKey
Case 2
TextBox2.Text = UsersKey
Case 3
TextBox3.Text = UsersKey
Case 4
TextBox4.Text = UsersKey
End Select
Exit For 'match found so exit inner loop
End If
Next
Next
Also, you say your settings are lost when the filename is changed. I assume when the version changes? The Settings has an upgrade method to read from a previous version. If you add an UpgradeSettings boolean option and set it to True and then do this at the start of your app, it will load the settings from a previous version:
If My.Settings.UpgradeSettings = True Then
My.Settings.Upgrade()
My.Settings.Reload()
My.Settings.UpgradeSettings = False
My.Settings.Save()
End If
Updated Answer:
Instead of using a listtbox, read the settings file line by line and output the results to the correct textbox based on the key...something like this:
Dim settingsFile As String = "C:\settings.txt"
If IO.File.Exists(settingsFile) Then
For Each line As String In IO.File.ReadLines(settingsFile)
Dim params() As String = Split(line, ":")
If params.Length = 2 Then
params(0) = params(0).Trim
params(1) = params(1).Trim
Select Case params(0)
Case "Key1"
Textbox1.Text = params(1)
Case "Key2"
Textbox2.Text = params(1)
End Select
End If
Next line
End If
You can associate text box with a key via its Name or Tag property. Lets say you use Name. In this case TextBox2 is associated with key2. TextBox[N] <-> Key[N]
Using this principle the code will look like this [considering that your list item is string]
Sub Test()
If ListBox1.SelectedIndex = -1 Then Return
Dim data[] As String = DirectCast(ListBox1.SelectedItem, string).Split(new char(){":"})
Dim key As String = data(0).Substring(3)
Dim val As String = data(1).Trim()
' you can use one of the known techniques to get control on which your texbox sits.
' I omit this step and assume "Surface1" being a control on which your text boxes sit
DirectCast(
(From ctrl In Surface1.Controls
Where ctrl.Name = "TextBox" & key
Select ctrl).First()), TextBox).Text = val
End Sub
As you can see, using principle I just explained, you have little parsing and what is important, there is no growing Select case if, lets say, you get 20 text boxes. You can add as many text boxes and as many corresponding list items as you wish, the code need not change.
Can somebody help me fix this code, I have two textbox where when you paste the text on the 1st Textbox and click the search button, it highlighted the exact text on the 2nd textbox if it is present on the 2nd textbox. But when the string on 2nd textbox contains linefeed/newline, it added one character from the start of the text. Here is the code:
Private Sub FindText(ByVal start_at As Integer)
Dim pos As Integer
Dim target As String
target = Textbox1.Text
pos = InStr(start_at, Textbox2.Text, target)
If pos > 0 Then
' We found it.
TargetPosition = pos
Textbox2.SelStart = TargetPosition - 1
Textbox2.SelLength = Len(target)
Textbox2.SetFocus
Else
' We did not find it.
MsgBox "Not found."
Textbox2.SetFocus
End If
End Sub
' Search button
Private Sub cmdSearch_Click()
FindText 1
End Sub
I believe the problem is that Textbox is treating newline as a single character while Len() counts as CRLF as two. You should probably count the number of times CRLF appears in the text preceding the match target and adjust SelStart accordingly. Try this line instead:
Textbox2.SelStart = Len(Replace(Left(target, pos - 1), vbCrLf, "^")) - 1
'Textbox2.SelLength = Len(Replace(target, vbCrLf, "^"))
If target can include line breaks you may have a similar problem with SelLength which is why I've left the second, commented line. This works by substituting two-character line-break sequences into a single-character string. It's completely arbitrary what value to use for replacement since the result is discarded and only the length ultimately matters.
I know I could use .FindString for this but for some reason it is not working.
Basically,if listbox items contains just a PART of textbox text,it does action.
Here's the example of not-working code :
Dim x As Integer = -1
x = ListBox1.FindString(TextBox1.Text)
If x > -1 Then
'dont add
ListBox2.Items.Add("String found at " & x.ToString)
Else
End If
The FindString method returns the first item which starts with the search string (MSDN). If you want to match the whole item, you would have to use FindStringExact (MSDN). If you want to perform more complex searches, you would have to iterate through all the elements in the ListBox.
UPDATE:
Code delivering the exact functionality expected by the OP.
For i As Integer = 0 To ListBox1.Items.Count - 1
If (ListBox1.Items(i).ToString.Contains(TextBox1.Text)) Then
ListBox2.Items.Add("String found at " & (i + 1).ToString) 'Indexing is zero-based
Exit For
End If
Next