Thread: Schoolwork help
View Single Post
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