Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2005, 4:08 PM   #1
Psychosis
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Psychosis is on a distinguished road
Schoolwork help

I started this online course a while ago, and unfortunately I've pretty much forgotten 99.9% of everything we were taught, and I'm about 4 units behind, so I was wondering if you guys could give me a hand. Thankfully this site isn't blocked at my school. So the first assignment is basically a counter. Here's the first assignment:

(BTW, I know how to do the GUI's, but I can't get the rightclick/properties/copyimage link type of thing, computer doesn't allow it) So when it says "It should look like this, I know how to make the GUI's.

Quote:
Originally Posted by Assignment
Now, add a label and a button onto the form. Name the label lblCount and set it's Caption to "Count: 0" (no quotes). Adjust the Font to display the text of the label in a larger font and change the Alignment property to center the text in the label. Name the button btnClick and set it's Caption to "Click Me!", again, no quotes. Your GUI should now look like this:



Now, write code for the button's Click event that will cause the number in the label to increase by 1 each time the button is clicked.

Start by declaring a variable to count the number of clicks. Think about a good variable name (Note: Count is a reserved word and therefore can't be used), what type it should be and where you should place the declaration statement.

Next, in the button's Click event, write a statement that will increase your counter variable by 1. Then write a line of code that will display that number in the label, as follows:



Next, add a list box (lstNumbers) and a second label (lblTotal). Write code so that each time the button is clicked a random Real number between 50 and 100 will be added to the list box and the total of all the numbers in the list box will be displayed in lblTotal. Now, your GUI looks like this:



Start by declaring two Single variables to hold the random number and the total. Again, make them meaningful variable names.

The Rnd function built-in to VB generates one random number between 0 and 1, not including 1. That is,

0 <= Rnd < 1

Since we need these numbers to be in the random of 50 to 100, which has a range of 51 numbers, we need to multiply Rnd by 51:

0 <= Rnd*51 < 51

Finally, we want the lowest random number to be 50, so we add on 50:

50 <= Rnd*51 + 50 < 101

To display a number in a list box, use the AddItem method of the Listbox object:

lstNumber.AddItem Number
But, since these numbers are Real numbers, we'll control how many decimal places are displayed with the FormatNumber function:

lstNumber.AddItem FormatNumber( Number, 1 )
Finally, write code that will accumulate the sum of the numbers in your total variable. Display this value in the label on the line below the title "Total:".

Total = Total + Number 'add the random number to the accumulated total
lblTotal.Caption = "Total:" & vbCrLf & FormatNumber( Total, 1 )
Test: Now, add a new label to the GUI and display the average of all the numbers in the list box, rounded to 2 decimal places.

Finally, add two more list boxes (lstLow and lstHigh) and a second button (btnTransfer) to the GUI. Set the Caption of btnTransfer to "Transfer". Write code for the Transfer button that will copy all the numbers above 75 into lstHigh and all the numbers 75 and lower into lstLow. Do not declare any more variables to write this code.

Recall that the List Box's ListCount property stores the number of items in a list box and the ListIndex property refers to the index of an item in the list box, starting at 0. That is, the ListIndex of the first item in a list box is 0. The second item has ListIndex of 1, third is 2, etc. The last item in a list box is at ListIndex of ListCount-1.

Also note that the Text property of the List Box contains the actual text of the item at position ListIndex. So, if the fourth item in the list (ListIndex is 3) is 78, then

lstNumbers.ListIndex = 3
Number = Val( lstNumbers.Text )
will set Number to the value of the number in the fourth line of the list box.

Deciding which list box to place the value into requires an If statement:

If Number > 75 Then
Else
End If
and you will need to transfer all the numbers in the list box lstNumbers into one of the two list boxes, so you need a loop:

For count = 0 to lstNumbers.ListIndex-1
Next count
Basically I don't know how to make the counter thing. When I try and go like:

Dim intNumber as Integer
<The command button line thing, I can't remember the exact text of it, but anyways)
intNumber = intNumber + 1 <- But this doesn't work when I put it beside the text label.

Edit: I should've just meant that I need help with the coding. That's it, if there's anything else that I need, I'll post it here. But with the work help, (not trying to be greedy) but if a response could be immediate, that'd be VERY appreciated. Thank you.
Psychosis is offline   Reply With Quote
Old Nov 30th, 2005, 4:26 PM   #2
MBirchmeier
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 211
Rep Power: 4 MBirchmeier is on a distinguished road
Quote:
Originally Posted by Psychosis
I started this online course a while ago, and unfortunately I've pretty much forgotten 99.9% of everything we were taught, and I'm about 4 units behind, so I was wondering if you guys could give me a hand. Thankfully this site isn't blocked at my school. So the first assignment is basically a counter. Here's the first assignment:...
While it's appreciated that you're mentioning this is an assignment no one is going to give you a handout. Especially not leading to potential cheating / academic misconduct.

If you have specific questions, or post what you have, and ask things like 'why isn't this compiling' or why does the counter get stuck at 3 or why do I keep getting a segmentation fault then we'd be more than happy to answer though.

Otherwise read the book, ask the teacher, and do your own work.

Chances are any help we give you wouldn't be anything you'd have to hide from your teacher.

-MBirchmeier
MBirchmeier is offline   Reply With Quote
Old Nov 30th, 2005, 5:09 PM   #3
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Agreed... we don't do homework here unless it is a specific question. If you can supply us with some code and specific questions we might be able to help.
crawforddavid2006 is offline   Reply With Quote
Old Dec 3rd, 2005, 6:53 PM   #4
Psychosis
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Psychosis is on a distinguished road
Okay, here's the GUI:



And here's my code:

Quote:
Originally Posted by MyCode
Private Sub btnClick_Click()
intNumber As Integer

intNumber = intNumber + 1

lblCount.Caption = "Count: " + intNumber

End Sub
Yet I get a compile error stating: "Statement invalid outside type block" - from what I "understand", my space for the text to accumulate is big enough...
Psychosis is offline   Reply With Quote
Old Dec 3rd, 2005, 6:59 PM   #5
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 597
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Oh i forgot to mention something. If you want me to do your home work it's called Pay Pal.
crawforddavid2006 is offline   Reply With Quote
Old Dec 4th, 2005, 3:07 AM   #6
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
jesus man, what could you have learned that you've already forgotten that covers these ideas?
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Dec 4th, 2005, 5:44 AM   #7
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
Originally Posted by Psychosis
Private Sub btnClick_Click()
intNumber As Integer

intNumber = intNumber + 1

lblCount.Caption = "Count: " + intNumber

End Sub
Maybe it has changed in newer version, but shouldn't it be:
lblCount.Caption = "Count: " & intNumber

Absolutely not sure, I haven't touched VB for a few years.
Polyphemus_ is offline   Reply With Quote
Old Dec 4th, 2005, 5:57 AM   #8
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,183
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by Polyphemus_
Maybe it has changed in newer version, but shouldn't it be:
lblCount.Caption = "Count: " & intNumber

Absolutely not sure, I haven't touched VB for a few years.
Dunno about VB6 (never touched it), but in VB.NET, you can use either & or +.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Dec 4th, 2005, 6:54 AM   #9
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 4 ivan is on a distinguished road
It was a long time while I programmed in VB6 so I may be wrong about this, but I think you must use

Dim intNumber As Integer

VB sucks...
ivan is offline   Reply With Quote
Old Dec 4th, 2005, 5:24 PM   #10
Psychosis
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Psychosis is on a distinguished road
Oh that's probably what I did wrong. (The & instead of + ), as well, I thought I had the "Dim" part in there too...but I guess not.

Okay, I got the count going right, but it's only working once (Only going to "1", and not continuing) here's my code:

Quote:
Private Sub btnClick_Click()
Dim intNumber As Integer

intNumber = intNumber + 1

lblCount.Caption = "Count: " & intNumber

End Sub
And uhm, bl00dninja, I don't really think that was nessessary...
Psychosis is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC