Results 1 to 8 of 8

Thread: [RESOLVED] how to create a customized combo box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Resolved [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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to create a customized combo box

    Quote Originally Posted by silverblatt View Post
    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.
    Quote Originally Posted by silverblatt View Post
    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.
    Quote Originally Posted by silverblatt View Post
    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.
    Quote Originally Posted by silverblatt View Post
    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:
    1. Public Class ComboBoxEx
    2.     Inherits ComboBox
    3.  
    4. End Class
    You can now add members to it like you would any other class, e.g.
    vb.net Code:
    1. Public Class ComboBoxEx
    2.     Inherits ComboBox
    3.  
    4.     Public Property SomeProperty As SomeType
    5.  
    6.     Public Sub New(someProperty As SomeType)
    7.         Me.SomeProperty = someProperty
    8.  
    9.         DropDownStyle = ComboBoxStyle.DropDownList
    10.     End Sub
    11.  
    12.     Public Sub SomeMethod()
    13.         '...
    14.     End Sub
    15.  
    16.     Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
    17.         'Code here to execute before event handlers.
    18.  
    19.         MyBase.OnKeyPress(e)
    20.  
    21.         'Code here to execute after event handlers.
    22.     End Sub
    23.  
    24. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    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).

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to create a customized combo box

    Quote Originally Posted by silverblatt View Post
    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…

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    Re: how to create a customized combo box

    Got it. Thanks again.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to create a customized combo box

    Quote Originally Posted by silverblatt View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2010
    Posts
    74

    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

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [RESOLVED] how to create a customized combo box

    Quote Originally Posted by silverblatt View Post
    …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

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
  •  



Click Here to Expand Forum to Full Width