Results 1 to 6 of 6

Thread: About Listbox and a textbox

  1. #1
    Yan Yaramillo is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2011
    Location
    Panama city, Panama, Panama
    Posts
    3
    Reputation
    0

    About Listbox and a textbox

    Hi!

    Here's my question.

    I'm trying to make an aplication in wich you'll use the textbox to select an item form the listbox
    If the listbox has:

    One
    Two
    Three
    Four

    And if I want to select one of them without using the mouse but only with a textbox then what should I do?

    Well I'm deleting those items that are typed on the textbox one by one.
    I've alredy done this and its actually works with a buttom:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Text.ToUpper()
    ListBox1.Items.RemoveAt(ListBox1.FindString(TextBo x1.Text))
    TextBox1.Clear()
    TextBox1.Focus()
    End Sub

    The problem is that I want to delete in this way:

    If I select form the textbox "Two" then "Four" and "Three" have to be deleted with "TWO" but the others stay in the listbox
    If I select form the textbox "Three" then "Four" has to be deleted with "THREE" but the others stay in the listbox

    I need help please what should I do?


  2. #2
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    11,331
    Reputation
    1543
    Call FindString to get the index of the specific item, then use a For or Do loop to remove the items with that index or higher.

  3. #3
    Yan Yaramillo is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2011
    Location
    Panama city, Panama, Panama
    Posts
    3
    Reputation
    0
    I don't understand!

  4. #4
    Solitaire is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    New York
    Posts
    418
    Reputation
    162

    Use the Keypress event

    Place your code in the Keypress event. When user presses Enter after typing in the text, it will be deleted from the listbox. The Do loop will also delete all items following the one the user typed.


    Code:
        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            Dim sel As Integer
            If e.KeyChar = Chr(13) Then
                sel = ListBox1.FindString(TextBox1.Text)
                 For x As Integer = sel To ListBox1.Items.Count - 1
                    ListBox1.Items.RemoveAt(sel)
                Next
                TextBox1.Clear()
                TextBox1.Focus()
            End If
        End Sub

    If you prefer to use a button, then just leave out the If and End If lines.

  5. #5
    Yan Yaramillo is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2011
    Location
    Panama city, Panama, Panama
    Posts
    3
    Reputation
    0
    Thanks Solitaire, It really helps me a lot!

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,177
    Reputation
    2368
    Quote Originally Posted by Solitaire
    For x As Integer = sel To ListBox1.Items.Count - 1
    ListBox1.Items.RemoveAt(sel)
    Next
    When removing from the collection you're looping you have to loop from last to first Step-1. Or use a Do loop as suggested.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking