Visual Basic help! [Archive] - Glock Talk

PDA

View Full Version : Visual Basic help!


epsylum
11-17-2005, 18:38
Okay this may sound bad. But I am taking a general computer class at my community college. It is called "computer fundementals for technology". Anyhow, the first half of the semester was Microsoft Office. I aced the whole thing since I use office every day at work. But the last half is VB. I have never used VB and so far have only had limited experience programming my TI-83 calculator. I HATE programming and am horrible at it. Anyhow I have tried for 3 days now to get just the basics to work (like getting entered numbers in a textbox to calculate in a formula and display on a label) and I can't. My teacher is useless and is more monotone than Ben Stein. The book is horrible as it's one of those do exactly what I say books so you wind up only making exactly what it wants you to make and doesn't go into the basic ideas of how things work, just "type this and this happens". I am majoring in Design Technology with maybe a minor in machine design. I want to be a draftsman (I already passed all the AutoCAD classes), not a programmer. But, I have to pass this course to get my degree.

There is only one assignment for the VB portion of the semester. Make a VB program. If I can't get this done, I will probably fail.

So basically, if anyone is good with VB, could you please help me out. I will take tips all the way to if you feel like banging out a simple ballistic data program (just eneter weight, diameter, and velocity to get ME, TKO, and sectional density and such) and e-mailing me the non-compiled VB file, I would owe you big time.

PM me if you wish to help.

Thanks.

cgwahl
11-17-2005, 19:50
VB6 or VB.NET?

epsylum
11-17-2005, 20:10
VB6

David_G17
11-18-2005, 14:34
walk through this:
http://www.hitmill.com/programming/vb/hello.html

and then this:
http://www.hitmill.com/programming/vb/validation.pdf

this is similar to what you are doing:
http://www.hitmill.com/programming/vb/temp_convert.html

epsylum
11-18-2005, 15:00
Thanks, that is a lot clearer than my book.

grokdesigns
11-18-2005, 16:29
This website has tons of VB code samples:
http://planetsourcecode.com/vb/default.asp?lngWId=1

You can sort by difficulty and examine the code. I've been programming in VB for about 6 years now and have found countless helpful examples on that site.

David_G17
11-19-2005, 12:48
Originally posted by epsylum
Thanks, that is a lot clearer than my book.

99.99% of the programming I know, I learned by googling "c++ tutorial", "coldfusion tutorial", "php tutorial", etc.

textbooks are worthless in my experience.

epsylum
11-19-2005, 20:55
Originally posted by David_G17
99.99% of the programming I know, I learned by googling "c++ tutorial", "coldfusion tutorial", "php tutorial", etc.

textbooks are worthless in my experience.

I wish they were worthless, but I paid $100 for this POS book. Damn college textbooks.

Oh well I am buying next semesters books on ebay and selling these. Screw the bookstore.

Nyper
11-21-2005, 12:14
Check your PMs.

Creeper1313
11-23-2005, 13:14
You may have already gotten your answers by PM, but here's a quick and dirty explanation on how to do that assignment.

In VB6, create a new project. On the gray canvas of the form that you have to work with add the following.

Three textboxes with labels. Set their names and labels to be meaningful like: "Weight", "Diameter", and "Velocity".

A command button called "Calculate".

Labels for your answers. "ME: ", "TKO: ", "Sectional Density: ", etc.
You can just have the caption now, we'll add the answer later. Make sure you also give them names you'll remember (eg TKOanswer). Also make them wide enough to accept your answer alongside the label. IE "TKO: _____________________" (pretend those are spaces)

Now go into the code itself. At the top left there will be a drop down box with all the objects currently on your form. Pick the command button you created and named "Calculate".

By default it will pick the Click event, which is the one you want (there are many others, but don't worry about them now). The code will appear as follows:

Private Sub Calculate_Click()

End Sub


Between these two lines you will type the code for what you want to have happen when the button is clicked. In the case of your program, this is doing math on the items entered in the textboxes. I'll use TKO as an example because I found it's formula on google (heheh) "diameter x weight x velocity divided by 7000" (based on caliber for diameter, grains weight, fps for velocity... change to whatever formula you'd want).

To do this math, you want your code to look in the 3 textboxes and draw their numeric values for the formula, then you want to put it in the Answer captions (eg "TKO: ")

Private Sub Calculate_Click()

If IsNull(Diameter) = True Or IsNull(Weight) = True Or IsNull(Velocity) = True Then
MsgBox ("All textboxes must be filled in to calculate. Try Again")
Exit Sub
End If

TKOanswer.Caption = "TKO: " & CStr( (CDbl(Diameter) * CDbl(Weight) * CDbl(Velocity) ) / 7000 )

End Sub


The first part of the code with the If IsNull statements looks at your 3 text boxes and makes sure none of them are blank, as this will cause an error. The Exit Sub just quits the Click event before it does anything else.

You are probably wondering what all the CStr() and CDbl() functions do. Let me explain. Since textboxes contain text by default, and your math needs actual numbers, not text, CDbl converts the text values in the 3 textboxes to the type "Double" which is a decimal number (there are many differnt data types, eg CInt would be used to convert to an Integer and get rid of decimals). Be careful with the parenthesis, order of operations, etc. The outer CStr is to convert the double answer back into a String (Text) that the TKOanswer.Caption needs. (the .Caption is the property of a label that contains it's text, objects in VB have tons of properties, such as length, width, style, etc.)
The "&" is to combine the Text "TKO: " with the CStr'd text of the answer.

So for example if someone entered ".223" "72" and "2700" in the 3 respective textboxes and pushed calculate, this line would roughly translate into:
TKOanswer's caption = the text "TKO: " combined with
The string conversion of the answer to:
(.223 * 72 * 2700) / 7000

Just repeat that code for the other answer labels and change the math as appropriate. And like I said, watch your parethesis... e.g. The outer Cstr( ) should contain all your math.
Let me know if you have any other questions.

PS sorry the code I posted isn't nicely indented. The spaces seem to get chopped off in posting.

Creeper1313
11-23-2005, 19:32
I whipped up an example to show you some screens. Also, it seems the IsNull wasnt the correct approach for protecting agianst leaving fields blank, just make sure the 3 textboxes do not = "" (blank).

http://img474.imageshack.us/img474/5075/vb16mt.png

Here's it in action (it will update the answer each time you change the values and re-click the "Calculate" button.

http://img474.imageshack.us/img474/6093/vb27lr.png

epsylum
11-26-2005, 11:58
Thanks a lot Creeper.

I am going to give it another shot right now.

epsylum
12-01-2005, 19:55
Okay, it's overwith.

I turned it in today. I had some issues with file transfering (I need a dang USB flash drive BADLY) trying to get it on the school computer. I basically had my code, but everything else was missing. So I just copied and pasted my code and made the form again. There were some in the class who had fancy shmancy stuff, but most were along my level of work, so I think I am okay there. There was even a guy who had a lot less than what I had and he said the teacher said he would probably get a B on it, so I am even hopeful for a decent grade out of it.

What I am getting at is,

THANK YOU!!!

to everyone who offered help.

I completely forgot to compile it, but I will next week and post it on here if anyone wants it. It's nothing fancy by far, but could see myslef using it every now and then