find strings in listbox
#1

hello,

 

i am trying to find indexes of items from a string.

 

i introduce a string in a textbox and click on a search button which has this function:

listBox1.FindString(textBox1.Text)

but with this, i only get first index.

 

how could i do to get all indexes?

 

i have tried

int[] idxs = listBox1.FindString(textBox1.Text)

but idxs is array type and listBox1.FindString(textBox1.Text) is int type, so i get an error.

 

could anyone help me please?

 

thanks in advance.

#2

i usually use this (vb.net)

Dim searchString As String = TextBox4.Text
Dim index As Integer = UserList.FindString(searchString)
If string.contains = true then, else error.
If index <> -1 Then
UserList.SetSelected(index, True)
Else
MsgBox("Not Found")
End If

may be in c# can be like :

string searchString = TextBox4.Text;
int index = UserList.FindString(searchString);
// If string.contains = true then, else error.
if (index != -1) {
UserList.SetSelected(index, true);
} else {
Interaction.MsgBox("Not Found");
}

#3
the problem of this is i only get one index and i want to get all indexes that contains char i have introduced in textbox

#4
ah, ok i undertand but i cant help you. i think you must create e list where put all indexes founded in it then show in the listbox, wait someone help you as well tham me Smile

#5
I usually keep all listbox item in a List and when i search for something in the list u can loop trough that list and add everything that matches to the listbox

#6

According to MSDN:

2

 

Finds the first item in the 2 that starts with the specified string.

 

FindString has 2 overloads:

2

Have you looked at this:

2

 

Finds the first item in the 2 that starts with the specified string. The search starts at a specific starting index.

List<int> foundIndexes = new List<int>();
int index = -1, firstIndex = -1;
while ((index = listBox1.FindString("test", index)) !=-1){
if(index == firstIndex ){
break;
}
if (firstIndex == -1){
firstIndex = index;
}
foundIndexes.Add(index);
}

FindString only looks for string that starts with your string, if you want to search for strings that are exactly like your string use FindStringExact:

2

2

#7


According to MSDN:

2

 

FindString has 2 overloads:

2

Have you looked at this:

2

List<int> foundIndexes = new List<int>();
int index = -1, firstIndex = -1;
while ((index = listBox1.FindString("test", index)) !=-1){
if(index == firstIndex ){
break;
}
if (firstIndex == -1){
firstIndex = index;
}
foundIndexes.Add(index);
}

FindString only looks for string that starts with your string, if you want to search for strings that are exactly like your string use FindStringExact:

2

2

 

thanks. tomorrow i will try it and i post here.

#8

I usually keep all listbox item in a List<string> and when i search for something in the list u can loop trough that list and add everything that matches to the listbox

 

 

According to MSDN:

2

 

FindString has 2 overloads:

2

Have you looked at this:

2

List<int> foundIndexes = new List<int>();
int index = -1, firstIndex = -1;
while ((index = listBox1.FindString("test", index)) !=-1){
if(index == firstIndex ){
break;
}
if (firstIndex == -1){
firstIndex = index;
}
foundIndexes.Add(index);
}

FindString only looks for string that starts with your string, if you want to search for strings that are exactly like your string use FindStringExact:

2

2

 

 

thanks again someone and wiza.

 

works perfectly.



Forum Jump:


Users browsing this thread: 1 Guest(s)