One more question, if I want to search for multiple collections of items, like names, phone numbers, etc, in the same listbox, is it possible to create a category separators between those collections.
Start4me 0 Newbie Poster
Minimalist 96 Posting Pro
Did you get the code to work? And to your question: no a listbox ist not meant to this. Either use a listview control or use multiple listboxes synchronised - which is not easy. If you got the former part working as you wanted please close this thread as solved and mybe start another one.
Start4me 0 Newbie Poster
The search works well, but I need this to work the other way around. I need the results after search to appear in the listbox, not in the textbox2 like it is right now. Since I can't click on the item in a textbox like I can in the listbox.
Minimalist 96 Posting Pro
I you copied my code and have a button1 to clear not selected items you can just insert the following line at line number 26 in the code above
Button1.PerformClick()
this will call the eventhandler of button1.
Start4me 0 Newbie Poster
OK, so it does remove the items from the listbox, but somehow after the user deletes characters, the items don't come back to the listbox like they do in the textbox2. And since the items get removed from the listbox, can we get rid of textbox2?
Edited by Start4me because: Improve the idea
Minimalist 96 Posting Pro
Replace: Button1.PerformClick() with
If TextBox1.Text <> "" Then
Button1.PerformClick()
End If
Delete textbox2 and the referring code
Start4me 0 Newbie Poster
OK, I've done like you said, but even now after the user deletes characters in the textbox, the items don't come back to the listbox. Even with the replaced code of the Button1.PerformClick().
Minimalist 96 Posting Pro
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
Dim strOr As String, strReturn As String, strL As String, valueL As String
strOr = Trim(TextBox1.Text)
strL = UCase(Trim(TextBox1.Text)) 'hold input in lower case
TextBox2.Text = ""
ListBox1.ClearSelected()
For Each value As String In MySource 'returns the origanal string from list
valueL = UCase(Trim(value)) 'hold string from list in lower case
If InStr(valueL, strL) > 0 And strOr <> "" Then 'now compare only lower cases
strReturn = value ' now we found a match so we use the original string again
TextBox2.Text = TextBox2.Text & strReturn & vbNewLine
ListBox1.Items.Add(strReturn)
' put code for highlighting here
Dim ind As Integer = ListBox1.FindString(strReturn)
If ind <> -1 Then
ListBox1.SetSelected(ind, True)
End If
End If
Next
If TextBox1.Text <> "" Then
Button1.PerformClick()
End If
End Sub
I could' exactly remember what I changed - too busy. Ok I copy the code for the TextBox1_KeyUp event. Try this:
Edited by Minimalist because: code missing
Start4me 0 Newbie Poster
Works like a charm, howeve, when I tried to make the whole original list to come back when the user completely cleared the textbox, I encountered an issue. I've used this code:
If TextBox1.Text = "" Then
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Add(item)
Next
End If
And if the user continues to press backspace over and over, the same collection will be added over and over. How would I prevent that from happening? (By the way, since my plan is to use multiple different collections, does that mean that the whole code needs to be rewritten to adjust to the collections?)
Minimalist 96 Posting Pro
In line 2 you have to clear the listbox
ListBox1.Items.Clear()
Start4me 0 Newbie Poster
And what about this:
"By the way, since my plan is to use multiple different collections, does that mean that the whole code needs to be rewritten to adjust to the collections?"
How would I recode the whole code that was written above, or I don't need to?
Minimalist 96 Posting Pro
No, you don't need too. The code will work with any collection.
Start4me 0 Newbie Poster
So this is all the code I have:
Public Class Form1
'Declaration
' <BrowsableAttribute(True)> _
Public Property AutoCompleteMode As AutoCompleteMode
Public MySource As New AutoCompleteStringCollection()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Displays the required components in order to perform search of the directory
GroupBox1.Visible = True
Panel1.Visible = True
Panel2.Visible = True
RadioButton0.Checked = True
TextBox1.Focus()
End Sub
'Save Settings when closing form
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
My.Settings.Save()
End Sub
Private Sub RadioButton0_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton0.CheckedChanged
'Displays all of the items in all collecitons
If RadioButton0.Checked = True Then
'Clearazation of all previous items
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Remove(item)
Next
For Each item As String In My.Settings.ListBoxCollection2
ListBox1.Items.Remove(item)
Next
'Initialization of the required component informaiton
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Add(item)
Next
For Each item As String In My.Settings.ListBoxCollection2
ListBox1.Items.Add(item)
Next
'Brings back the focus to the textbox
TextBox1.Focus()
End If
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton1.CheckedChanged
'Displays name item colleciton
If RadioButton1.Checked = True Then
'Clearazation of all previous items
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Remove(item)
Next
For Each item As String In My.Settings.ListBoxCollection2
ListBox1.Items.Remove(item)
Next
'Initialization of the required component informaiton
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Add(item)
Next
'Brings back the focus to the textbox
TextBox1.Focus()
End If
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton2.CheckedChanged
'Displays room number item colleciton
If RadioButton2.Checked = True Then
'Clearazation of all previous items
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Remove(item)
Next
For Each item As String In My.Settings.ListBoxCollection2
ListBox1.Items.Remove(item)
Next
'Initialization of the required component informaiton
For Each item As String In My.Settings.ListBoxCollection2
ListBox1.Items.Add(item)
Next
End If
'Brings back the focus to the textbox
TextBox1.Focus()
End Sub
Private Sub ListBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseClick
'Test of how SelectedItem works
If Me.ListBox1.SelectedItem = "N104" Then
Label1.Text = "exsists"
Else : Label1.Text = "unselected"
End If
End Sub
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
Dim strOr As String, strReturn As String, strL As String, valueL As String
strOr = Trim(TextBox1.Text)
strL = UCase(Trim(TextBox1.Text)) 'hold input in lower case
ListBox1.ClearSelected()
For Each value As String In MySource 'returns the origanal string from list
valueL = UCase(Trim(value)) 'hold string from list in lower case
If InStr(valueL, strL) > 0 And strOr <> "" Then 'now compare only lower cases
strReturn = value ' now we found a match so we use the original string again
ListBox1.Items.Add(strReturn)
' put code for highlighting here
Dim ind As Integer = ListBox1.FindString(strReturn)
If ind <> -1 Then
ListBox1.SetSelected(ind, True)
End If
End If
Next
'If the user clears the textbox, then return the whole previous collection of items
If TextBox1.Text <> "" Then
Dim theLsect As String
theLsect = Nothing
Dim itemList As New List(Of String)
For Each s As String In ListBox1.SelectedItems
itemList.Add(s)
Next
Dim itemArr() As String = itemList.ToArray
Dim SectionArray() As String = itemArr
ListBox1.Items.Clear()
For Each s As String In itemArr
ListBox1.Items.Add(s)
Next
End If
End Sub
End Class
However I was unable to search through the collections because I never assigned the collections respectively to the checked radiobuttons to:
MySource
So I couldn't code this:
If RadioButton1.Checked = True Then
MySource =ListBoxCollection1
End If
I found that if rewriting the code in the TextBox1_KeyUp even:
For Each value As String In MySource 'returns the origanal string from list
valueL = UCase(Trim(value)) 'hold string from list in lower case
If InStr(valueL, strL) > 0 And strOr <> "" Then 'now compare only lower cases
strReturn = value ' now we found a match so we use the original string again
ListBox1.Items.Add(strReturn)
' put code for highlighting here
Dim ind As Integer = ListBox1.FindString(strReturn)
If ind <> -1 Then
ListBox1.SetSelected(ind, True)
End If
End If
Next
to:
If RadioButton1.Checked = True Then
For Each value As String In My.Settings.ListBoxCollection1 'returns the origanal string from list
valueL = UCase(Trim(value)) 'hold string from list in lower case
If InStr(valueL, strL) > 0 And strOr <> "" Then 'now compare only lower cases
strReturn = value ' now we found a match so we use the original string again
ListBox1.Items.Add(strReturn)
' put code for highlighting here
Dim ind As Integer = ListBox1.FindString(strReturn)
If ind <> -1 Then
ListBox1.SetSelected(ind, True)
End If
End If
Next
If TextBox1.Text = "" Then
ListBox1.Items.Clear()
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Add(item)
Next
End If
End If
for each radiobutton, will make the search work. But isn't there a simplier, less coded approach to this?
Start4me 0 Newbie Poster
I think I'm starting to learn how all this works. I've changed
Public MySource As New AutoCompleteStringCollection()
to:
Public MySource As New System.Collections.Specialized.StringCollection
And coded in the TextBox1_KeyUp event:
If RadioButton1.Checked = True Then
MySource = My.Settings.ListBoxCollection1
If TextBox1.Text = "" Then
ListBox1.Items.Clear()
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Add(item)
Next
End If
End If
But I was unable to set MySource to two ListBoxCollections at the same time. I tried someting like:
If RadioButton0.Checked = True Then
MySource = My.Settings.ListBoxCollection1
MySource = My.Settings.ListBoxCollection2
If TextBox1.Text = "" Then
ListBox1.Items.Clear()
For Each item As String In My.Settings.ListBoxCollection1
ListBox1.Items.Add(item)
Next
For Each item As String In My.Settings.ListBoxCollection2
ListBox1.Items.Add(item)
Next
End If
End If
But nothing worked. Does it mean that I have to create another ListBoxCollection and have all items from all ListBoxCollection in one (which I don't want to do)?
Minimalist 96 Posting Pro
Sorry but you completely lost me with the my.settings stuff. I don't understand what you want to achieve with this?
This will not work:
MySource = My.Settings.ListBoxCollection1
MySource = My.Settings.ListBoxCollection2
If assign values to a variable the variable will only hold the last values assigned to it. x=5 and x=7 means that x=7 and not 5 and 7
Since I live in another part of the world there will be delays.
Start4me 0 Newbie Poster
So how would I combine the two ListBoxCollections together? Is there a code that will combine the two collections together? Because I've tried:
Dim col1 As System.Collections.Specialized.StringCollection = My.Settings.ListBoxCollection1
Dim col2 As System.Collections.Specialized.StringCollection = My.Settings.ListBoxCollection2
MySource = col1 + col2
And this didn't work.
Edited by Start4me because: improve my answer
Minimalist 96 Posting Pro
Yes there is somecode here:
http://stackoverflow.com/questions/19473054/vb-net-how-to-combine-2-lists
Start4me 0 Newbie Poster
I've tried to modify the code to:
Dim result As List(Of String) = New List(Of String)
For Each item As String In My.Settings.ListBoxCollection1
For Each item2 As String In My.Settings.ListBoxCollection2
result.Add(item + item2) 'Modify line and add to List
Next
result.Add("") 'Add seperator
TextBox1.Lines = result.ToArray() 'Have to convert to String Array since that is what Lines is
Next
But that didn't work. I am not sure how to code line 6 to combine the two ListBoxCollections.
Start4me 0 Newbie Poster
I've encountered a little bug, and I'm curious if it can be fixed. I'm attaching a photo where you can see what I want to fix.
In scene 1, the textbox is empty.
In scene 2, I search for "d", and I have a list of 2 matching items displayed.
In scene 3, I add "i", and the only result that is shown is "Remaldin, Jade".
In scene 4, I delete the "i", and now I have the same results as in scene 2, but the "Remaldin, Jade" stays on top, instead of going down. But I want the listbox to show the results in scene 4 as in scene 2.
How would I fix this?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.