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

  • Domanda

  •  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

    martedì 19 novembre 2019 05:18

Risposte

  • @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

    • Contrassegnato come risposta christing giovedì 21 novembre 2019 09:24
    giovedì 21 novembre 2019 03:18
  • @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

    • Contrassegnato come risposta christing giovedì 21 novembre 2019 09:24
    giovedì 21 novembre 2019 05:33
  • @Ashidacchi

    Thanks for your helping. :):):)

     
    • Contrassegnato come risposta christing giovedì 21 novembre 2019 06:23
    giovedì 21 novembre 2019 06:07

Tutte le risposte

  • 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

    martedì 19 novembre 2019 05:47
  • 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


    martedì 19 novembre 2019 05:53
  • @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


    • Modificato christing giovedì 21 novembre 2019 02:11
    giovedì 21 novembre 2019 01:43
  • @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

    giovedì 21 novembre 2019 02:00
  • @Ashidacchi

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


    • Modificato christing giovedì 21 novembre 2019 02:44
    giovedì 21 novembre 2019 02:43
  • @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

    • Contrassegnato come risposta christing giovedì 21 novembre 2019 09:24
    giovedì 21 novembre 2019 03:18
  • @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

    • Contrassegnato come risposta christing giovedì 21 novembre 2019 09:24
    giovedì 21 novembre 2019 05:33
  • @Ashidacchi

    Thanks for your helping. :):):)

     
    • Contrassegnato come risposta christing giovedì 21 novembre 2019 06:23
    giovedì 21 novembre 2019 06:07
  • @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

    • Contrassegnato come risposta christing giovedì 21 novembre 2019 09:24
    • Contrassegno come risposta annullato christing giovedì 21 novembre 2019 09:24
    giovedì 21 novembre 2019 06:33
  • @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  :):)

    giovedì 21 novembre 2019 07:59
  • @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

    giovedì 21 novembre 2019 08:11
  • i marks already
    giovedì 21 novembre 2019 09:24
  • thanks!

    Ashidacchi -- http://hokusosha.com

    giovedì 21 novembre 2019 10:18