sn1fflez
asked on
What is wrong with this code?
So I'm converting an application from vb6 to vb..net and have run into an error with the following code in vb.net. If I type a number into "textnodesnum" during runtime, I get an error saying that "Double.Parse(Chr(13))" is in the wrong format.... any ideas?
Private Sub txtNodesNum_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtNodesNum.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
Dim char_Renamed As Double
char_Renamed = Double.Parse(Chr(KeyAscii))
If char_Renamed = Double.Parse(Chr(13)) Then
txtNodesNumChange()
End If
eventArgs.KeyChar = Chr(KeyAscii)
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub
and Asc(...) should return int, not double...
try
Private Sub txtNodesNum_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtNodesNum.KeyPress
Dim KeyAscii As Short = CShort(Asc(eventArgs.KeyChar))
Dim char_Renamed As String
char_Renamed = CStr(Chr(KeyAscii))
If char_Renamed = Chr(13) Then
MessageBox.Show("....")
End If
eventArgs.KeyChar = Chr(KeyAscii)
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub
It should be as simple as this:
Private Sub txtNodesNum_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) Handles txtNodesNum.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
If Chr(KeyAscii) = Chr(13) Then
txtNodesNumChange()
End If
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub
Same Result..
ASKER
I will try these solutions out.. thanks for the help so far everyone
as i noticed from your code i think that you want to achieve the enter Key ...you can accomplish this "also" with key_Down Event
Private Sub txtNodesNum_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNodesNum.KeyDown
If e.KeyCode = Keys.Return Then
...........
End If
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
char_Renamed = Asc(Chr(KeyAscii))