• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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

Open in new window

0
sn1fflez
Asked:
sn1fflez
1 Solution
 
HainKurtSr. System AnalystCommented:
try

char_Renamed = Asc(Chr(KeyAscii))
0
 
HainKurtSr. System AnalystCommented:
and Asc(...) should return int, not double...
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ZhaolaiCommented:
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

Open in new window

0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
Same Result..
0
 
sn1fflezAuthor Commented:
I will try these solutions out.. thanks for the help so far everyone
0
 
John (Yiannis) ToutountzoglouInstructor Multiengine PilotCommented:
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

Open in new window

0
 
andr_ginCommented:
1.)
The Double.Parse method throws an exception, if the string is not valid.
You can also use the Double.TryParse method to check, if a string can be converted.

2.)
Chr(13) is the carriage return. In VB.NET you can also write vbCr instead

3.) Lets look at your code:

  Dim KeyAscii As Short = Asc(eventArgs.KeyChar)

  You do not get the Char of the key that was pressed. You get the code of the key. Sometimes it is the same (the a key is the ascii of the lower case letter a), but often not. For example there is not key code for upper case a. You only get Shift + lower case a.
You should handle the KeyPress event instead. Then you get the Char that was pressed.


  Dim char_Renamed As Double
  char_Renamed = Double.Parse(Chr(KeyAscii))

  This throws an exception if any key other than a number is pressed.


  If char_Renamed = Double.Parse(Chr(13)) Then
  When handling KeyPress it is also not necessary to make a Double.Parse. You simply write:
  If e.KeyChar = vbCr OrElse e.KeyChar = vbLf Then
         txtNodesNumChange()
  End If

    eventArgs.KeyChar = Chr(KeyAscii)
  If KeyAscii = 0 Then
 
I dont know what you want to do with this line, but there is no Chr(0). 0 means "end of string" and is no valid character. It is not possible to get a KeyPressed or KeyDown event, if no key was pressed at all.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now