locked
How do I place decimal point in calculator?

    Question

  • I have written a working calculator, that is except for the decimal point. I push the button, & it shows, but when I input another digit behind it, it disappears. Here is the code I'm using...

    Private Sub point_Click(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles point.Click
        Dim a As Double = TextBox1.Text
        Dim b As String = "."
        Dim c As Double = TextBox2.Text
        If acall.Text = 0 Then
          TextBox1.Text = (a & b)
        End If
        If acall.Text = 1 Then
          TextBox2.Text = (c & b)
        End If
      End Sub

    If it matters, here is how I'm doing my numbers...

    Private Sub nine1_Click(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles nine1.Click
        Dim a As Double = TextBox1.Text
        Dim b As Double = 9
        Dim c As Double = TextBox2.Text
        If acall.Text = 0 Then
          If TextBox1.Text = 0 Then
            TextBox1.Text = 9
          Else
            TextBox1.Text = (a & b)
          End If
        Else
          If TextBox2.Text = 0 Then
            TextBox2.Text = 9
          Else
            TextBox2.Text = (c & b)
          End If
        End If
      End Sub
    I'm using a 3 textbox system on this calculator, do to the many unusual functions in it. It is a position, & shape calculator for user controlled graphics. On my other (regular, simple) calculator, the decimal point does not work either!
    Saturday, June 19, 2010 5:49 AM

Answers

  • then take the time to do it

    and then click on Tools in VisualStudio >> Options >> Projects and Solutions >> VB Defaults. Turn Explicit, Strict and Infer to ON

    and it's not changing to Integer - read the warning again.

    what datatype is the Text property of a TextBox? That is what you need to change it to

     

    see this link - what datatype do all the examples show?

    http://msdn.microsoft.com/en-us/library/a19tt6sk(VS.71).aspx

    that is what you need to change the datatype of your variable declarations to

    • Marked as answer by docfnt Saturday, June 19, 2010 8:10 AM
    Saturday, June 19, 2010 6:39 AM
  • you can use a String for a number, you just have to enclose it in quotes, e.g. Dim myStr As String = "5"

    and any Text property of a Control, such as TextBox or Label, etc also has to be in quotes, e.g. TextBox1.Text = "150"

    since you have shown the willingness to learn and do what is needed, I'll give you this to try. Try it in an app with 3 buttons and 3 textboxes (rename TextBox3 to acall). Type a 1 or 0 in the acall textbox, then click the buttons. You'll see it only allows one decimal point, and adds however many digits after the decimal

    study this code so you can see how it adds ONE decimal point, and concatenates the digits to the end of the TextBox Text. Also notice there is no need for all those variables

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Select Case acall.Text
          Case Is = "0"
            If Not TextBox1.Text.Contains(".") Then
              TextBox1.Text &= "."
            End If
          Case Else
            If Not TextBox2.Text.Contains(".") Then
              TextBox2.Text &= "."
            End If
        End Select
    
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Select Case acall.Text
          Case Is = "0"
            TextBox1.Text &= "1"
          Case Else
            TextBox2.Text &= "1"
        End Select
      End Sub
    
      Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Select Case acall.Text
          Case Is = "0"
            TextBox1.Text &= "2"
          Case Else
            TextBox2.Text &= "2"
        End Select
      End Sub

     

    • Marked as answer by docfnt Saturday, June 19, 2010 8:10 AM
    Saturday, June 19, 2010 7:16 AM

All replies

  • PS, I tried John Anothony Oliver's code for the decimal point, but for some reason, it did not work with mine.

    I did read, as well as copy & paste the code for the decimal from here...

    http://social.msdn.microsoft.com/Forums/en-US/vbide/thread/7cc30ce1-b7ec-43ef-9c0e-e6fd067f542a

    But it didn't like my code, i guess...no exceptions, it just did the same thing. no decimal after the next number.

    Saturday, June 19, 2010 5:55 AM
  • at the very top of your code type

    Option Strict On

    that will help you straighten out what you need to do (hint: TextBox Text property is NOT a double)

    by the way, how many decimal points are allowed?

     

    Saturday, June 19, 2010 6:19 AM
  • If you mean how many digits behind it, till the textbox fills, or beyond. Neither calc are rigged for scientific notation.

    If you mean how many decimal points, I never really considered that one....Ooops!

     

    I'm trying it right now...

    • Edited by docfnt Saturday, June 19, 2010 6:29 AM
    Saturday, June 19, 2010 6:25 AM
  • did you type Option Strict On at the very top of your code?

    and yes, I meant how many decimal points (not decimal places)

    because with how you are doing it you could add any number to the textbox

    Saturday, June 19, 2010 6:28 AM
  • Photobucket

     

    Strick Option doesn't much care for my code either. I'll be a while if I have to change every "Double" to "Integer", or whatever it wants...

     

    There's over 800 lines of code in this calculator...so far. That's a lot of doubles to change! I DREAD the thought that I may have to rewrite the whole critter to use the decimal point. Are they all their cracked up to be?  LOL...  :-)

    • Edited by docfnt Saturday, June 19, 2010 6:41 AM
    Saturday, June 19, 2010 6:35 AM
  • then take the time to do it

    and then click on Tools in VisualStudio >> Options >> Projects and Solutions >> VB Defaults. Turn Explicit, Strict and Infer to ON

    and it's not changing to Integer - read the warning again.

    what datatype is the Text property of a TextBox? That is what you need to change it to

     

    see this link - what datatype do all the examples show?

    http://msdn.microsoft.com/en-us/library/a19tt6sk(VS.71).aspx

    that is what you need to change the datatype of your variable declarations to

    • Marked as answer by docfnt Saturday, June 19, 2010 8:10 AM
    Saturday, June 19, 2010 6:39 AM
  • WOW! Little by little, the "blues" are going away. I thought that "string" was one of the types you could not use to declare numbers. I have never used string for that reason. I still have about 750 line to go, but I'm working on it...
    Saturday, June 19, 2010 6:59 AM
  • you can use a String for a number, you just have to enclose it in quotes, e.g. Dim myStr As String = "5"

    and any Text property of a Control, such as TextBox or Label, etc also has to be in quotes, e.g. TextBox1.Text = "150"

    since you have shown the willingness to learn and do what is needed, I'll give you this to try. Try it in an app with 3 buttons and 3 textboxes (rename TextBox3 to acall). Type a 1 or 0 in the acall textbox, then click the buttons. You'll see it only allows one decimal point, and adds however many digits after the decimal

    study this code so you can see how it adds ONE decimal point, and concatenates the digits to the end of the TextBox Text. Also notice there is no need for all those variables

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        Select Case acall.Text
          Case Is = "0"
            If Not TextBox1.Text.Contains(".") Then
              TextBox1.Text &= "."
            End If
          Case Else
            If Not TextBox2.Text.Contains(".") Then
              TextBox2.Text &= "."
            End If
        End Select
    
      End Sub
    
      Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Select Case acall.Text
          Case Is = "0"
            TextBox1.Text &= "1"
          Case Else
            TextBox2.Text &= "1"
        End Select
      End Sub
    
      Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Select Case acall.Text
          Case Is = "0"
            TextBox1.Text &= "2"
          Case Else
            TextBox2.Text &= "2"
        End Select
      End Sub

     

    • Marked as answer by docfnt Saturday, June 19, 2010 8:10 AM
    Saturday, June 19, 2010 7:16 AM
  • I see what you mean! I have a LOT more reading to do now! I'm gonna find everything I can on what's going on here, why, & how it works. This is the part of the huge galaxy of stuff I need to learn, since I'm just doing this at home on my own time. I have never been to school for programming, so I'm spending all my free time trying to learn this. I know I have a long, long way to go, but I'm loving every minute of it!

    Somewhere, some day, I hope to find some tricks on doing the algabra method for writing events. I know I wind up using WAY too much code because I don't know how to write events that cover several related events at once.

    Thanx a million, Jwavila! You have truely given me some new insight, & more avenues to study, so I can make the next level. I might be a snail going up a hill, but even if I never reach the top, I'll keep going, because I know there's one there, somewhere!  :-)

     PS;

    Odd, the program you wrote ignores what I ut in TB1, or acall. TB2 comes up ".12" regaurdless. I'll have to run this in the "Posi Calc" to see what happens.

    PPS...Once in the Posi Calc, it rode like a Cadillac! Everything runs Great! Working on losing those Zeros now...

     

    Saturday, June 19, 2010 8:09 AM