Hi,
I am a total novice to VB.Net, and have been asked to develop a simple 4 function calculator as part of my coursework.
I am really struggling!!! I would be extremely grateful if anyone could please help???

( I have managed to use AppendText to get the numbers in the display panel, but I am at a loss with the code for the functions add, minus divide and multiply)

Thanks:confused:

Recommended Answers

All 15 Replies

I am at a loss with the code for the functions add, minus divide and multiply

kk, the functions themselves are easy. Just do some such like this.

Private Function Add(lhs As Integer, rhs As Integer) As Integer
    Return lhs + rhs
End Function

If you pull numbers from the display panel with something like CInt(pnlDisplay.Text) then you can save each of the numbers punched in. Once you have 'em, you can pass 'em to the functions and push the result to the panel with something like pnlDisplay.Text = CStr(result) . Easy squeezy, don't forget to check for division by zero. :)

im doing this at a level now just remember that Integer type variables are for simple math (or double type variables for more complex stuff) . Tex/string isnt and for add use +, divide use / and multiply use *

e.g

Dim var1 as double
dim var2 as double
dim ans as double
 
var1 = Inputbox("Enter the 1st number")
var2 = Inputbox("Enter the 2nd number")
 
ans =  var1 * var2
 
msgbox "The answer is " & ans & " "

Thanks for replying guys..
Another problem I also have, is that the calculator is to be similar to a real one... i.e. only one display box for input & output..

I have declared Num1, Num2 and Total As DECIMAL
For the addition button, I have used the code..
Num1+= Val(txtDisplay.text)
Num2+= Val(txtDisplay.text)
then I've entered txtDisplay.Clear () to clear the display box

Then for the = button, the code: txtDisplay.text = Total

The idea being that I added the 2 numbers together and then they would be displayed when I press the = sign.

I have a feeling that I am way off guys, and ask you to forgive me for being a total novice!!

I have a feeling that I am way off guys, and ask you to forgive me for being a total novice!!

No fair! For a novice you've got your head screwed on pretty good. That's basically the same setup I was thinking of, and I can boast a few more years of .NET experience. ;)

I look forward to seeing your results. :)

No fair! For a novice you've got your head screwed on pretty good. That's basically the same setup I was thinking of, and I can boast a few more years of .NET experience. ;)

I look forward to seeing your results. :)

With regards to my problem, I have found that if I make 2 display boxes (exactly the same) and then add the two values from i.e. textbox1 and textbox2 - the show the result in textbox1 (so it appears there is only 1 text box)- it works.
But is there a wrong or right way of doing things?
Using 2 display boxes seems to be an easy way out!!
I will welcome any critcal comments:confused:

i think maybe the proper way would be to use a control array

But is there a wrong or right way of doing things?

The only wrong way is the one that doesn't meet your requirements. Anything else is fair game. :D

Using 2 display boxes seems to be an easy way out!!

Using 2 boxes is harder, I think. You have to keep track of multiple input controls when you really only need one. Ignoring the tricky user interface details that make an app usable, your form really doesn't need more than this.

Imports System

Public Class MainForm
    Private _lhs As Decimal
    Private _rhs As Decimal
    Private _op As String

    Private Sub Operator_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles btnAdd.Click, btnSub.Click, btnMul.Click, btnDiv.Click

        ' Set up the first half of our calculation
        _lhs = CType(uOperand.Text, Decimal)
        _op = DirectCast(sender, Button).Text

        ' Reset the input box for the next operand
        uOperand.ResetText()
        uOperand.Focus()
    End Sub

    Private Sub Result_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles btnDone.Click

        ' Yay! Get the second half
        _rhs = CType(uOperand.Text, Decimal)

        ' Calculate the result

        uOperand.Text = CStr(result)
    End Sub
End Class

I am feeling totally deflated when I read you guys' knowledge on the subject, because I am feeling very useless about my basic programming skills!!!
I want to build a basic calculator with the 4 basic functions - but can't!!!!
If anyone would be so kind as to guide a thicky like me (who has got swallowed up in it all!!) - then I promise to be the best man at their daughter's wedding!!!!!

Please explain!!!!!

I want to build a basic calculator with the 4 basic functions - but can't!!!!

Windows Forms apps are pretty basic. ;) The first thing you need to do is build the interface. That's just a game of dragging controls around and playing with properties until you have a good prototype. It's a lot easier now than it used to be. After you've got the interface, you make it work by filling out the events. Do it one by one so you don't get swamped. Once that's all working, you tweak until the app is production quality. The last step is the hardest.

Don't get me wrong, programming is really hard and it takes tons of dedication and passion to get good at it. The only way to learn this stuff is to slosh through the muck on your own. But it's worth every bead of sweat you shed. :cheesy:

k, that's a dreary picture. Let's say that most of the work is on your head and if you really get stuck on something specific, I can help. :)

Windows Forms apps are pretty basic. ;) The first thing you need to do is build the interface. That's just a game of dragging controls around and playing with properties until you have a good prototype. It's a lot easier now than it used to be. After you've got the interface, you make it work by filling out the events. Do it one by one so you don't get swamped. Once that's all working, you tweak until the app is production quality. The last step is the hardest.

Don't get me wrong, programming is really hard and it takes tons of dedication and passion to get good at it. The only way to learn this stuff is to slosh through the muck on your own. But it's worth every bead of sweat you shed. :cheesy:

k, that's a dreary picture. Let's say that most of the work is on your head and if you really get stuck on something specific, I can help. :)

Cheers;)

Hi,

I am now completely stuck!
I have my form set up - I have my numbers displayed in my textbox (txtDisplay) using AppendText.. but I cant work out the code for the function buttons. It is really getting me down - as I have been trying for a week now!!:sad:

I would really appreciate some help - given to me is laymans terms!!

Thanks!

It is really getting me down - as I have been trying for a week now!!

Hmm, but think of how much you must have learned from all those tries. :) Time is never wasted, even if you don't feel productive. But let's get you a jumpstart on the problem. What have you tried and how didn't it work?

Hmm, but think of how much you must have learned from all those tries. :) Time is never wasted, even if you don't feel productive. But let's get you a jumpstart on the problem. What have you tried and how didn't it work?

Thanks for replying:)

I am so far able to type in my numbers into the display panel
(Which I thought was a major achievement!):lol:

As far as I am aware - I understand that the main coding is in the +, - , / and * buttons.

I want to know how the 'plus' button will work, and then hopefully I'll be able to work out the other functions from this.......:confused:

My variables are Dim PreviousValue As Double
Dim CurrentValue As Double
Dim Total As Double

For the + button, I have written...

txtDisplay.Text = PreviousValue 'first value
txtDisplay.Clear()
txtDisplay.Text = CurrentValue 'second value
Total = PreviousValue + CurrentValue

I kind of hoped that it was as straightforward as that!!!
But was obviously very wrong!!!

I have been told by a few people to use a Control Array for my functions.... but I wouldn't know where to start:sad:

I would be extremely grateful for some help - and will add whoever can help straight to the top of my Christmas Card list:lol:

Thanks
I can build a simple calculator with 2 display panels i.e. one input one output - but this one is to have only 1 display panel - input & output!!!

I feel that this is fairly harsh for a 1st Assignment - don't you?

I kind of hoped that it was as straightforward as that!!!

It is. :) Well, not quite that easy, but it's close. In fact, the code I posted before shows the easiest way I could think of to set it up. The only thing missing is the design code for the form itself, and the actual calculations. Let's walk through it real quick.

Private _lhs As Decimal
Private _rhs As Decimal
Private _op As String

_lhs is the left side operand and _rhs is the right side operand. That's pretty obvious. The trick is that I also saved the operator as a string. That way when you click the = button, it knows what calculation to do.

Private Sub Operator_Click(ByVal sender As Object, ByVal e As EventArgs) _
    Handles btnAdd.Click, btnSub.Click, btnMul.Click, btnDiv.Click

    ' Set up the first half of our calculation
    _lhs = CType(uOperand.Text, Decimal)
    _op = DirectCast(sender, Button).Text

    ' Reset the input box for the next operand
    uOperand.ResetText()
    uOperand.Focus()
End Sub

This is the event handler for the +, -, *, and / buttons. Each of the buttons' Text property matches their operation, so btnMul.Text is "*". When one of the buttons is clicked, _lhs is set to the value in the text box (uOperand). I'm basically assuming that at this point, whatever is in the textbox is the left side operand. The event also saves the Text property of the button that was clicked, thus saving the operator. That's the tricky part.

Private Sub Result_Click(ByVal sender As Object, ByVal e As EventArgs) _
    Handles btnDone.Click

    ' Yay! Get the second half
    _rhs = CType(uOperand.Text, Decimal)

    ' Calculate the result

    uOperand.Text = CStr(result)
End Sub

This is the event for the = button. When it's clicked, the left side operand and operator have already been saved (we're assuming :)), and the right side operand is in the textbox. The meat of the event handler is actually a comment, ' Calculate the result , where you would add a switch or if chain that checks the value of _op and adds, subtracts, multiplies, or divides accordingly.

I have been told by a few people to use a Control Array for my functions....

Ya, but I think that would just confuse you more. Best to keep it simple 'till you have something working and can boost your confidence. :)

I feel that this is fairly harsh for a 1st Assignment - don't you?

I think it's a great assignment! It's forcing you to think squiggly. Programmers have to think squiggly or they'll never get cool stuff done. For you, let's just call it 'thinking outside the textbox'. :lol:

Hmm, to be perfectly honest, event driven programming really isn't good for beginners because there's just so much going on that you can't see. A good procedural course to get an idea of how programmers have to think would be better. Then once you know how to do a list of things in order, they can introduce how to do a list of things any which way. ;)

commented: Excellent - and a very kind person ! +1

I really appreciate you taking time out to show me this.
I will try to get it working - but if I can't - then forgive me for being a numpty!!
My head is full of garbage at the moment - and I'm gonna have a nice cuppa...

Thankyou so much - much appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.