Results 1 to 7 of 7

Thread: For/Next loop to search a listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    3

    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??

  2. #2
    Lively Member
    Join Date
    Nov 2002
    Location
    Malaysia
    Posts
    124
    VB Code:
    1. Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
    2.         Dim itemCount As Integer
    3.         Dim a As Integer = 0
    4.         itemCount = ComboBox1.Items.Count
    5.         Do While a < itemCount
    6.             ComboBox1.SelectedIndex = a
    7.             If (TextBox1.Text.Equals(ComboBox1.Text)) Then
    8.                 MessageBox.Show("Item Found!")
    9.                 Return
    10.             Else
    11.                 a += 1
    12.             End If
    13.         Loop
    14.     End Sub

  3. #3
    Member
    Join Date
    May 2002
    Location
    Malaysia
    Posts
    45
    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    3
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Posts
    3
    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.

  6. #6
    Member
    Join Date
    May 2002
    Location
    Malaysia
    Posts
    45
    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.ObjectByVal e As System.EventArgsHandles 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 
    0 to cboAddresses.Item.Count
    If (cboAddresses.Item(i).ToString.ToUpper txtDomainSearch.Text.ToUpperthen
    result
    .Append (cboAddresses.Item(i).ToString +vbRrLf)
    end If
    Next

    If (result.Length=0then 
    MessageBox
    .Show("No Match Item!")
    Else
    Meesagebox.Show(result.ToString)
    End if

    End Sub 

  7. #7
    Member
    Join Date
    May 2002
    Location
    Malaysia
    Posts
    45
    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
  •  



Click Here to Expand Forum to Full Width