Jump to content

Visual Basic


Primarygun

Recommended Posts

I'm creating a project with visual basic.

There are nine labels on the form. I want to create a system in which the labels hold different integers from 1 to 9.

I have a problem in creating the command for

not including zero*

the labels are of different value or words from each other*

Link to comment
Share on other sites

Not sure I follow. You have a set of data that may include the number zero, but you want to exclude that from the label if it happens to come up in distribution, is that it? In that case just use something like "if > 0" in your program logic. So it would look something like this:

 

dim myVariable as integer
if myVariable > 0 then
 lblMyLabel.text = myVariable
else
 lblMyLabel.text = "Error: The integer was a zero"
end if

 

Make sense?

Link to comment
Share on other sites

I'm creating a form which will hold nine labels, you can call them boxes.

The Arrangement is like those in soduku.

Soduku really has 81 small boxes in 9 large boxes.

I'm creating one large box but encountered the problem I mentioned.

Lol. It is a prerequisite to make a large box before creating nine large boxes, but I still get none!

Link to comment
Share on other sites

I'm confused what you are trying to do.

 

You have 9 labels, in a square shape, each holding an integer 1-9.

 

What do you want the prog to do? Number all the squares? And presumably you want it to number them differently each time, you don't want:

lbl1 = "1"

lbl2 = "2"

 

Can you post your non-working code, maybe I'll get a better idea.

Link to comment
Share on other sites

OK, I got that part. What I don't understand is what you want.

 

Do you want to click a button and all the labels are given a caption (number)? And you want them to be given different captions (numbers) each time you click the button?

 

If not then you could just have:

Private Sub cmd1_Click()
lblOne = "1"
lblTwo = "2"
lblThree = "3"
' etc etc until lblNine
End Sub

Link to comment
Share on other sites

Yes. You almost digested my exotic tongue.

When I click a button ( Press Me ),

Then,

a corresponding set of labels with different numbers appear.

There could be a way of doing this but it is too tedious.

Writing commands such as

Label1.Text <> Label2.Text

If I do it in this way, it is very slow for both the typing process and computing process.

So the question is here, do you have any other neater, shorter commands?

Thanks

Link to comment
Share on other sites

OK, still not getting it!

 

Do you want this kind of pattern:

 

squares.jpg

 

whereby each time you press Click_Me you get the numbers in different places/positions?

 

Or do you want:

Private Sub cmd1_Click()
lblOne = "1"
lblTwo = "2"
lblThree = "3"
' etc etc until lblNine
End Sub

if you did that then you would have already made the form with the 9 labels and named them appropriately.

Link to comment
Share on other sites

Ah, ok :)

 

Well you need some way to start it off. To generate a random number between 1 and 9 use this:

 

Dim num As Integer

Private Sub cmdClickMe_Click()
   Randomize
   num = Int((9 * Rnd) + 1) 'Generates random num between 1 and 9
   lbl1 = num 'Displays result in label 1
End Sub

where num is your randomly generated number.

 

OK, then you go one of two ways:

 

1) You use a similar command for lbl2 and lbl3 however you will need to include If functions to ensure that no two labels contain the same captions.

 

2) From the first random you could then generate the rest, e.g.

after the first number has been generated

If lbl1 = "1" Then
lbl2 = "2"
Else
lbl2 = "1"
End If

and then repeat that code. The If function would soon become quite complex. But then it would in method 1 also.

 

I don't know. I'm just coming up with this off the top of my head. There could be a better way.

 

[edit]if you want an example of method 1 I can write one for you.

Link to comment
Share on other sites

Why not just shove the numbers 1 to 9 in an array and then rearrange that array to randomise the numbers in it and then set each label to it's corresponding array element.

 

Or, you could just set them to defaults to begin with and then randomise the labels in the form or store references to those labels in an array and randomise them by swapping values around in a particular way.

 

There are plenty of good ways/algorithms to randomise sets of numbers or other data reasonably well, although if you need them randomise in such a way as to maintain certain rules about placement and sums of rows etc then you'd have to do a little more work.

 

This would avoid the need for lots of ifs and checks as you can only rearrange the numbers that are already there and so you aren't introducing new numbers that you have to check against all the time.

 

[ Edit ]

 

Something along the lines of this (I had to do in in VB.Net as I don't have anything other than VS 2005 on my windows box and I don't use it alot especially not for VB, so I don't know if/whether I can use normal old school VB6 on there, you can probably get the general idea though) -

 

Public Class Form1

   Private arrayOfLabels(9) As Label

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       arrayOfLabels(0) = Label1
       arrayOfLabels(1) = Label2
       arrayOfLabels(2) = Label3
       arrayOfLabels(3) = Label4
       arrayOfLabels(4) = Label5
       arrayOfLabels(5) = Label6
       arrayOfLabels(6) = Label7
       arrayOfLabels(7) = Label8
       arrayOfLabels(8) = Label9
   End Sub

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim randGen As New Random()
       Dim i As Integer, swapA As Integer, swapB As Integer
       For i = 1 To 100
           swapA = randGen.Next(9)
           swapB = randGen.Next(9)
           swap(swapA, swapB)
       Next
   End Sub

   Private Sub swap(ByVal swapA As Integer, ByVal swapB As Integer)
       Dim strTemp As String
       strTemp = arrayOfLabels(swapA).Text
       arrayOfLabels(swapA).Text = arrayOfLabels(swapB).Text
       arrayOfLabels(swapB).Text = strTemp
   End Sub
End Class

 

The way it uses the Random class/object isn't ideal as it should really be passing a better seed into it (it just uses the current time in microseconds or something atm I think which doesn't always work out if you are generating alot of numbers very quickly as the time might not have moved over or the seeds will be very close together etc) but I'm sure the Randomise code in VB6 that 5614 provided would be more use and could be dropped in. The way in which it uses the random numbers to rearrange the values as well could certainly be improved as doing a reasonably large amount of random swaps probably isn't the best way but you get the general idea.

Link to comment
Share on other sites

But, this means that 1 must be in box 1 or box 2.
That is true. If that is such a problem then you should use method 1.

 

1) You use a similar command for lbl2 and lbl3 however you will need to include If functions to ensure that no two labels contain the same captions

 

Dim num1 As Integer
Dim num2 As Integer

Private Sub Command1_Click()
   Randomize
   num1 = Int((9 * Rnd) + 1)
   lbl1 = num1

   Randomize
   num2 = Int((9 * Rnd) + 1)
   If num2 = num1 Then
       Do
       Randomize
       num2 = Int((9 * Rnd) + 1)
       Loop Until num2 <> num1
   Else
   End If

   lbl2 = num2

End Sub

I only just realised that I needed the Do...Loop Until command in there. Otherwise you could have num1=num2.

 

Also I appreciate you don't need the line spacing or the Else... I just put that all in there for clarity.

 

Again this code will get very length as you get to the 8th and 9th label.

Link to comment
Share on other sites

That looks pretty good to me. A minor correction: You can't set a variable equal to a label anymore (I think since VB6?), but if you just append the ".text" method to it then it's fine.

 

Like this:

 

lbl1.text = num1

 

(Assuming that "lbl1" is your label's name.)

 

Also "Randomize" and "RND" get an empty paramterization space "()" after them, but intellisense in VB7+ will append that automatically. (Skipping the .text, however, would generate an error that might be confusing to a newcomer.)

 

I just tested this whole program in VB7.1 and it runs fine. Good call, 5614.

Link to comment
Share on other sites

I dont know what the code is, but try using two arrays like this:

 

- One array holds values 1 through 9, the other array is empty.

- Create a for loop that selects RND * Ubound(Array1) and inserts its value into Array2. Delete the selected index, move everything up to fill the gaps (you might be able to do this easier by making Array1 into a dictionary object).

- The process above repeats itself until Array1 is empty and Array2 is a shuffled set of unique numbers.

 

The advantage above is that you only iterate through your loop 9 times. Otherwise, you trap yourself having a do loop with a nested for loop (this do loop creates a random number, the for loop goes through all the indexes to make sure its unique).

 

Also, you shouldnt use 9 labels call lbl1, lbl2, lbl3, etc. Create a control array instead. :)

Link to comment
Share on other sites

Pangloss: the code ran fine in VB 6.0 which is where I learnt, coded and tested that code!

 

But thanks for pointing it out.

 

But one thing I'd like to point out about your point out(!) is that for labels you have a caption, not text. So it could be lbl1.caption unless that has changed in VB7 as well!

 

The code would get quite long, if you think about it, for like the 9th box. Because you'd have to do If commands for all of the 8 other labels.

Link to comment
Share on other sites

Yeah that does change in VB7 (labels change to .text instead of .caption) (or .value for HTML labels in ASP.NET). Annoying, I know, especially since that particular trait is not backwards compatible. Thanks for clarifying where the change occurred, though -- that kind of tidbit is useful to me in my work.

 

I think it's something that most users would probably figure out on their own, but since this was clearly a learning thread I thought it might be worth mentioning. I teach beginning VB/ASP and it never ceases to amaze me what people can get hung up on sometimes, especially when they get tired and/or frustrated. (grin)

 

As a side note, I think as we go forward we're going to find that most "learners" are going to be using VB8 (Visual Studio 2005). It's a free download, after all. Unfortunately VBA in Office is still using the old VB6 IDE. (Even with the Office 2007 beta!) So people who come up through the VBA route (like Excel gurus) get a mixed message on some of these details.

Link to comment
Share on other sites

- One array holds values 1 through 9' date=' the other array is empty.

- Create a for loop that selects RND * Ubound(Array1) and inserts its value into Array2. Delete the selected index, move everything up to fill the gaps (you might be able to do this easier by making Array1 into a dictionary object).

- The process above repeats itself until Array1 is empty and Array2 is a shuffled set of unique numbers.

[/quote']

 

You don't use two arrays, you only use one. Just shuffle the numbers within the array. You do this by iterating through each element in the array and swapping it with another element picked at random.

 

Pardon my pseudocode. It'll need polishing.

 

for i= 1 to 9

..do while randomelement = i

....'all this does is ensure that

....'you're not swapping an element with itself,

....'which is useless

....randomelement = rnd()*9+1

..end loop

..temp = array(i) 'to swap two numbers, you need a temporary holder

..array(i) = array(randomelement)

..array(randomelement) = temp

next

 

Starting config:

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

After one iteration:

1 2 3 4 5 6 7 8 9

7 2 3 4 5 6 1 8 9

After two iterations:

1 2 3 4 5 6 7 8 9

7 4 3 2 5 6 1 8 9

etc.

Link to comment
Share on other sites

You don't use two arrays' date=' you only use one. Just shuffle the numbers [i']within[/i] the array. You do this by iterating through each element in the array and swapping it with another element picked at random.

 

Pardon my pseudocode. It'll need polishing.

 

for i= 1 to 9

..do while randomelement = i

....'all this does is ensure that

....'you're not swapping an element with itself,

....'which is useless

....randomelement = rnd()*9+1

..end loop

..temp = array(i) 'to swap two numbers, you need a temporary holder

..array(i) = array(randomelement)

..array(randomelement) = temp

next

 

Starting config:

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

After one iteration:

1 2 3 4 5 6 7 8 9

7 2 3 4 5 6 1 8 9

After two iterations:

1 2 3 4 5 6 7 8 9

7 4 3 2 5 6 1 8 9

etc.

 

That's what I suggested but rather than having an array of numbers, I just kept an array of the labels and swapped their text/values around :P

Link to comment
Share on other sites

You don't use two arrays' date=' you only use one. Just shuffle the numbers [i']within[/i] the array. You do this by iterating through each element in the array and swapping it with another element picked at random.

 

Pardon my pseudocode. It'll need polishing.

 

for i= 1 to 9

..do while randomelement = i

....'all this does is ensure that

....'you're not swapping an element with itself,

....'which is useless

....randomelement = rnd()*9+1

..end loop

..temp = array(i) 'to swap two numbers, you need a temporary holder

..array(i) = array(randomelement)

..array(randomelement) = temp

next

 

Starting config:

1 2 3 4 5 6 7 8 9

1 2 3 4 5 6 7 8 9

After one iteration:

1 2 3 4 5 6 7 8 9

7 2 3 4 5 6 1 8 9

After two iterations:

1 2 3 4 5 6 7 8 9

7 4 3 2 5 6 1 8 9

etc.

 

 

You probably dont even need to use a temporary array or even a do loop:

 

myArray(9) = 1, 2, 3, 4, 5, 6, 7, 8, 9

For I = 1 To 9
   RandomIndex = (Rnd * 8) + 1
   'rnd * 8 chooses between random numbers between 0 through 8. The + 1 bumps
   'the choice to 1 through 9.

   If I <> RandomIndex Then
       myArray(I) = myArray(I) Xor myArray(RandomIndex)
       myArray(RandomIndex) = myArray(I) Xor myArray(RandomIndex)
       myArray(I) = myArray(I) Xor myArray(RandomIndex)
   End If
Next

Using the xor operator 3 times like that switches two numbers without having to use a temporary variable.

 

I win this thread :)

 

Thanks Aeternus for improving the pseudocode *huggles*

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.