|
-
Mar 14th, 2023, 08:34 PM
#1
Thread Starter
Lively Member
[RESOLVED] how to create a customized combo box
I'm building a VB.Net WinForms application in VS 2017. What I want to do is create a customized combo box, instances of which will be dynamically added to a form by code at run time as needed. The customizations I need are:
1. preset the values of several existing properties (DropDownStyle, Top, Width, Left, etc.) in the constructor
2. create a few new properties, the values of which will be passed to the constructor and set there
3. add a few public methods
4. add custom handling of the OnKeyPress event, to launch custom code when the user presses the Insert or Delete keys.
I know how to do all this with individual combo boxes that have been manually dragged onto the form from the toolbox. But since this application will be creating combo boxes dynamically in code, I'm not sure how to code the above functionality once and then have it automatically apply to each instance. It's unlikely that I will need this functionality elsewhere, so I'm just looking for the simplest most bulletproof way to get the job done in this project.
I'm pretty sure I could do all this with the stock combo box, but am not sure how to hook up my custom code to the OnKeyPress event. I've also considered creating a class or a user control, but am not sure what the best approach is.
Thanks in advance for any suggestions.
-
Mar 14th, 2023, 09:47 PM
#2
Re: how to create a customized combo box
 Originally Posted by silverblatt
1. preset the values of several existing properties (DropDownStyle, Top, Width, Left, etc.) in the constructor
If you use a standard control, you can obviously set those properties in the designer or in code. To make default values other than those of the standard ComboBox class, you need a custom control.
 Originally Posted by silverblatt
2. create a few new properties, the values of which will be passed to the constructor and set there
You obviously can't add properties to an existing class and you can't add a constructor either. That would require a custom control.
 Originally Posted by silverblatt
3. add a few public methods
You can write extension methods for an existing type and call them as though they were members, but they will only have access to public members. For actual member methods that can also access protected members, you need a custom control.
 Originally Posted by silverblatt
4. add custom handling of the OnKeyPress event, to launch custom code when the user presses the Insert or Delete keys.
There is no OnKeyPress event. There's a KeyPress event and an OnKeyPress method. If you use a standard ComboBox, you can obviously handle the event. If you create a custom control, you can and should override the OnKeyPress method.
Creating a custom control is very simple. You just add a new class to your project with an appropriate name, then make it inherit the standard ComboBox class:
vb.net Code:
Public Class ComboBoxEx
Inherits ComboBox
End Class
You can now add members to it like you would any other class, e.g.
vb.net Code:
Public Class ComboBoxEx
Inherits ComboBox
Public Property SomeProperty As SomeType
Public Sub New(someProperty As SomeType)
Me.SomeProperty = someProperty
DropDownStyle = ComboBoxStyle.DropDownList
End Sub
Public Sub SomeMethod()
'...
End Sub
Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
'Code here to execute before event handlers.
MyBase.OnKeyPress(e)
'Code here to execute after event handlers.
End Sub
End Class
Once you build your project, that control will be added to the Toolbox and you can use it like any other control, in the designer and in code.
Last edited by jmcilhinney; Mar 15th, 2023 at 10:32 PM.
-
Mar 14th, 2023, 10:43 PM
#3
Thread Starter
Lively Member
Re: how to create a customized combo box
Thanks, jmcilhinney. I'm curious as to why the OnKeyPress method you show only takes a single argument (e As KeyPressEventArgs), whereas the standard KeyPress methods take two arguments (sender As Object, e As KeyPressEventArgs).
-
Mar 14th, 2023, 11:29 PM
#4
Re: how to create a customized combo box
 Originally Posted by silverblatt
Thanks, jmcilhinney. I'm curious as to why the OnKeyPress method you show only takes a single argument (e As KeyPressEventArgs), whereas the standard KeyPress methods take two arguments (sender As Object, e As KeyPressEventArgs).
OnKeyPress is situated within the encapsulating class of each instance of your extended ComboBox. The sender cannot be anything but that ComboBox itself, whereas in a form you can handle 3 comboboxes selectedindex_changed event in the same handler, and the sender object tells you which ComboBox selectedindex has changed…
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 14th, 2023, 11:37 PM
#5
Thread Starter
Lively Member
Re: how to create a customized combo box
-
Mar 14th, 2023, 11:54 PM
#6
Re: how to create a customized combo box
 Originally Posted by silverblatt
Thanks, jmcilhinney. I'm curious as to why the OnKeyPress method you show only takes a single argument (e As KeyPressEventArgs), whereas the standard KeyPress methods take two arguments (sender As Object, e As KeyPressEventArgs).
There is no standard KeyPress method. You seem to have ignored what I said earlier. KeyPress is an event, while OnKeyPress is a method. The OnKeyPress method exists specifically to raise the KeyPress event. That's why you call the base method: that's where the event is raised. That's why my comments indicate that code before that call is executed before all event handlers and code after that call is executed after event handlers. When you have a ComboBox on a form and you handle the KeyPress event, you add a method to the form - the event handler is a member of the form, NOT the control - named something like ComboBox1_KeyPress. That event handler has two parameters because the first one is the object that raised the event, because the same method can handle multiple events of the same and/or different objects. The OnKeyPress method doesn't need to know what object raised the event because it is part of that object.
To see a bit more about how events work, you might like to read this blog post of mine.
-
Mar 15th, 2023, 10:24 PM
#7
Thread Starter
Lively Member
Re: [RESOLVED] how to create a customized combo box
Thanks for the blog post reference - it was quite interesting, buy WAY beyond the scope of what I'm doing here. It turns out that it was actually the KeyDown event I needed to handle, not the KeyPress event. So, after making the corresponding adjustments to your code (see below) it's working as intended and the new control is now present in the toolbox. And, despite my careless use of the terminology, I am clear that [event_name] is an event while On[event_name] is the method that handles that event. Thanks again for your detailed explanations.
Code:
Public Class cboEx
Inherits ComboBox
'property declarations here
'method declarations here
Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
Select Case e.KeyCode
Case Keys.Delete
'code to handle Delete key here
Case Keys.Insert
'code to handle Insert key here
Case Else
'ignore other keystrokes
End Select
MyBase.OnKeyDown(e)
End Sub
End Class
-
Mar 15th, 2023, 10:44 PM
#8
Re: [RESOLVED] how to create a customized combo box
 Originally Posted by silverblatt
…I am clear that [event_name] is an event while On[event_name] is the method that handles that event.
The sequence of events is…
The user interacts with the control
The control sends a Windows Message to the app. Message queue
* The onkeydown is handled by the control, which you’re handling
* You can run code before or after mybase.onkeydown raises the event in the form.
The mybase.onkeydown raises the event in the form
The steps with the asterisk are only possible with an extended control, such as yours
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|