With this code:

Imports System.Text

Public Class Form1
    Public quoteStr As String = Global.Microsoft.VisualBasic.ChrW(34)
    Public illegalChar As Boolean = False
    Public asteriskReplacement As New StringBuilder(" ", 4)
    Public asteriskComboIndex As Integer = 0

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       Dim keyboardCharacters As String() = {" single space", "- hyphen", _
            " -  <single space>hyphen<single space>", "` open single quote",_
            "' close single quote", "_ underscore", "=", "~", "!", "@", "#", "$",_
            "%", "^", "&", "(", ")", "+", "[", "{", "}", ";", quoteStr, ",", ".", _
            "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d",_
            "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",_
            "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",_
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",_
            "U", "V", "W", "X", "Y", "Z"}        ComboBox1.Items.AddRange(keyboardCharacters)
        ComboBox1.SelectedIndex = asteriskComboIndex
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)_
      Handles ComboBox1.SelectedIndexChanged
        Dim indexS As String = ComboBox1.SelectedIndex
        MsgBox("SelectedIndex _" + indexS + "_")
        Dim index As Integer = ComboBox1.SelectedIndex
        asteriskComboIndex = index
        WhichChar(ComboBox1, asteriskReplacement, index)
        MsgBox("SelectedIndexChanged SelectedText _" + ComboBox1.SelectedText + "_")
    End Sub

    Public Sub WhichChar(ByVal CBox As ComboBox, ByRef retVal As StringBuilder, ByVal index As Integer)
        MsgBox("WhichChar  CBox.SelectedText _" + CBox.SelectedText + "_")
        Dim selectedItem As New Object
        selectedItem = CBox.SelectedItem
        ClearStrBuilder(retVal)
        MsgBox("timing")
        Select Case index
            Case 0  'single space
                retVal.Append(" ")
            Case 1  'hyphen
                retVal.Append("-")
            Case 2  'single space>hyphen<single space>
                retVal.Append(" - ")
            Case 3  'open single quote
                retVal.Append("`")
            Case 4  ' close single quote
                retVal.Append("'")
            Case 5  'underscore
                retVal.Append("_")
            Case Else   'everything else
                retVal.Append(CBox.SelectedText)
        End Select
    End Sub

    Private Sub ClearStrBuilder(ByRef str As StringBuilder)
        Dim holdStr As String = str.ToString()
        str = str.Remove(0, holdStr.Length)
        str.Length = 0
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)_
      Handles MyBase.FormClosed
        MsgBox("Form1_FormClosed asteriskReplacement _" + asteriskReplacement.ToString() + "_")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Button1_Click asteriskReplacement _" + asteriskReplacement.ToString() + "_")
        MsgBox("Button1_Click  ComboBox1.SelectedText _" + ComboBox1.SelectedText + "_")
    End Sub

    Private Sub ComboBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave
        Dim indexS As String = ComboBox1.SelectedIndex
        MsgBox("ComboBox1 SelectedIndex _" + indexS + "_")
        Dim index As Integer = ComboBox1.SelectedIndex
        asteriskComboIndex = index
        WhichChar(ComboBox1, asteriskReplacement, index)
    End Sub
End Class

Create a default Windows Form Project with a default ComboBox and default Button in the default Form. Double-click the Form to open the code window, replace everything there with the above code. Run the code in the debugger – menu item Debug/Start Debugging . Close the spurious MsgBox.

Left click once in the ComboBox, press the <a> key (any valid file name character will do); left click the button. Note the two MsgBox messages report that no item was selected in the ComboBox (button click) and that nothing was entered into the asteriskReplacement StringBuilder (you may uncomment the “Timing” MsgBox in WhichChar() to verify that it is not entered). Note that in each message string I have wrapped the variable with a leading and trailing underscore so that null string variables look like __ <two underscores together> and a string containing a single space looks like _ _ <underscore><space><underscore>.

The problem is that when I enter a value via the keyboard (even adding a carriage return) the ComboBox does not enter the _SelectedIndexChanged() function. It does enter the _Leave() function (when I click the button), but note that by the time _Leave() is entered the .SelectedIndex value is no longer valid.

I have spent many hours trying to figure this out; it is obviously a very basic concept and everyone else uses this gadget with ease. I have no idea where I am going wrong. Any help would be greatly appreciated!

Recommended Answers

All 4 Replies

A ComboBox, as it's name implies is a combination of a TextBox and a ListControl.
If you type an 'a' into the "TextBox" you will see that the ComboBox.Text property contains an 'a'. To put that 'a' into the dropdownlist you have to do a ComboBox1.Items.Add or .Insert

A ComboBox, as it's name implies is a combination of a TextBox and a ListControl.
If you type an 'a' into the "TextBox" you will see that the ComboBox.Text property contains an 'a'. To put that 'a' into the dropdownlist you have to do a ComboBox1.Items.Add or .Insert

Not quite what I am looking for. The "a" is already on the list of items, I do not want to add it. Think of the ComboBox in a web shopping cart--you are asked to fill in your credit card info...there are two comboBoxes to enter Expiration date--month and year. The month box has "1 (Jan)", "2 (Feb)", "3 (Mar)...etc. as possible items to select (I see this in lots of non-web Windows apps as well.)

What other tool would do this job?

I think you need to check out the DropDownStyle property.
Set it to DropDownList.

The SelectedIndexchanged() event only fires when you actually click an item to "change" the contents of the combobox.... perhaps "click" or "keypress" would be the right choice?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.