|
-
Apr 2nd, 2003, 07:24 PM
#1
Thread Starter
New Member
For/Next loop to search a listbox
Hi all,
I'm pretty new to VB. I'm taking my first BV course at my university and I'm having trouble tryin to figure something out.
Here's the problem:
I have a combo box (cboAddresses) with email addresses and a text box(txtDomainSearch). Domain names entered in the txtDomainName need to search the items in cboAddresses by using a For/Next loop and the matches(if there are any) will be displayed in a message box. But I can not get the search to work.
Its frustrating because i know someone else would be able to do this in 2 minutes and I've been spending hours. I thought this was pretty simple and i know it is, but i just cant figure it out. can anyone help??
-
Apr 2nd, 2003, 08:04 PM
#2
Lively Member
VB Code:
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
Dim itemCount As Integer
Dim a As Integer = 0
itemCount = ComboBox1.Items.Count
Do While a < itemCount
ComboBox1.SelectedIndex = a
If (TextBox1.Text.Equals(ComboBox1.Text)) Then
MessageBox.Show("Item Found!")
Return
Else
a += 1
End If
Loop
End Sub
-
Apr 2nd, 2003, 08:08 PM
#3
Member
Or you want to do it with a For...Next Loop you can try this
Private Sub SearchItem()
dim i as integer
For i= 0 to cboAddresses.Item.Count-1
If (cboAddresses.Item(i)=txtDomainSearch.Text) then
messagebox.show ("Item Found!")
end if
Next
end sub
Last edited by StormJason; Apr 2nd, 2003 at 08:14 PM.
-
Apr 2nd, 2003, 08:24 PM
#4
Thread Starter
New Member
Thanks for your help! but its not quite what i need.
Here is a detailed description of the problem:
The Search button will look for the addresses with the domain name the user has specified in the text box and display all such addresses (each on a separate line) in a message box. Search should first check that the user has entered something in the text box. Give an appropriate message, if not. Remember to nest any checks so that only one message displays at a time. If the text box is not empty, use a For/Next loop to check each item in the combo box to see if it has the domain name in the check box. (You won’t need a blnFound here since the entire box must be checked even if you’ve found one.) If the domain name matches, add it to a String that you will later display in a message box with all the domain names. Remember you want each name on a separate line in the message box. You can use &= to keep adding to the String. When you declare this String you’ll have to set it initially to “”. After the loop is finished, check whether there is anything in the String. If not, inform the user via a message box that there are no addresses with that domain name and call Clear Labels. If the String isn’t empty, display a message box with the e-mail addresses that matched. Then call Clear Labels.
Do whatever is necessary to match domain names regardless of case. So if the user enters KENT.EDU it will match kent.edu.
One point about the items from the combo box that you are comparing to the text box has to do with Option Strict On. ComboBox.Items(intIndex) is not regarded as a String. When you enter the For/Next loop you’ll have to use CStr to convert it before you can compare it to the text box Text property which is a String.
THe more I try to figure it out the more confused i get.
THanks.
-
Apr 2nd, 2003, 09:14 PM
#5
Thread Starter
New Member
How do i get For/Next to search strings? THe addresses in the list are considered integers. I know i can use Cstr, but i still cant get it to work.
-
Apr 2nd, 2003, 09:17 PM
#6
Member
i am not sure wheter i fully understand your question or not... but here is another example.. see wheter it would help or not
PHP Code:
Imports System.Text
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
if txtdoma
Dim i As Integer
Dim result as new StringBuilder
If (txtDomainSearch.Text="") Then
MessageBox.Show("Please Enter an Address to Seach!")
Exit Sub
End if
For i = 0 to cboAddresses.Item.Count
If (cboAddresses.Item(i).ToString.ToUpper = txtDomainSearch.Text.ToUpper) then
result.Append (cboAddresses.Item(i).ToString +vbRrLf)
end If
Next
If (result.Length=0) then
MessageBox.Show("No Match Item!")
Else
Meesagebox.Show(result.ToString)
End if
End Sub
-
Apr 2nd, 2003, 09:19 PM
#7
Member
opps...please ignore first line after the Sub... is a wrong typing....
and the "VbRfLf " is suppose to be "VbCrLf"
Hopefully this is what you want
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
|