Microsoft KB Archive/168824

= FIX: Setting ComboBox Control Text in Click Event Wipes Out Text =

Article ID: 168824

Article Last Modified on 8/7/2007

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q168824



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, change it at the LostFocus event by shifting the focus to another control, such as TextBox, although it leaves 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 run time, 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:  Start a new Standard EXE project. Form1 is created by default. Add a TextBox control, Text1, to Form1. Add a ComboBox control, Combo1, to Form1.  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 that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in the latest service pack for Visual Studio 6.0.

For additional information about Visual Studio service packs, click the following article numbers to view the articles in the Microsoft Knowledge Base:

194022 INFO: Visual Studio 6.0 Service Packs, What, Where, Why

194295 HOWTO: Tell That a Visual Studio Service Pack Is Installed

To download the latest Visual Studio service pack, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/vstudio/Aa718353.aspx



Steps to Reproduce Behavior
 Start a new Standard EXE project. Form1 is created by default.</li> Add a ComboBox control, Combo1, to Form1.</li>  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

</li> Start the program or press the F5 key.</li> Click one item of the ComboBox control drop-down list and note that the text portion of the ComboBox control is wiped out and becomes blank.</li></ol>

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

Keywords: kbbug kbctrl kbfix kbvs600sp4fix kbvs600sp5fix KB168824

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.