List box on multi-line textbox

decrypt

Junior Contributor
Joined
Oct 6, 2003
Messages
216
I am trying to create something similiar to that which is featured in Visual Studio.net. I am sure you are very familiar with the list box that appears when you type in something like "textbox1." and then it displays a list of possible endings to the line. Basically what I want to create is something that does this exact same thing, except I tell it what to say. So far, I have a program similiar to that of notepad. What I want it to do, is as I put a period in, it reads the word prior to the period, and if anything is available for the word, it displays a list box just like that in the vs.net development environment. I can go up and down the list with the arrow keys, and as I type in the word after the period, it narrows down on possible things it could be.

So basically, it has to act exactly like how it works in the visual studio development environment, except it runs on my program, and I define everything.

For example:
If I were to type in hello in the notepad I created, and then put a period on the end of it, it would display all the results that I programmed it to give:

hello.text
hello.blah
hello.blah2
hello.blah3
hello.listgoeson

I hope I have explained this well enough, and I was wondering if someone could point me in the direction of what I could use.

Thanks in advanced,
decrypt
 
Visual Basic:
            lst.Location = New Point(TextBox1.Left + TextBox1.Width / 2, TextBox1.Top + TextBox1.Height / 2)
            lst.Visible = True

            AddHandler lst.SelectedIndexChanged, AddressOf GetSelection

            Me.Controls.Add(lst)

            lst.BringToFront()

at this point in the code, I'm sure it's supposed to display the listbox when I type in "Hello.", but nothing happens...
 
decrypt said:
Visual Basic:
            lst.Location = New Point(TextBox1.Left + TextBox1.Width / 2, TextBox1.Top + TextBox1.Height / 2)
            lst.Visible = True

            AddHandler lst.SelectedIndexChanged, AddressOf GetSelection

            Me.Controls.Add(lst)

            lst.BringToFront()

at this point in the code, I'm sure it's supposed to display the listbox when I type in "Hello.", but nothing happens...
Is "Hello." the only thing you've typed? I believe the code looks for a space before it so if it's the only thing it won't pop up anything
 
oh, ok, I see how it works now...

Thanks a lot for the code :)

edit:

the only thing I can't figure out is how to get the listbox to follow the text...
 
Last edited:
Visual Basic:
Private Sub txtMain_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMain.KeyPress
        If e.KeyChar = "." Then
            Dim lst As New ListBox
            Dim index As Integer
            Dim key As String

            If txtMain.SelectionStart - 1 = -1 Then
                Exit Sub
            End If

            index = txtMain.SelectionStart - 1

            Do While txtMain.Text.Substring(index, 1) <> " "
                index -= 1
                If index = -1 Then Return
            Loop

            key = txtMain.Text.Substring(index + 1, txtMain.SelectionStart - index - 1)

            lst.Location = New Point(txtMain.SelectionStart, 200)

            'here
            If key.ToLower = "hello" Then
                lst.Items.Add("hello")
                lst.Items.Add("second")
                lst.Items.Add("what")
                lst.Visible = True
                lst.SelectedIndex = 0
            ElseIf key.ToLower = "what" Then
                lst.Items.Add("what")
                lst.Items.Add("google")
                lst.Visible = True
                lst.SelectedIndex = 0
            Else
                Exit Sub
            End If

            'here

            AddHandler lst.KeyDown, AddressOf lstKeyDown
            AddHandler lst.MouseDown, AddressOf lstMouseDown
            AddHandler txtMain.KeyDown, AddressOf txtMainKeyDown

            Me.Controls.Add(lst)
            lst.BringToFront()
            lst.ScrollAlwaysVisible = True
            lst.Select()
        End If
    End Sub
    Private Sub lstKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Enter Then
            If CType(sender, ListBox).SelectedIndex > -1 Then

                Dim index As Integer = txtMain.SelectionStart

                txtMain.Text = txtMain.Text.Insert(txtMain.SelectionStart, CType(sender, ListBox).SelectedItem)

                txtMain.SelectionStart = index + CType(CType(sender, ListBox).SelectedItem, String).Length

                Me.Controls.Remove(sender)

            End If
        End If
    End Sub
    Private Sub lstMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If CType(sender, ListBox).SelectedIndex > -1 Then

            Dim index As Integer = txtMain.SelectionStart

            txtMain.Text = txtMain.Text.Insert(txtMain.SelectionStart, CType(sender, ListBox).SelectedItem)

            txtMain.SelectionStart = index + CType(CType(sender, ListBox).SelectedItem, String).Length

            Me.Controls.Remove(sender)

        End If
    End Sub
    Private Sub txtMainKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        If e.KeyCode = Keys.Space Then
        End If
    End Sub

Ok, I moved around your code a lot to make it more understandable for myself. Now, there is one major thing that I can't seem to work out. How could I be able to type on the main form, and at the same time, be able to move up and down on the list box with the up and down arrow keys. Also, while I'm typing, it would go down in the listbox to the letter that it starts with. So when I type in something like 'Pizza.he', it would highlight the topmost thing in the listbox that starts with 'he'.

I have also changed it to a richtextbox instead of a textbox
 
Last edited:
If I'm understanding you correctly you won't be able to do the first thing. Only one control can have focus at one time so you can't type in a textbox and use the arrow keys to move around in the list unless you handle the arrow keys in one of the textbox's Key events.

As for the second part you can do something like:

ListBox1.SelectedIndex = ListBox1.FindString(TextBox1.Text)
 
thanks, a lot I got it working. I even got it to input what I type on the textbox while navigating through. Thanks a lot everybody.

The only thing I cannot figure out, is how to position the listbox with the current selectedindex on the richtextbox.
 
Last edited:
Back
Top