By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,610 Members | 2,110 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,610 IT Pros & Developers. It's quick & easy.

Help with Combobox Autofill Code

P: n/a
I have some comboboxes that are created dynamically at run time based
on user actions. I found a procedure on a message board to autofill
the combobox text from the dataview that the comboboxes are bound to.
It works perfectly on the comboboxes that I have created at design
time, but the ones that I create runtime behave differently.

The problem is that when the user tabs onto the combobox, the box
stays blank, which is fine. However, once they press any key, the
first value in the bound dataview appears in the box and the key that
they pressed appears at the end of this text. For example, let's say
that the first value in the bound dataview is the string "example".
If the user hits the letter "a", the box immediately displays
"examplea", which forces the user to backspace all the way to clear
out this unwanted value and then begin typing their string again. At
that point, everything autofills fine, but it's obviously
unaccepatable to force every entry to begin with hitting a key and
then backspacing.

The combobox is created with this code:

Dim newCbo As New ComboBox
With newCbo
.Name = "cboUOM" & intCount + 1
.DataSource = vueUOM
.DisplayMember = "USUOM"
End With
AddHandler newCbo.Validating, AddressOf ValNewCbo
AddHandler newCbo.KeyPress, AddressOf FillUOMCbo
AddHandler newCbo.LostFocus, AddressOf ActiveIngrRow
AddHandler newCbo.GotFocus, AddressOf ActiveIngrRow
The dataview looks like this and is part of the form load procedure:
Dim vueUOM As New DataView
vueUOM.Table = ds.UOMConv
vueUOM.Sort = "USUOM"
The handler that takes care of the autofill function is this:

Private Sub FillUOMCbo(ByVal sender As Object, ByVal e As
Dim APIRetVal As Integer
Dim sText As String
Dim thisCbo As ComboBox = DirectCast(sender, ComboBox)
With thisCbo
If Not (e.KeyChar = Convert.ToChar(Keys.Back) Or e.KeyChar
= Convert.ToChar(Keys.Escape) Or e.KeyChar =
Convert.ToChar(Keys.Enter)) Then
If .SelectionLength 0 Then
sText = .Text.Substring(0, .SelectionStart) &
sText = .Text & e.KeyChar
End If
APIRetVal = SendMessage(.Handle, CB_SELECTSTRING, -1,
If APIRetVal <-1 Then
.SelectionStart = sText.Length
.SelectionLength = .Text.Length - .SelectionStart
+ 1
.Text = sText
.SelectionStart = sText.Length + 1
End If
e.Handled = True
End If
End With
End Sub

Also note that this is declared in the class declarations:

Public Const CB_SELECTSTRING = &H14D
' API Declarations
Public Declare Function SendMessage Lib "user32" Alias
"SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal
wParam As Integer, ByVal lParam As String) As Integer
Can anybody see why the autofill is behaving in the manner that it
is? I just don't see what I need to do to make it work as intended.


Jun 29 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.