This documentation is archived and is not being maintained.

Assignment not supported

Visual Studio .NET 2003

In Visual Basic 6.0, an integer or variant value could be assigned to the KeyAscii parameter of a KeyPress event in order to override the KeyAscii value passed to the event. The most common usage was to assign a value of 0 to cancel the keystroke.

In Visual Basic .NET, you cannot assign any value other than 0 to KeyAscii. The following example shows the effect of upgrading the KeyPress event:

' Visual Basic 6.0
Private Sub Text1_KeyPress(KeyAscii As Integer)
   Dim MyValue As Variant
   KeyAscii = MyValue
   KeyAscii = 0
   KeyAscii = 1
End Sub

' After upgrade to Visual Basic .NET
Private Sub Text1_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs)
   Dim KeyAscii As Short = Asc(eventArgs.KeyChar)  
   ' UPGRADE_WARNING: Assignment not supported: KeyAscii to a non-zero value.
   KeyAscii = MyValue
   KeyAscii = 0
   ' UPGRADE_WARNING: Assignment not supported: KeyAscii to a non-zero value.
   KeyAscii = 1
   If KeyAscii = 0 Then
      eventArgs.Handled = True
   End If
End Sub

What to do next

  • You cannot assign key values in the KeyPress event. Instead, create your own function to assign the key value outside of the KeyPress event.

See Also

Control.KeyPress Event

Show: