I kind of hoped that it was as straightforward as that!!!
It is. :) Well, not quitethat 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. ;)