Microsoft KB Archive/204054

= PRB: Error 35603 "Invalid Key" with TreeView or ListView Control =

Article ID: 204054

Article Last Modified on 7/16/2004

-

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
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q204054



SYMPTOMS
If you add a ListItem to a ListView control or a node to a TreeView control when the value being assigned to the key is numeric or is a string that evaluates to a number (such as "123"), the following error message is raised:

Run-time error '35603':

Invalid key



CAUSE
Keys are restricted to non-numeric values for compatibility with the Visual Basic for Applications (VBA) Collection Object. Although collections allow numeric keys, an error could occur if an item in a collection was accessed using exclamation (!) syntax with a numeric key.



RESOLUTION
Append a non-numeric character to numeric keys.



STATUS
This behavior is by design.



Steps to Reproduce the Behavior
 Create a new Standard EXE project in Visual Basic. Form1 is created by default. On the Project menu, click to select Components, check Microsoft Windows Common Controls, and then click OK. Add a ListView control, a TreeView control, and a CommandButton control to Form1.  Add the following code to the Declarations section of Form1: Option Explicit

Private Sub Command1_Click ' Uncomment either of the following lines to raise the error ' ListView1.ListItems(1).Key = "123" ' TreeView1.Nodes(1).Key = "123" End Sub

Private Sub Form_Load ListView1.ListItems.Add, , "Test1" TreeView1.Nodes.Add, , , "Node1"

' Uncomment either of the following lines to raise the error ' ListView1.ListItems.Add, "123", "Test2" ' TreeView1.Nodes.Add, , "123", "Node2" End Sub  Uncomment any of the commented lines of code in the Form_Load event and run the project. Note that the error message in the "Symptoms" section appears immediately. Clicking the Debug button selects the uncommented line.</li> Stop the project and comment the line again.</li> Uncomment either line of code in the Command1_Click event, run the project, and click on Command1. The error message in the "Symptoms" section appears. Clicking the Debug button selects the uncommented line.</li></ol>

Additional Notes

 * You can use the IsNumeric function to test values before you use them as keys.
 * The use of white space (such as spaces) as the only non-numeric characters or appending only a single Alpha character may fail, depending on the language selected in Regional Settings.

Additional query words: comctl32 mscomctl 35603

Keywords: kbnofix kblistview kbtreeview kbcmnctrls kbprb KB204054

-

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

© Microsoft Corporation. All rights reserved.