none
'Unhandled exception: Cast from string "click" to type 'integer' is not valid - Combobox RRS feed

  • Question

  • Thanks for helping me out:

    Combobox collection from -3 to +3

    The goal of the message is to warn the user that the systems only covers +- 3 timezones, if he types in for example +4 or -4, he gets the message (by the way: how to protect the system if he types for instance a character?)

    When I run the application: 'Unhandled exception: Cast from string "click" to type 'integer' is not valid

     

    Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged

     

    'if more more than 3 timezones

     

    Dim utc As Integer

    utc =

    ComboBox1.Text

     

    If utc < -3 > 3 Then

    MsgBox("Calculations only for maximum 3 Time Zones", MsgBoxStyle.OKOnly, "Info")

    ComboBox1.Text = 0

     

    End If

     

    End Sub

    Wednesday, April 28, 2010 2:25 PM

Answers

  • Can you use next time use this button  at the dropdown VB.Net to paste in your code

    It seems to me no need to set the textbox to "0", it is better to keep it the same so the op can change it, but how is it possible to get that value in the combobox text.

    Is it not easier to take the selecteditem?

    But anyway here the answer on your question.

    Your code looks a little bit on C# which has not as much keywords like VB.

            If IsNumeric(ComboBox1.Text) Then
                If Math.Abs(CInt(ComboBox1.Text)) < 3 Then
                    MsgBox("Calculations only for maximum 3 Time Zones", MsgBoxStyle.OkOnly, "Info")
                End If
            End If


    Success
    Cor
    • Marked as answer by Bremberg Monday, May 3, 2010 6:14 PM
    Friday, April 30, 2010 3:55 PM

All replies

  • use Integer.TryParse

    Integer.TryParse(ComboBox1.Text,utc)

    If utc < 3 AndAlso utc >-3 Then

    'Code Here

    End If


    Asgar
    • Edited by _asgar Wednesday, April 28, 2010 2:46 PM
    • Marked as answer by Bremberg Friday, April 30, 2010 12:54 PM
    • Unmarked as answer by Bremberg Friday, April 30, 2010 12:56 PM
    • Marked as answer by Bremberg Friday, April 30, 2010 1:07 PM
    • Unmarked as answer by Bremberg Friday, April 30, 2010 1:34 PM
    Wednesday, April 28, 2010 2:42 PM
  • 'Always set Option Strict On
    Dim utc As Integer
      If Integer.TryParse(ComboBox2.Text, utc) Then
    
    
       If utc < -3 Or utc > 3 Then 'Depending on what you're testing, you can use And or AndAlso instead of Or.
    'Replace Or with any of the two to see which result satisfies your need
    
        MsgBox("Calculations only for maximum 3 Time Zones", MsgBoxStyle.OkOnly, "Info")
    
        ComboBox2.Text = CStr(0)
    
       End If
      End If
    

    Only performance counts!
    • Marked as answer by Bremberg Friday, April 30, 2010 1:07 PM
    • Unmarked as answer by Bremberg Friday, April 30, 2010 1:35 PM
    Wednesday, April 28, 2010 3:40 PM
  • You might consider replacing the word Integer with Int32, like this:

    If Int32.TryParse(ComboBox1.Text, utc) then

    Friday, April 30, 2010 3:48 PM
  • Can you use next time use this button  at the dropdown VB.Net to paste in your code

    It seems to me no need to set the textbox to "0", it is better to keep it the same so the op can change it, but how is it possible to get that value in the combobox text.

    Is it not easier to take the selecteditem?

    But anyway here the answer on your question.

    Your code looks a little bit on C# which has not as much keywords like VB.

            If IsNumeric(ComboBox1.Text) Then
                If Math.Abs(CInt(ComboBox1.Text)) < 3 Then
                    MsgBox("Calculations only for maximum 3 Time Zones", MsgBoxStyle.OkOnly, "Info")
                End If
            End If


    Success
    Cor
    • Marked as answer by Bremberg Monday, May 3, 2010 6:14 PM
    Friday, April 30, 2010 3:55 PM
  • Sorry same result... 'TryParse' is not a member of 'Integer'

    Thanks anyway

    Friday, April 30, 2010 4:01 PM
  • No, replace Integer with Int32, like my post said. Here...

     

     Private Sub ComboBox1_TextChanged(ByVal sender As Object, _
              ByVal e As System.EventArgs) _
              Handles ComboBox1.TextChanged
    
      'if more more than 3 timezones
    
      Dim utc As Integer
    
      If Int32.TryParse(ComboBox1.Text, utc) Then
       If utc < -3 Or utc > 3 Then
        MsgBox("Calculations only for maximum 3 Time Zones", _
          MsgBoxStyle.OkOnly, _
          "Info")
        ComboBox1.Text = ""
       End If
      End If
    
     End Sub
    
    • Edited by jinzai Friday, April 30, 2010 4:18 PM add code
    Friday, April 30, 2010 4:12 PM
  • What version of Visual basic are you using?

    To turn option Strict On: Just type "Option Strict On" on top of your Form (above the Public Class Form1). I've ran the code I gave to you once again and it worked when I entered 6.

    I've edited the code to generate messages depending on the number entered.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
      Dim utc As Integer
      If Integer.TryParse(ComboBox2.Text, utc) Then
    
    
       If utc < -3 Or utc > 3 Then 'Depending on what you're testing, you can use And or AndAlso instead of Or.
        'Replace Or with any of the two to see which result satisfies your need
    
        MsgBox("Calculations only for maximum 3 Time Zones", MsgBoxStyle.OkOnly, "Info")
    
        ComboBox2.Text = CStr(0)
       Else
        MsgBox("Number outside the range")
    
       End If
    
      Else
       MsgBox("Please enter an integer")
    ComboBox2.Select()
      End If
    
    
    
    
     End Sub

    Only performance counts!
    Friday, April 30, 2010 4:28 PM
  • Sylva...

    ...probably not 2010, like you and _asgar seem to be using.....the error message tells me he is using < 2010, like VB2008, which is why I said to use Int32. There is no Integer structure to have a TryParse method in VB2008, which is what the issue is...your answer, and _asgar's...and of course, Cor's looks fabulous. In VB2008, Integer is a data type, and Int32 is a structure.

     

    Friday, April 30, 2010 4:54 PM
  • Jinzai,

    I'm actually using VB .Net 2008 and I used/use Integer.TryParse and it works. Maybe he is using VB6.


    Only performance counts!
    Friday, April 30, 2010 5:08 PM
  • Hi guys

    I am really impressed. It is wonderful to see that real pro's are helping out a starter with silly questions.

    I am still using VB 2003. Yes I know VB 2010 is there... Maybe time for a change...

    I'll will try your ideas tomorrow

    Thanks and have a great day

     

    Sunday, May 2, 2010 6:02 PM