1 Replies - 6964 Views - Last Post: 21 March 2010 - 08:39 PM Rate Topic: -----

#1 MilleLacs  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-March 10

Visual Basic, Creating a find button to search a list box

Posted 21 March 2010 - 05:43 PM

I am new to Visual Basic.
A TA at my college recommended Dream In Code as a resource that is available for help with coding problems.

I have been trying to figure out how to get my code to work. (It was a homework assignment which has already been turned in but I am determined to get this code to work.)

I have a text box titled txtSearch and btnClick.
I need to search a list box for a city name and then have it displayed in a message box. If there is not a match then I need to display an alternative message box stating there is no match. City is in the list box starting at 16.

The text needs to be case insensitive.
Any help would be appreciated.

I am only getting the message "That is not a valid city." for any entry that I type into my txtSearch even though I know it should be a match.

Here is what I have for code
  Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

        ' Declare variables to find text on txtSearch when button clicked
        Dim length As Integer = txtSearch.TextLength
        Dim value As String = txtSearch.Text.ToUpper
        Dim Selected As String
        Dim ssstring As String

        'Loops through Weather list box to find matching city

        For i As Integer = 0 To lstWeather.Items.Count - 1
            lstWeather.SelectedIndex = i
            Selected = lstWeather.SelectedItem.ToString.ToUpper
            ssstring = Selected.Substring(16, length)
            If value = ssstring Then
                ' If ssstring = value Then

                ShowMessage("City Record Found")
            Else
                MessageBox.Show(" That is not a valid city.", "Information", MessageBoxButtons.OK, _
                                 MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

                'Replaces the search criteria with original text
                txtSearch.Text = ("Search city by 1st letter.")
                'Change the color of the search box to Blue
                txtSearch.ForeColor = Color.Blue
                Exit Sub

            End If
        Next

    End Sub




Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Visual Basic, Creating a find button to search a list box

#2 Kinwang2009  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 76
  • Joined: 07-February 10

Re: Visual Basic, Creating a find button to search a list box

Posted 21 March 2010 - 08:39 PM

View PostMilleLacs, on 21 March 2010 - 04:43 PM, said:

I am new to Visual Basic.
A TA at my college recommended Dream In Code as a resource that is available for help with coding problems.

I have been trying to figure out how to get my code to work. (It was a homework assignment which has already been turned in but I am determined to get this code to work.)

I have a text box titled txtSearch and btnClick.
I need to search a list box for a city name and then have it displayed in a message box. If there is not a match then I need to display an alternative message box stating there is no match. City is in the list box starting at 16.

The text needs to be case insensitive.
Any help would be appreciated.

I am only getting the message "That is not a valid city." for any entry that I type into my txtSearch even though I know it should be a match.

Here is what I have for code
  Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

        ' Declare variables to find text on txtSearch when button clicked
        Dim length As Integer = txtSearch.TextLength
        Dim value As String = txtSearch.Text.ToUpper
        Dim Selected As String
        Dim ssstring As String

        'Loops through Weather list box to find matching city

        For i As Integer = 0 To lstWeather.Items.Count - 1
            lstWeather.SelectedIndex = i
            Selected = lstWeather.SelectedItem.ToString.ToUpper
            ssstring = Selected.Substring(16, length)
            If value = ssstring Then
                ' If ssstring = value Then

                ShowMessage("City Record Found")
            Else
                MessageBox.Show(" That is not a valid city.", "Information", MessageBoxButtons.OK, _
                                 MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

                'Replaces the search criteria with original text
                txtSearch.Text = ("Search city by 1st letter.")
                'Change the color of the search box to Blue
                txtSearch.ForeColor = Color.Blue
                Exit Sub

            End If
        Next

    End Sub




Thanks


Hi millelacs,
Here is my Help but in different method. Any how this should provide some help how to do it. Because of the shortage of time i cant go through your code but i will try to figure out your errors when i get time.

Hope my source code helps.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1