Solved

What is wrong with this code?

Posted on 2010-09-07
8
350 Views
Last Modified: 2012-05-10
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
Comment
Question by:sn1fflez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33619739
try

char_Renamed = Asc(Chr(KeyAscii))
0
 
LVL 53

Expert Comment

by:Huseyin KAHRAMAN
ID: 33619750
and Asc(...) should return int, not double...
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33619852
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 17

Expert Comment

by:Zhaolai
ID: 33619858
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
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33619885
Same Result..
0
 

Author Comment

by:sn1fflez
ID: 33620002
I will try these solutions out.. thanks for the help so far everyone
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33620216
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
 
LVL 5

Accepted Solution

by:
andr_gin earned 500 total points
ID: 33625226
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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question