Hi, All,

Greetings!

Is this doable? I have a textbox for email address entry, now, when I type the email address and reach the @ character, there will be a dropdown suggesting common domains like msn.com,hotmail.com, aol.com, yahoo.com and etc.

I hope you can help me. Thanks in advance.

Recommended Answers

All 2 Replies

Sure it is doable! You just have to code it. :))

Here is a crude implementation of a custom textbox with this capability. You have to double click on a item to select it. You could change that to single click if you want. You may need to change the "@" detection algorithm. It works with a standard US keyboard with "@" = shift-2.

There is no error checking; I leave that to you.

Public Class AutoCompleteDropDownTB
   Inherits TextBox
   Public WithEvents dropdown As New ListBox
   Private frmDropDown As New Form

   Public Sub New()
      dropdown.Parent = frmDropDown
      dropdown.SelectionMode = SelectionMode.One

      With frmDropDown
         .FormBorderStyle = FormBorderStyle.None
         .Size = dropdown.Size
         .Visible = False
         .StartPosition = FormStartPosition.Manual
         .TopLevel = True
         .TopMost = True
      End With
   End Sub

   Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
      If e.Shift AndAlso e.KeyCode = Keys.D2 AndAlso Not Text.Contains("@"c) Then
         ShowDropDown()
      End If
      MyBase.OnKeyDown(e)
   End Sub

   Private Sub ShowDropDown()
      dropdown.SelectedIndex = 0 ' make sure 1st item is selected
      Dim offset As Int32
      If BorderStyle = Windows.Forms.BorderStyle.Fixed3D Then
         offset = 2
      Else
         offset = 0
      End If

      With frmDropDown
         .Location = PointToScreen(New Point(0, Size.Height - offset))
         .Show()
      End With
   End Sub

   Private Sub dropdown_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropdown.DoubleClick
      frmDropDown.Hide()
      Text &= dropdown.SelectedItem.ToString
      SelectionStart = TextLength
   End Sub

   Private Sub dropdown_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles dropdown.LostFocus
      frmDropDown.Hide()
   End Sub
End Class

Just add this to your project and rebuild. It should show up at the top of your toolbox.

Example usage:

Public Class Form1

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

      ' add the dropdown list items

      With AutoCompleteDropDownTB1.dropdown.Items
         .Add("fred.com")
         .Add("wilma.com")
         .Add("barney.com")
      End With
   End Sub
End Class

Great code TnTinMN, thank you so much. This is now solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.