none
How to make calculator that allows user to use only 1 decimal point and not more RRS feed

  • Question

  • Im a beginner at VB and I need to know how to make it so that the user cant press decimal more than once as a school project, this is my code. 

    Private Sub btnNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    	Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click,
    	btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDot.Click
    
    		Dim btnNumberClicked As Button = sender 'Contains properties of clicked button
    
    		If blnNewOp Then
    			Me.txtDisplay.Text = btnNumberClicked.Tag
    			blnNewOp = False
    		Else
    			Me.txtDisplay.Text = Me.txtDisplay.Text & btnNumberClicked.Tag
    		End If
    
    	End Sub

     
    Saturday, May 5, 2018 11:05 PM

Answers

  • Use Decimal.TryParse or Double.TryParse on a string before making it Me.txtDisplay.Text. And if the string can't be parsed then that would be due to the string either being too long for Decimal or too long for Double or because it has more than one period in it probably. Or possibly use IsNumeric on a string and if it's not don't use it.

    La vida loca

    • Marked as answer by NotLevi Sunday, May 6, 2018 12:45 AM
    Saturday, May 5, 2018 11:18 PM

All replies

  • Use Decimal.TryParse or Double.TryParse on a string before making it Me.txtDisplay.Text. And if the string can't be parsed then that would be due to the string either being too long for Decimal or too long for Double or because it has more than one period in it probably. Or possibly use IsNumeric on a string and if it's not don't use it.

    La vida loca

    • Marked as answer by NotLevi Sunday, May 6, 2018 12:45 AM
    Saturday, May 5, 2018 11:18 PM
  • Sorry but could you provide an example using my code? I'm new and I don't understand this stuff ;(
    Saturday, May 5, 2018 11:51 PM
  • Sorry but could you provide an example using my code? I'm new and I don't understand this stuff ;(

    There's examples of how to use all three at the links provided. For a school assignment it is up to you to determine the logic to use not others so you can get a grade by plagiarizing code.

    La vida loca

    Saturday, May 5, 2018 11:55 PM
  • Sorry but could you provide an example using my code? I'm new and I don't understand this stuff ;(

    Hi

    It is impossible to show an example using your code as your code is like a spaghettified jumble of keywords and errors.

    However, here is some code that limits the user entry into a TextBox - only the 'Allowed' characters are accepted (see remarks in the code) and only one decimal point is allowed, as well as allowing a + character only at beginning and BackSpace is allowed.

    Also, put the 2 Options at the very top of your code which will help you with some errors.

      Option Strict On
      Option Explicit On
    '=================================
    
    Private Sub TextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    
        ' allows only "0123456789.+-" and BackSpace
        ' allows "-+" only as first char.
        ' allows "." only once.
    
        Dim t As TextBox = DirectCast(sender, TextBox)
    
        Dim tb() As Char = t.Text.ToCharArray
    
        Dim allowed As String = "0123456789.+-" & Chr(8)
    
        If allowed.IndexOf(e.KeyChar) = -1 Or (e.KeyChar = "." And tb.Count(Function(c) c = ".") > 0) Or (e.KeyChar = "-" And tb.Count > 0) Or (e.KeyChar = "+" And tb.Count > 0) Then
          e.Handled = True
        End If
      End Sub


    Regards Les, Livingston, Scotland

    Sunday, May 6, 2018 12:08 AM
  • Sorry but could you provide an example using my code? I'm new and I don't understand this stuff ;(

    You need to determine which button was clicked so that you check whether there is already a . in the input string.   One way to do that is to put the operator value into the Tag property of the button, as a string.  That way you can test the property to see which button it is.

    Private Sub btnNumber_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btnDot.Click Dim btnNumberClicked As Button = CType(sender, Button) 'Contains properties of clicked button If btnNumberClicked.Tag = "Dot" then If Instr(txtDisplay.Text,".") <> -1 then Exit Sub 'Ignore this keystroke
    End If If blnNewOp Then Me.txtDisplay.Text = btnNumberClicked.Tag blnNewOp = False Else Me.txtDisplay.Text = Me.txtDisplay.Text & btnNumberClicked.Tag End If End Sub


    Sunday, May 6, 2018 12:35 AM