LCKB
find strings in listbox - Printable Version

+- LCKB (https://lckb.dev/forum)
+-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109)
+--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196)
+---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192)
+---- Thread: find strings in listbox (/showthread.php?tid=2092)



- soryjero - 08-01-2013


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.




- ReturnKratos - 08-01-2013


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");
}




- soryjero - 08-01-2013

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




- ReturnKratos - 08-01-2013

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




- Wizatek - 08-01-2013

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




- someone - 08-01-2013


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




- soryjero - 08-01-2013



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.




- soryjero - 08-02-2013


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.