About Listbox and a textbox

Yan Yaramillo

New member
Joined
Aug 13, 2011
Messages
3
Location
Panama city, Panama, Panama
Programming Experience
Beginner
:tears_of_joy: 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(TextBox1.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

:confused: I need help please what should I do?

 
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.


VB.NET:
    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.
 
Solitaire said:
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.
 
Back
Top