Need Help Making a User Control Based on a ComboBox

Retired96

New member
Joined
Aug 19, 2019
Messages
2
Programming Experience
10+
I wanted to learn about user controls so I decided to try to duplicate the old VB5 DriveListBox using a Combo box. The problem I am having is that all the items in the listbox are entered twice (The first 5 items are repeated a 2nd time. Here are the steps I used with VB2005 (but it also happens with VB2017). I have reduced the code down to a simple example.

I start a new project and name it "text case".
I add a new class to the project called DriveListBox. Here is the code for the new class

VB.NET:
Public Class DriveListBox
    Inherits ComboBox
   
    Public Sub New()
        Items.Clear()
        Sorted = False
        DropDownStyle = ComboBoxStyle.DropDownList
       
        For i As Integer = 1 To 5
            Items.Add("Item " & i.ToString)
        Next
       
        SelectedIndex = 1
        SelectedText = Items(1)
    End Sub

End Class

Next I build "test case" and drag control DriveListBox1 from the ToolBox onto Form1 of the project.
When I run the program in debug mode, DriveListBox contains 10 entries, Item 1 thru Item 5 and then Item 1 thru Item 5 again. I know the 2nd set of 5 entries are coming from the code in Form1Designer.vb.

Can anyone explain to me why this is happening and how to fix it? I would be very grateful.
 
Last edited by a moderator:
The first order of business is to clear up some terminology. What you're talking about is a "custom control". A "user control" is something specific, i.e. a class that inherits the UserControl class. A user control is specifically a way to group other controls together into a reusable unit. It's a shame that so many people get this wrong - including experienced developers - because the first question we have to ask every time someone says that they are creating a user control is whether it actually is a user control or it's really a custom control.
 
The next order of business is those last two lines of code in the constructor. Firstly, by setting the SelectedIndex to 1, you are selecting the second item in the drop-down list. Does that really make sense? Secondly, setting the SelectedText in that context makes absolutely no sense at all. I suspect that that property does not mean what you think it means. SelectedText works the same way in a ComboBox as it does in a TextBox, so it can't possibly be useful if DropDownStyle is DropDownList. I suspect that what you actually want there is SelectedItem but that is still redundant if you have already set SelectedIndex.
 
As for your issue, from what I was able to gather, you can do the following to avoid populating the drop-down list in the designer:
VB.NET:
If LicenseManager.UsageMode = LicenseUsageMode.Runtime Then
    'Code here will not be run in the designer.
End If
I tested that and it worked for me. The control looked like a regular, empty ComboBox when added to a form but was then populated when I ran the project.

Note that there is also a DesignMode property but it is only True when the control itself is open in the designer, not when it is added to a host (form or user control) in the designer.
 
Sorry I was confused about the terminology. I am mainly searching the internet when I need information and as you state there is a lot of misinformation out there. On you 2nd point, the lines make no sense in the simple example. I started out trying to duplicate the old vb5 DriveListBox which set the selected entry to the drive of the current directory and the text portion of the control to the name of that drive.. Your fix is just what I needed and it works. Thank you very much.
 
Back
Top