Any Visual Basic geeks here?

Status
Not open for further replies.
Joined
May 23, 2004
Messages
2,586
Location
Northeast
Just wondering if anyone is really good with this software. I'm taking a class where we use Visual Basic 2005 and I'm really, really struggling with it. It just doesn't make sense to me, then again my teacher isn't very good at all.
 
I won't call myself an expert, but I started writing VB 6.0 code in '99, and recently I've spent some time writing a rather large VB.NET 2003 application. This app builds special TCP/IP packets for talking to PLC-based equipment over the network.

I find that .NET is about more difficult than VB 6 unless one has a good background in other OOP languages. It's taken me some time to learn my way around, and I still seem to struggle more than I did with VB 6, which I picked up very quickly.

Is there anything specifically that I can help with?
 
You'd probably get a kick out of what im trying to do. This would probably take an experienced person 5 minutes to do.

All i have to do is make a simple calculator. I've coded the number buttons so that the show up on the display, but i cant get the numbers to add up when i try to do addition. Since i haven't gotten addition to work, i havent even tried division, multiplication, or subtraction.

I put my crummy source code here, and picture of the form here
 
I assume you're doing a traditional style calculator and not RPN. You've got the right idea more or less. You'll need to change what you're doing in the operation (add, subtract, etc) click handler. You should store that value that's currently in the textbox, clear it, and store what kind of operation it is. Then, when the equals button is clicked, fetch the stored value and using the current contents of the textbox perform the type of operation that you stored earlier.

A couple of hints:

For storing the type of operation I would do something very simple, I like to use constants when enumerating integers.

Const CONST_OP_ADD as Integer = 1
Const CONST_OP_SUB as Integer = 2
Const CONST_OP_MUL as Integer = 3
Const CONST_OP_DIV as Integer = 4

Dim Op_Type as Short = 0
Dim value1 as single

'In the add handler
Op_Type = CONST_OP_ADD
value1 = val(txtDisplay1.Text)

'In the equals handler
Dim result as Single

Select Case Op_Type
Case CONST_OP_ADD
result = value1 + Val(txtDisplay1.Text)
Case CONST_OP_SUB
result = value1 - Val(txtDisplay1.Text)
Case Else
Op_Type = 0
End Select

if Op_Type <> 0 then
txtDisplay1.Text = result.tostring("0.0######")
op_type = 0
end if


You will also want to add validation of the text in the textbox before attempting to convert it to floating point(single data type). I typically use IsNumeric in the textbox's lostfocus event. It can get a little bit tricky, but I'll leave fancier details out for now. I've given you a bunch of hints but left some stuff out, have fun.

I've been told by true windows programmers that I code like a hardware guy, so if any real programmers come along please be nice, ;-).
 
The teacher gave us an extension since so many people were having trouble. I haven't tried to work on it again recently because of this, but im sure all that will definietly come in handy. Thank you very much!
 
Status
Not open for further replies.
Back
Top