Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

looking for example code to make own combo box

15 views
Skip to first unread message

Bob

unread,
Oct 17, 2002, 1:53:26 PM10/17/02
to
I'm looking for some good code examples to make my own combo box control
class. In particular I'd like to see how to achieve autocomplete, and have
the dropdown portion of the control appear outside the containing window if
necessary without the window losing focus.

TIA,
Bob


Keith

unread,
Oct 17, 2002, 2:48:51 PM10/17/02
to
Here's some code that I found for autocomplete:

Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
Me.ComboBox1.Text = ""
Me.ComboBox1.Items.Add("a")
Me.ComboBox1.Items.Add("aaa")
Me.ComboBox1.Items.Add("combo")
Me.ComboBox1.Items.Add("combobox")
Me.ComboBox1.Items.Add("combobox test")
Me.ComboBox1.Items.Add("common")
Me.ComboBox1.Items.Add("common dialog")
End Sub

Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal
e As System.Windows.Forms.KeyEventArgs) Handles
ComboBox1.KeyUp

Dim index As Integer
Dim actual As String
Dim found As String

' Do nothing for some keys such as navigation keys.
If ((e.KeyCode = Keys.Back) Or _
(e.KeyCode = Keys.Left) Or _
(e.KeyCode = Keys.Right) Or _
(e.KeyCode = Keys.Up) Or _
(e.KeyCode = Keys.Delete) Or _
(e.KeyCode = Keys.Down) Or _
(e.KeyCode = Keys.PageUp) Or _
(e.KeyCode = Keys.PageDown) Or _
(e.KeyCode = Keys.Home) Or _
(e.KeyCode = Keys.End)) Then

Return
End If

' Store the actual text that has been typed.
actual = Me.ComboBox1.Text

' Find the first match for the typed value.
index = Me.ComboBox1.FindString(actual)

' Get the text of the first match.
If (index > -1) Then
found = Me.ComboBox1.Items(index).ToString()

' Select this item from the list.
Me.ComboBox1.SelectedIndex = index

' Select the portion of the text that was
automatically
' added so that additional typing will replace
it.
Me.ComboBox1.SelectionStart = actual.Length
Me.ComboBox1.SelectionLength = found.Length
End If

End Sub

>.
>

Bob

unread,
Oct 17, 2002, 2:43:51 PM10/17/02
to
I shoud back up a bit and ask a more basic question, since the answer to
which may be enough to allow me to just subclass the existing combo box. I
am having a problem using the 'enter' key to finish editing. In particular,
the combo box beeps. Now I have already gotten around this problem when
using a single-line textbox control by just handling the keypress. But the
combo box ignores such handling and beeps anyway! Check out this code. Put
focus in the textbox control and you will notice that indeed there is no
beeping when you hit enter. But press enter when either of the two combo
boxes have focus, and they still beep. Is this a bug?

TIA,
Bob

Option Strict On

Module Main

Private WithEvents frm As Form
Private WithEvents cb As ComboBox
Private WithEvents tx As TextBox
Private scb As SubCombo

Sub main()
frm = New Form()
frm.Show()
cb = New ComboBox()
frm.Controls.Add(cb)
tx = New TextBox()
frm.Controls.Add(tx)
tx.Left = cb.Left + cb.Width + 10
cb.Items.Add("test1")
cb.Items.Add("test2")
cb.Items.Add("test3")
cb.Items.Add("test4")
cb.Items.Add("test5")
scb = New SubCombo()
frm.Controls.Add(scb)
scb.Top = cb.Top + cb.Height + 10
scb.Items.Add("test1")
scb.Items.Add("test2")
scb.Items.Add("test3")
scb.Items.Add("test4")
scb.Items.Add("test5")
Application.Run()
End Sub

Public Sub frm_Closed( _
ByVal sender As Object, ByVal e As System.EventArgs _
) Handles frm.Closed
Application.Exit()
End Sub

Public Class SubCombo
Inherits ComboBox

Protected Overrides Sub OnKeyPress( _
ByVal e As System.Windows.Forms.KeyPressEventArgs _
)
Select Case e.KeyChar
Case Convert.ToChar(Keys.Return)
e.Handled = True
Case Else
MyBase.OnKeyPress(e)
End Select
End Sub

End Class

Public Sub cb_KeyPress( _


ByVal sender As Object, ByVal e As

System.Windows.Forms.KeyPressEventArgs _
) Handles cb.KeyPress
Select Case e.KeyChar
Case Convert.ToChar(Keys.Return)
e.Handled = True
End Select
End Sub

Public Sub tx_KeyPress( _


ByVal sender As Object, ByVal e As

System.Windows.Forms.KeyPressEventArgs _
) Handles tx.KeyPress
Select Case e.KeyChar
Case Convert.ToChar(Keys.Return)
e.Handled = True
End Select
End Sub

End Module


"Bob" <noe...@nospam.net> wrote in message
news:OsBTbXgdCHA.620@tkmsftngp12...

John Eikanger [MS]

unread,
Oct 17, 2002, 8:12:41 PM10/17/02
to
Hi, Bob

Something odd is going on here. We are still looking at this, but here is
something you can try short term:

Place an invisible command button on the form and set the AcceptButton
property to that button. When the return key for any controls on the form,
the button will grab it before the control can process it.

HTH,

John Eikanger
Microsoft Visual Basic Developer Support

This posting is provided “AS IS” with no warranties, and confers no rights.
(c) 2002 Microsoft Corporation. All rights reserved.
--------------------
| From: "Bob" <noe...@nospam.net>
| References: <OsBTbXgdCHA.620@tkmsftngp12>
| Subject: Re: looking for example code to make own combo box
| Date: Thu, 17 Oct 2002 14:43:51 -0400
| Lines: 105
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <u8RtnzgdCHA.2408@tkmsftngp11>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: 199.2.136.1
| Path: cpmsftngxa06!tkmsftngp01!tkmsftngp11
| Xref: cpmsftngxa06 microsoft.public.dotnet.languages.vb:69746
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Bob

unread,
Oct 18, 2002, 10:42:13 AM10/18/02
to
Nice. I hadn't thought of that.

Thanks a lot,
Bob

Option Strict On

Module Main

Private WithEvents frm As Form

Private scb As SubCombo

Sub main()
frm = New Form()
frm.Show()

scb = New SubCombo()
frm.Controls.Add(scb)

scb.Items.Add("test1")
scb.Items.Add("test2")
scb.Items.Add("test3")
scb.Items.Add("test4")
scb.Items.Add("test5")
Application.Run()
End Sub

Public Sub frm_Closed( _
ByVal sender As Object, ByVal e As System.EventArgs _
) Handles frm.Closed
Application.Exit()
End Sub

Public Class SubCombo
Inherits ComboBox

Private WithEvents btn As Button
Private OldAcceptButton As IButtonControl
Private OldCancelButton As IButtonControl

Public Sub New()
btn = New Button()
Me.Controls.Add(btn)
btn.Visible = False
End Sub

Protected Overrides Sub OnKeyPress( _
ByVal e As System.Windows.Forms.KeyPressEventArgs _
)
Select Case e.KeyChar
Case Convert.ToChar(Keys.Return)
e.Handled = True
Case Else
MyBase.OnKeyPress(e)
End Select
End Sub

Protected Overrides Sub OnKeyDown( _
ByVal e As System.Windows.Forms.KeyEventArgs _
)
Select Case e.KeyCode
Case Keys.Return


e.Handled = True
End Select
End Sub

Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
OldAcceptButton = Me.FindForm.AcceptButton
Me.FindForm.AcceptButton = btn
OldCancelButton = Me.FindForm.CancelButton
Me.FindForm.CancelButton = btn
End Sub

Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
Me.FindForm.AcceptButton = OldAcceptButton
Me.FindForm.CancelButton = OldCancelButton
End Sub

End Class

End Module


"John Eikanger [MS]" <joh...@online.microsoft.com> wrote in message
news:eEA3nrjdCHA.2676@cpmsftngxa06...

John Eikanger [MS]

unread,
Oct 18, 2002, 6:45:25 PM10/18/02
to
Hi, Bob

Here's the way to do it, or as close to right as I'm going to get.
Implement the code in Knowledge Base article Q320583. Here is a link to it:

HOW TO: Trap Keystrokes in .NET Controls by Using Visual Basic .NET
http://support.microsoft.com/default.aspx?scid=kb;[LN];Q320583

I have commented the article to add the keyword "Hook". If, like me, you
like to do things the hard way first, Knowledge Base article Q311317
includes an example of how to hook the WndProc associated with an hWnd and
filter the message before they get to the combo box. The problem with this
approach is that the editable portion of the combobox is a separate window
from the rest of the control. The Handle property of the combobox does not
point that the edit window, so you need to call GetWindow to get the handle
of the editable part of the control.

HTH,

John Eikanger
Microsoft Visual Basic Team

This posting is provided “AS IS” with no warranties, and confers no rights.
(c) 2002 Microsoft Corporation. All rights reserved.
--------------------

| From: "Bob" <noe...@nospam.net>
| References: <OsBTbXgdCHA.620@tkmsftngp12> <u8RtnzgdCHA.2408@tkmsftngp11>
<eEA3nrjdCHA.2676@cpmsftngxa06>


| Subject: Re: looking for example code to make own combo box

| Date: Fri, 18 Oct 2002 10:42:13 -0400
| Lines: 104
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Bob

unread,
Oct 19, 2002, 2:17:27 PM10/19/02
to
Aha. Much simpler.

Protected Overrides Function ProcessCmdKey( _
ByRef msg As System.Windows.Forms.Message, _
ByVal keyData As System.Windows.Forms.Keys _
) As Boolean

Const WM_KEYDOWN As Integer = &H100
Const WM_SYSKEYDOWN As Integer = &H104
If ((msg.Msg = WM_KEYDOWN) _
Or (msg.Msg = WM_SYSKEYDOWN)) Then
Select Case keyData
Case Keys.Enter, Keys.Enter Or Keys.Control, _
Keys.Enter Or Keys.Shift, Keys.Enter Or Keys.Alt
msg = Nothing
End Select
End If

End Function

In one portion of the Knowledge Base article ("Implement the Overridden
Method") I see

Case (Keys.Alt And Keys.Z)
Console.WriteLine("<ALT> + z Captured")

And in a subsequent portion ("Build an Example") I see

Case (Keys.Alt Or Keys.Z)
Me.Parent.Text = "<ALT> + Z Captured"

'OR' seems to work, but not 'AND'. Perhaps I found a mistake?

Thanks very much for your help!
Bob

"John Eikanger [MS]" <joh...@online.microsoft.com> wrote in message

news:8wFNkfvdCHA.2280@cpmsftngxa08...

John Eikanger [MS]

unread,
Oct 21, 2002, 3:16:17 PM10/21/02
to
Hi, Bob

Good catch. It should be OR all around. I have logged the content change
request.

Thanks,

John Eikanger
Microsoft Visual Basic Team

This posting is provided “AS IS” with no warranties, and confers no rights.
(c) 2002 Microsoft Corporation. All rights reserved.
--------------------

| From: "Bob" <noe...@nospam.net>


| Subject: Re: looking for example code to make own combo box

| Date: Sat, 19 Oct 2002 14:17:27 -0400
| Lines: 70
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb

0 new messages