CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2018
    Posts
    1

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

    Im making a calculator as a school project and I would like to know how to make it so that only 1 decimal is allowed in the textbox. The current code that need's changing is (And I know its basic but im just learning it):

    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
    Last edited by 2kaud; May 4th, 2018 at 03:44 AM. Reason: Added code tags

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to make calculator that allows user to use only 1 decimal point and not more

    Moved to .Net area as this is not VB6 code.

    One simple method would be to use the .IndexOf method to see if there is a decimal point in your text and then if there is call it again with a new starting point and see if there is another.

    You should be able to find examples for using IndexOf fairly easy

    Another option is when they click the decimal button check and see if there is already a decimal in the string and disallow that entry.

    Or yet another disable the decimal button once they click it once within any given number.

    If they can type into the field though button trapping will not catch that, same if they paste into it so you may need to use more than one method to insure you cover all the bases
    Last edited by DataMiser; May 6th, 2018 at 03:16 PM.
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured