BUG: Setting ComboBox Control Text in Click Event Wipes Out Text

ID: Q168824


The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0


SYMPTOMS

When setting the ComboBox control Text property in the Click event of ComboBox control, the text always returns blank. This is different behavior than Visual Basic 3.0 and Visual Basic 4.0.


RESOLUTION

Instead of setting the Text property of the ComboBox control in the Click event, do it at the LostFocus event by shifting the focus to another control, such as TextBox, although it will leave the focus set to the TextBox. To work around that, hide a TextBox behind the ComboBox with TabStop property set to False; in the GotFocus event of the Textbox, set the focus back to the ComboBox. To place a TextBox behind the ComboBox in design time, choose the Send To Back menu command from the Format/Order menu; in runtime, set the z-order of TextBox to 1.

The following code example shows how to keep the text of ComboBox control after being set at the Click event:

  1. Start a new Standard EXE project. Form1 is created by default.


  2. Add a TextBox control, Text1, to Form1.


  3. Add a ComboBox control, Combo1, to Form1.


  4. Place the following code into the General Declarations section of Form1:
    
          Dim ClickFlag As Integer
    
          Private Sub Combo1_Click()
             ClickFlag = True
             Text1.SetFocus
          End Sub
    
          Private Sub Combo1_LostFocus()
             If ClickFlag = True Then
                Combo1.Text = Mid(Combo1.Text, 4)
                ClickFlag = False
             End If
          End Sub
    
          Private Sub Form_Load()
             ClickFlag = False
             With Combo1
                 .AddItem "1. Adam"
                 .AddItem "2. Bob"
                 .AddItem "3. Charles"
             End With
             Combo1.Text = Mid(Combo1.List(0), 4)
    
             'Hide Text1 under Combo1
             Text1.Move Combo1.Left, Combo1.Top, Combo1.Width, Combo1.Height
             Text1.ZOrder (1)
             Text1.TabStop = False
          End Sub
    
          Private Sub Text1_GotFocus()
             Combo1.SetFocus
          End Sub
     



STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.


MORE INFORMATION

Steps to Reproduce Behavior

  1. Start a new Standard EXE project. Form1 is created by default.


  2. Add a ComboBox control, Combo1, to Form1.


  3. Paste the following code into the General Declarations section of Form1:
    
          Private Sub Combo1_Click()
             Combo1.Text = Mid(Combo1.Text, 1, 3)
          End Sub
    
          Private Sub Form_Load()
            With Combo1
              .AddItem "1. Adam"
              .AddItem "2. Bob"
              .AddItem "3. Charles"
            End With
            Combo1.Text = Mid(Combo1.List(0), 4)
          End Sub
     


  4. Start the program or press the F5 key.


  5. Click one item of the ComboBox control dropdown list and note that the text portion of the ComboBox control is wiped out and becomes blank.


(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Adrian Chiang, Microsoft Corporation

Additional query words: kbVBp500bug kbVBp600bug kbdse kbDSupport kbVBp kbCtrl

Keywords : kbGrpVBDB
Version :
Platform : WINDOWS
Issue type : kbbug


Last Reviewed: January 5, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.