Microsoft KB Archive/836676

= BUG: The CheckedListBox control does not fire the KeyPress event when the key value is a space character =

Article ID: 836676

Article Last Modified on 11/27/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Academic Edition

-



SUMMARY
The KeyPress event on the CheckedListBox control does not fire when the key value is a space character.



SYMPTOMS
When you run the application with a CheckedListBox control on the form, the CheckedListBox control does not fire the KeyPress event if the key value is a space character (the SPACEBAR on the keyboard).



CAUSE
The SPACEBAR is captured as an item that is being checked or unchecked in the CheckedListBox control. Therefore, the ItemCheck event of the CheckedListBox control fires.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section of this article.



Create a new Microsoft Visual Basic Windows application

 * 1) Start Microsoft Visual Studio .NET or Microsoft Visual Studio 2005.
 * 2) On the File menu, point to New, and then click Project. The New Project dialog box appears.
 * 3) Under Project Types, click Visual Basic Projects, and under Templates, click Windows Application, and then click OK. By default, a form that is named Form1.vb is added to the project.

Note In Visual Studio 2005, click Visual Basic.
 * 1) Add a CheckedListBox control to the Form1 form.

Add items to the CheckedListBox control
 Double-click the Form1 form to obtain the Form1_Load event.  Add the following code in the Form1_Load event: Dim ItemObject(9) As System.Object Dim i As Integer For i = 0 To 9 ItemObject(i) = &quot;Item&quot; & i       Next i        CheckedListBox1.Items.AddRange(ItemObject) 

Add a handler for the KeyPress event
 In the code view of Form1.vb, click CheckedListBox1 in the first box.  Click the KeyPress event from the second box that is provided in the Code view of Form1.vb. Notice that in the Code view of Form1.vb, the KeyPress event appears as follows: Private Sub CheckedListBox1_KeyPress(ByVal sender As Object,   ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CheckedListBox1.KeyPress

End Sub   Add the following code to the KeyPress event of the CheckedListBox1 control: MsgBox(&quot;KeyPress event fired&quot;) </li></ol>

Build and then run the application

 * 1) On the Build menu, click Build Solution.
 * 2) On the Debug menu, click Start.

Press the SPACEBAR on the keyboard while the CheckedListBox control is selected
After you run the application, the CheckedListBox control appears with the items that are populated in the CheckedListBox control. Follow these steps:
 * 1) Press the SPACEBAR on the keyboard while the CheckedListBox control is selected on the form. Notice that the KeyPress event is not fired.
 * 2) Press any other character keys from your keyboard. Notice that the KeyPress event is fired.

<div class="references_section">