Quantcast

Maximum PC

It is currently Sat Dec 20, 2014 12:48 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Need help with my project in vb.net
PostPosted: Wed Jun 11, 2008 9:33 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
So i have to make a simple adding and subtracting calculator. The problem is thati don't know how to make vb.net display negative numbers. For example when i do 40-99 it gives me 59 instead of -59. Heres mt code:

Code:
Option Strict On
Public Class frmCalculator
    Dim total1, total2 As Double
    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        txtDisplay.Text = txtDisplay.Text & btn1.Text
    End Sub

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        txtDisplay.Text = txtDisplay.Text & btn2.Text
    End Sub

    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        txtDisplay.Text = txtDisplay.Text & btn3.Text
    End Sub

    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        txtDisplay.Text = txtDisplay.Text & btn4.Text
    End Sub

    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        txtDisplay.Text = txtDisplay.Text & btn5.Text
    End Sub

    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        txtDisplay.Text = txtDisplay.Text & btn6.Text
    End Sub

    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        txtDisplay.Text = txtDisplay.Text & btn7.Text
    End Sub

    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        txtDisplay.Text = txtDisplay.Text & btn8.Text
    End Sub

    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        txtDisplay.Text = txtDisplay.Text & btn9.Text
    End Sub

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        txtDisplay.Text = txtDisplay.Text & btn0.Text
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        total1 = total1 + Val(txtDisplay.Text)
        txtDisplay.Clear()
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtDisplay.Clear()
    End Sub

    Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
        total2 = total1 + Val(txtDisplay.Text)
        txtDisplay.Text = CStr(total2)
        total1 = 0

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        total1 = total1 - Val(txtDisplay.Text)
        txtDisplay.Clear()
    End Sub
End Class


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 11:33 am 
SON OF A GUN
SON OF A GUN
User avatar

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Is there a display property for the text box?


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 2:49 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
no its only .text


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 3:06 pm 
Team Member
Team Member
User avatar

Joined: Sun Jun 12, 2005 5:52 pm
Posts: 1319
Location: Infront of my Computer.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
total1 = total1 - Val(txtDisplay.Text)
txtDisplay.Clear()
End Sub

try changing the line to this

total1 = Val(total1) - Val(txtDisplay.Text)


Top
  Profile  
 
 Post subject:
PostPosted: Wed Jun 11, 2008 8:26 pm 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
ok i tried that but it doesnt work.


Top
  Profile  
 
 Post subject:
PostPosted: Sat Jun 14, 2008 4:39 pm 
Team Member [Top 500]*
Team Member [Top 500]*
User avatar

Joined: Thu Jun 24, 2004 2:22 pm
Posts: 8721
Location: Cleveland, Ohio
I am not sure what you are doing? I count ten buttons on the form?

I would create a simple form like this:

Image

Three text boxes on the form. TextBox1, TextBox2 and TextBox3. Use labels for Variable1, Variable2 and Result. Place the operators on the form using radio buttons.

The code for this looks like this:

Code:
Public Class Form1
    Inherits System.Windows.Forms.Form


    'Declare FirstNum and SecondNum variables
    Dim FirstNum, SecondNum As Double

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Assign text box values to variables
        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text

        'Determine checked button and calculate
        If RadioButton1.Checked = True Then
            TextBox3.Text = FirstNum + SecondNum
        End If
        If RadioButton2.Checked = True Then
            TextBox3.Text = FirstNum - SecondNum
        End If
        If RadioButton3.Checked = True Then
            TextBox3.Text = FirstNum * SecondNum
        End If
        If RadioButton4.Checked = True Then
            TextBox3.Text = FirstNum / SecondNum
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class


This is pretty much right out of Chapter 5 Visual Basic.NET Step by Step by Michael Halvorson.

There should be no problem displaying a negative number and nothing is needed.

Note the Radio Buttons are placed in a Group Box! That allows only one to be checked (True) of the group.

<EDIT>
To make it simple the entire source code and form can be found here as a sample:

http://www.bearblain.com/downloads.htm

Just download the zip folder saving to a location on your machine. Right click on the zip folder and extract the contents to a convenient location. Open VB.NET and navigate to the files and open them. Hope that helps.
</EDIT>

Ron


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 7:39 am 
Little Foot
Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
heres the problem reloadron, we haven't gotten to radio buttons so we have to use regular buttons.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 8:12 am 
Team Member
Team Member
User avatar

Joined: Sun Jun 12, 2005 5:52 pm
Posts: 1319
Location: Infront of my Computer.
For more efficiency, you can rewrite his if statements like this.
Code:
If RadioButton1.Checked = True Then
            TextBox3.Text = FirstNum + SecondNum
       Else If RadioButton2.Checked = True Then
            TextBox3.Text = FirstNum - SecondNum
       Else If RadioButton3.Checked = True Then
            TextBox3.Text = FirstNum * SecondNum
       Else If RadioButton4.Checked = True Then
            TextBox3.Text = FirstNum / SecondNum
       Else
            msgbox("No operator was selected.", "Error")
        End If
    End Sub


I did not have a computer with VB.NET near me so I could not double check to make sure I wrote the code correctly, but this code would give you a call back if there was no radio button selected in the group.


Top
  Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 10:29 am 
Team Member [Top 500]*
Team Member [Top 500]*
User avatar

Joined: Thu Jun 24, 2004 2:22 pm
Posts: 8721
Location: Cleveland, Ohio
xxmainframexpxx wrote:
heres the problem reloadron, we haven't gotten to radio buttons so we have to use regular buttons.


OK, then create a form and place 3 text boxes on your form. Then add some buttons using four buttons for the basic operators of Addition, Subtraction, Multiplication and division. Something like this:

Image

A download of a sample may be found here:

http://www.bearblain.com/downloads.htm

It is the basic calc program.

Hope that helps.

Button code looks like this:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text
        TextBox3.Text = FirstNum + SecondNum
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text
        TextBox3.Text = FirstNum - SecondNum
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text
        TextBox3.Text = FirstNum * SecondNum
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text
        TextBox3.Text = FirstNum / SecondNum
    End Sub
End Class


Ron


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.