4 Replies - 4095 Views - Last Post: 02 March 2011 - 03:50 PM Rate Topic: -----

#1 hifistyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-October 10

moving items from one list box to another

Posted 02 March 2011 - 02:40 PM

So,
I have my random alpha-numeric generator giving me a list of 500 items. I need to be able to search the "random Plate listbox" by entering into a textbox. Partial matches will be displayed in listbox1.
My code for searching the "random plate listbox" is not returning results. Could it be a code problem or something else? any advice appreciated.
John

Public Class PlateForm
    Const numEntries As Integer = 500
    Dim lb As New List(Of String)
    Private _inStr As Boolean

    Private Property InStr(ByVal p1 As String) As Boolean
        Get
            Return _inStr
        End Get
        Set(ByVal value As Boolean)
            _inStr = value
        End Set
    End Property

    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) Handles MyBase.Load

        For x As Integer = 1 To numEntries
            Dim foo As String = generatePlateNum()
            If Not lb.Contains(foo) Then lb.Add(foo)
        Next
        RandListBox.DataSource = lb
    End Sub

    Const validCH As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    Dim prng As New Random

    Private Function generatePlateNum(Optional ByVal reqLen As Integer = 7) As String
        Dim sb As New System.Text.StringBuilder

        For x As Integer = 1 To 7
            sb.Append(validCH(prng.Next(validCH.Length)))
        Next
        Return sb.ToString
    End Function

    Private Sub searchBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchBtn.Click
        ListBox1.Items.Clear()
        Dim listlength As Integer = (RandListBox.Items.Count - 1)
        Dim i As Integer
        Dim listString, newString As String
        Dim Counter As Integer

        If InStr(TextBox1.Text) Then
            RandListBox.Items.Add(ListBox1.Items.Item(i))
        End If

        listString = Nothing
        Counter = 0
        newString = Nothing

        
    End Sub


    Private Sub exitBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitBtn.Click
        Me.Close()

    End Sub

    Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter


    End Sub


End Class 


Is This A Good Question/Topic? 0
  • +

Replies To: moving items from one list box to another

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: moving items from one list box to another

Posted 02 March 2011 - 03:00 PM

The first thing I see is that you are trying to add to RandListBox when that is what you populated with the random strings.

Also, there is a FindString method you can use.

Replace your searchButton code with this and see if it gives you better results.
    Private Sub searchBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchBtn.Click
        ListBox1.Items.Clear()
        Dim idx As Integer = randListBox.FindString(TextBox1.Text)

        If idx > -1 Then '-1 means Nothing Found
            ListBox1.Items.Add(randListBox.Items.Item(idx))
        End If
    End Sub


Edited. After looking deeper, this is all you would need

Also got to thinking that you might not know the first par and FindString looks for strings that start with what you enter. So if you're wanting a partial match you could do it this way instead

     ListBox1.Items.Clear()

        For x As Integer = 0 To randListBox.Items.Count - 1
            If randListBox.Items.Item(x).ToString.Contains(TextBox1.Text) Then
                ListBox1.Items.Add(randListBox.Items.Item(x))
            End If
        Next

This post has been edited by CharlieMay: 02 March 2011 - 03:08 PM

Was This Post Helpful? 1
  • +
  • -

#3 hifistyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-October 10

Re: moving items from one list box to another

Posted 02 March 2011 - 03:14 PM

Hi,
I stand corrected. It does match when I enter text into the textbox1, however, I'm looking for it to give me more than one match, or matches of several letter-number combos.

This post has been edited by hifistyle: 02 March 2011 - 03:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: moving items from one list box to another

Posted 02 March 2011 - 03:42 PM

The second version of the code should do that for you. It will search the entire listbox and add anything that contains what you type to the other listbox.

I should note that this search is case sensitive by default, and if you don't have your textbox set to uppercase then you need to add .ToUpper after the .Text for the textbox in the contains parameters.

This post has been edited by CharlieMay: 02 March 2011 - 03:48 PM

Was This Post Helpful? 1
  • +
  • -

#5 hifistyle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 11-October 10

Re: moving items from one list box to another

Posted 02 March 2011 - 03:50 PM

It works as you say!

My thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1