none
I get this error message on my program Conversion from string "" to type 'Double' is not valid. RRS feed

  • Question

  •  I get this error message on my program Conversion from string "" to type 'Double' is not valid. what is i missing can someone help me 

    Private Sub txtPrice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPrice.TextChanged

            Dim a As Double = txtPrice.Text
            Dim i As Integer
            i = 2999
            If txtPrice.Text > i Then
                MessageBox.Show("Pls insert the Special Code when price max 3k", "Information")
                Form3.Show()
            End If
        End Sub

    Tuesday, November 19, 2019 5:18 AM

Answers

  • @christing,

    Sorry, I missed "." (dot).
    Code would be like this:
    Private Sub txtPrice_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPrice.KeyPress
      ' --- set True as default
      e.Handled = True
      ' --- describe valid characters, and other character is not allowed(e.Handled = True)
      If (e.KeyChar >= "0"c And e.KeyChar <= "9"c) _
          OrElse (e.KeyChar = "."c) _
          OrElse (e.KeyChar = ControlChars.Back) Then
        e.Handled = False
      End If
    End Sub
    Regards,

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by christing Thursday, November 21, 2019 9:24 AM
    Thursday, November 21, 2019 3:18 AM
  • @christing,

    According to your first post, if you want to check the value of [txtPrice], txtPrice.Validated event would be helpful.
    (instead of txtPrice.TextChanged event)
    Private Sub txtPrice_Validated(sender As Object, e As EventArgs) Handles txtPrice.Validated
    	If (Me.txtPrice.Text.Length = 0) Then
    		MsgBox("input valid price: blank is not valid")
    		Me.txtPrice.Select()
    		Exit Sub
    	End If
    	If (CSng(Me.txtPrice.Text) < 3000) Then
    		MsgBox("input valid price: greater/equal than 3000")
    		Me.txtPrice.Select()
    	End If
    End Sub
    Regards,

    P.S.
      Please remember to mark the replies as answers if they help.

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by christing Thursday, November 21, 2019 9:24 AM
    Thursday, November 21, 2019 5:33 AM
  • @Ashidacchi

    Thanks for your helping. :):):)

     
    • Marked as answer by christing Thursday, November 21, 2019 6:23 AM
    Thursday, November 21, 2019 6:07 AM

All replies

  • Hi christing,

    I don't know what value is in txtPrice, but this will solve the issue.
    (convert i to string)

    Dim a As Double = txtPrice.Text
    Dim i As Integer
    i = 2999
    If txtPrice.Text > i.ToString Then
       MessageBox.Show("Pls insert the Special Code when price max 3k", "Information")
       Form3.Show()
    End If

    Regards,


    Ashidacchi -- http://hokusosha.com

    Tuesday, November 19, 2019 5:47 AM
  • Hi christing,

    Sorry, I misunderstand the issue.

    If the value of txtPrice is always numeric, we can convert it to double.
    Dim a As Double = CDbl(txtPrice.Text)
    I recommend to add the below code to forbid inputting key except 0-9 and BackSpace into txtPrice.
    Private Sub txtPrice_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPrice.KeyPress
      If (e.KeyChar < "0"c OrElse "9"c < e.KeyChar) _
          AndAlso (e.KeyChar <> ControlChars.Back) Then
          e.Handled = True
      End If
    End Sub

    Regards,

    Ashidacchi -- http://hokusosha.com


    Tuesday, November 19, 2019 5:53 AM
  • @Ashidacchi

    Thanks you reply my message. May i ask if i want my txtPrice allow dot how can good write the code below is the code i writing for my key press. but i will get error on this message (Conversion from string "." to type 'Double' is not valid.) when i press dot as 1st text i will get this message.

    Private Sub txtPrice_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtPrice.KeyPress

    If (Not e.KeyChar = ChrW(Keys.Back) And ("0123456789.").IndexOf(e.KeyChar) = -1) Or (e.KeyChar = "." And txtPrice.Text.ToCharArray().Count(Function(c) c = ".") > 0) Then

    e.Handled = True
            End If


    • Edited by christing Thursday, November 21, 2019 2:11 AM
    Thursday, November 21, 2019 1:43 AM
  • @Ashidacchi

    Thanks you reply my message. 

    Private Sub txtPrice_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles txtPrice.KeyPress

    If (Not e.KeyChar = ChrW(Keys.Back) And ("0123456789.").IndexOf(e.KeyChar) = -1) Or (e.KeyChar = "." And txtPrice.Text.ToCharArray().Count(Function(c) c = ".") > 0) Then

    e.Handled = True
            End If

    hi may i ask if i want allow my txtPrice allow 123456789. how can i write the code

    If (Not e.KeyChar = ChrW(Keys.Back) And ("0123456789.").IndexOf(e.KeyChar) = -1) Or (e.KeyChar = "." And 

     Dim a As String = CDbl(txtPrice.Text) will get an error info when i fill out .on my txtPrice (Conversion from string "." to type 'Double' is not valid.)

    Hi christing,

    hi may i ask if i want allow my txtPrice allow 123456789. how can i write the code

      You can simply modify:

    Private Sub txtPrice_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPrice.KeyPress
      If (e.KeyChar < "1"c OrElse "9"c < e.KeyChar) _
          AndAlso (e.KeyChar <> ControlChars.Back) Then
          e.Handled = True
      End If
    End Sub


     Dim a As String = CDbl(txtPrice.Text) will get an error info when i fill out .on my txtPrice (Conversion from string "." to type 'Double' is not valid.)

    Which data-type do you want for the variable "a", String or Double?
    If it is String, code should be this:

    Dim a As String = txtPrice.Text 
    If it is Double, use this:
    Dim a As Double = CDbl(txtPrice.Text)

    Regards,


    Ashidacchi -- http://hokusosha.com

    Thursday, November 21, 2019 2:00 AM
  • @Ashidacchi

    May i ask how can allow my txtPrice allow (123456789.(dot)) 


    • Edited by christing Thursday, November 21, 2019 2:44 AM
    Thursday, November 21, 2019 2:43 AM
  • @christing,

    Sorry, I missed "." (dot).
    Code would be like this:
    Private Sub txtPrice_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPrice.KeyPress
      ' --- set True as default
      e.Handled = True
      ' --- describe valid characters, and other character is not allowed(e.Handled = True)
      If (e.KeyChar >= "0"c And e.KeyChar <= "9"c) _
          OrElse (e.KeyChar = "."c) _
          OrElse (e.KeyChar = ControlChars.Back) Then
        e.Handled = False
      End If
    End Sub
    Regards,

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by christing Thursday, November 21, 2019 9:24 AM
    Thursday, November 21, 2019 3:18 AM
  • @christing,

    According to your first post, if you want to check the value of [txtPrice], txtPrice.Validated event would be helpful.
    (instead of txtPrice.TextChanged event)
    Private Sub txtPrice_Validated(sender As Object, e As EventArgs) Handles txtPrice.Validated
    	If (Me.txtPrice.Text.Length = 0) Then
    		MsgBox("input valid price: blank is not valid")
    		Me.txtPrice.Select()
    		Exit Sub
    	End If
    	If (CSng(Me.txtPrice.Text) < 3000) Then
    		MsgBox("input valid price: greater/equal than 3000")
    		Me.txtPrice.Select()
    	End If
    End Sub
    Regards,

    P.S.
      Please remember to mark the replies as answers if they help.

    Ashidacchi -- http://hokusosha.com

    • Marked as answer by christing Thursday, November 21, 2019 9:24 AM
    Thursday, November 21, 2019 5:33 AM
  • @Ashidacchi

    Thanks for your helping. :):):)

     
    • Marked as answer by christing Thursday, November 21, 2019 6:23 AM
    Thursday, November 21, 2019 6:07 AM
  • @christing,

    Thank you for your response, but I'm afraid you should mark posts which help you.
    (Usually, it is not your own post)

    Regards,


    Ashidacchi -- http://hokusosha.com

    • Marked as answer by christing Thursday, November 21, 2019 9:24 AM
    • Unmarked as answer by christing Thursday, November 21, 2019 9:24 AM
    Thursday, November 21, 2019 6:33 AM
  • @Ashidacchi

    may i ask what is the meaning of this is not my own post i not get what you mean. i am glad you post help me a lot  :):)

    Thursday, November 21, 2019 7:59 AM
  • @christing,

    If my post could help you, you should mark my post as an answer, not yours.
    (This is what I meant in my previous post. Sorry, I'm poor in English.)

    Regards,

    Ashidacchi -- http://hokusosha.com

    Thursday, November 21, 2019 8:11 AM
  • i marks already
    Thursday, November 21, 2019 9:24 AM
  • thanks!

    Ashidacchi -- http://hokusosha.com

    Thursday, November 21, 2019 10:18 AM