converting to integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tragic54
    New Member
    • Feb 2008
    • 20

    converting to integer

    Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was not in the correct format' error. Ive tried declaring the variables as strings and converting the textbox to integer from there and still get the same thing. I'm just trying to get a value to store it in that variable so i can use it for later calculations. Theres a lot more to it, but considering i'm getting this so early i don't wanna continue until i get it right.

    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Form1
    
        Private Sub cboCommute_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCommute.SelectedIndexChanged
    
            Dim intCommuteChoice As Integer
            intCommuteChoice = Me.cboCommute.SelectedIndex
            Select Case intCommuteChoice
                Case 0
                    CarCommute()
    
            End Select
    
        End Sub
    
    
        Private Sub CarCommute()
            Me.GroupBox1.Visible = True
    
            Dim intlabel1 As Integer
            Dim intlabel2 As Integer
    
            intlabel1 = Convert.ToInt32(Me.Label1.Text)
            intlabel2 = Convert.ToInt32(Me.Label2.Text)
    
        End Sub
    
    End Class
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Which line throws the error?

    Comment

    • tragic54
      New Member
      • Feb 2008
      • 20

      #3
      25. I guess ill just declare variables as strings and take the input from the message boxes and convert them to integers when doing the calculations. Just wondering what the syntax is to force a textbox to accept an integer value instead of string.

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi,

        Try This :

        [code=vbnet]
        Private Sub TextBox1_KeyPre ss(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyPressE ventArgs) Handles TextBox1.KeyPre ss
        If InStr("01234567 89", e.KeyChar.ToStr ing) <= 0 Then
        e.Handled = True
        End If
        End Sub
        [/code]

        Regards
        Veena

        Comment

        • QVeen72
          Recognized Expert Top Contributor
          • Oct 2006
          • 1445

          #5
          Hi,

          If you are getting error on 25 and 26th line, you can just use the "Val" function like this..:

          [code=vbnet]
          intlabel1 = Val(Me.Label1.T ext)
          intlabel2 = Val(Me.Label2.T ext)
          [/code]

          Regards
          Veena

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Assuming it's still available in your version (I use VB6) I'd recommend using the Masked Edit control rather than a textbox. It allows much better control over what is input.

            Comment

            • kadghar
              Recognized Expert Top Contributor
              • Apr 2007
              • 1302

              #7
              what i've found easier is using CType(Var, Type) to make all the type changes you need while using Option Strict (yeah, the easy way out).

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Generally speaking though, I think it's better practice to restrict user input to what is acceptable, rather than waiting until they've entered something then complaining about it.

                Comment

                Working...