rndom numbers selection and display in VB2005

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 1672 Views - Last Post: 08 May 2007 - 12:12 PM Rate Topic: -----

#1 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

rndom numbers selection and display in VB2005

Posted 03 May 2007 - 02:13 PM

Hi there,

I'd like to submit this program I'm working on to thr forum.
I want to display 6 randomly generated numbers on 6 command buttons as captions at the click of another button. One number will be selected only once.
Here's my code:
Public Class Form1
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim x As Integer() = New Integer() {x(0), x(1), x(2), x(3), x(4), x(5)}
		Dim m_blnUsed As Boolean() = _
		New Boolean(x.GetUpperBound(0)) {}

		Dim objrandom As Random = New Random()
		Dim intrandom As Integer
		Do
			intrandom = objrandom.Next(0, m_blnUsed.Length)
		Loop Until m_blnUsed(intrandom) = False
		m_blnUsed(intrandom) = True

		Dim index As Integer
		For index = 0 To 5
			x(index) = x(intrandom)
		Next

		Button3.Text = Str(x(0))
		Button4.Text = Str(x(1))
		Button5.Text = Str(x(2))
		Button6.Text = Str(x(3))
		Button7.Text = Str(x(4))
		Button8.Text = Str(x(5))

	End Sub
	
End Class


I'm getting an error message

Quote

variable 'x' is used before it has been assigned a value. A null reference exception could result at runtime.
at the beginning where I'm declaring and initializing the array of integers x(0)

Can anyone help please.
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: rndom numbers selection and display in VB2005

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: rndom numbers selection and display in VB2005

Posted 03 May 2007 - 03:00 PM

Remove the x and the parenthesis from inside you array declaration.

Should be like this:
Dim x As Integer() = New Integer() {0, 1, 2, 3, 4, 5}

Was This Post Helpful? 0
  • +
  • -

#3 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 04 May 2007 - 09:31 AM

here's the modified code
Public Class Form1


	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim x As Integer() = New Integer(6) {}

		Dim randomgeberator As New Random
		Dim index As Integer
		For index = 0 To 5
			x(index) = randomgeberator.Next(0, 20)
		Next

		Button3.Text = Str(x(0))


		Button4.Text = Str(x(1))
		Button5.Text = Str(x(2))
		Button6.Text = Str(x(3))
		Button7.Text = Str(x(4))
		Button8.Text = Str(x(5))

	End Sub

End Class



i'm having this at runtime on the immediate window

Quote

A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe

now i'm being able to select randomly 6 numbers and display them in the buttons, the bit i can't achieve is to select one number once only which i was trying to do with the boolean array but i'm having trouble to set it up
any help will be so much appreciated
thanks
Was This Post Helpful? 0
  • +
  • -

#4 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 04 May 2007 - 12:19 PM

I've been able to achieve what i wanted with the code
Dim x As Integer() = New Integer(10) {}

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

		Dim m_blnUsed As Boolean() = _
		New Boolean(x.GetUpperBound(0)) {}


		Dim objrandom As Random = New Random()
		Dim intrandom As Integer
		Dim index As Integer
		Do
			intrandom = objrandom.Next(0, m_blnUsed.Length)
			
		Loop Until m_blnUsed(intrandom) = False
		m_blnUsed(intrandom) = True


		For index = 0 To 5
			x(index) = intrandom
		Next

		Button3.Text = Str(x(0))

		Do
			intrandom = objrandom.Next(0, m_blnUsed.Length)

		Loop Until m_blnUsed(intrandom) = False
		m_blnUsed(intrandom) = True


		For index = 0 To 5
			x(index) = intrandom
		Next
		Button4.Text = Str(x(1))
		Do
			intrandom = objrandom.Next(0, m_blnUsed.Length)

		Loop Until m_blnUsed(intrandom) = False
		m_blnUsed(intrandom) = True


		For index = 0 To 5
			x(index) = intrandom
		Next
		Button5.Text = Str(x(2))
		Do
			intrandom = objrandom.Next(0, m_blnUsed.Length)

		Loop Until m_blnUsed(intrandom) = False
		m_blnUsed(intrandom) = True


		For index = 0 To 5
			x(index) = intrandom
		Next
		Button6.Text = Str(x(3))
		Do
			intrandom = objrandom.Next(0, m_blnUsed.Length)

		Loop Until m_blnUsed(intrandom) = False
		m_blnUsed(intrandom) = True


		For index = 0 To 5
			x(index) = intrandom
		Next
		Button7.Text = Str(x(4))
		Do
			intrandom = objrandom.Next(0, m_blnUsed.Length)

		Loop Until m_blnUsed(intrandom) = False
		m_blnUsed(intrandom) = True


		For index = 0 To 5
			x(index) = intrandom
		Next
		Button8.Text = Str(x(5))

	End Sub

End Class

though at run time i'm still getting on immediate window

Quote

A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe


i'm more concern about the next stage which i don't have any clue, even how to start
what i want is the first of the buttons containing the selected numbers clicked, the number will be copied in the first of a 6 set of labels and so on.
to be more explicit, the 6 buttons will be clicked one at a time and their content copied in the 6 labels as going; the 1st button to be clicked to the 1st label, so on
Was This Post Helpful? 0
  • +
  • -

#5 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 04 May 2007 - 03:00 PM

i've been thinking (may be wrongly & sorry about that incase) there might be a way to set up an array of captions and assign them to the labels and i've been trying hard, but again i have no clue
any help pleasethis what i've been trying to do
Dim y As CaptionButton() = New CaptionButton() {}

label1.text="y(0)"   label2.text=y(1)
and i'm getting the error message

Quote

Declaration expected
at both the last 2 lines of the code (where i'm trying to assign the captions to the labels

This post has been edited by Modibbo: 04 May 2007 - 03:04 PM

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: rndom numbers selection and display in VB2005

Posted 04 May 2007 - 04:33 PM

Not sure why you are trying to set them up as Captionbuttons. You cannot use CaptionButtons in the manner that you are trying to use them.

MSDN CaptionButtons.
Caption buttons are what you find in the upper right corner of your form.

Close
Minimize
Maximize
Help
Restore

Just create an array of type String and assign the contents to each of your labels as needed.
Was This Post Helpful? 0
  • +
  • -

#7 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 05 May 2007 - 06:47 AM

this captionbutton storie shows i don't have any clue to what i'm doing, i've been programming only 2 months by the way
anyway thanks a lot once more again for all your help, i've been trying your suggestion with the code
 Dim y As String() = New String() {}

Label1.Text="y(0)"


and getting the error message

Quote

Declaration expected
with a wigly line on Label1
Was This Post Helpful? 0
  • +
  • -

#8 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 05 May 2007 - 11:28 AM

guys, how would i go about choosing between the buttons, for the 1st clicked store its content in the 1st label, the 2d button to the 2nd label, so on.
my problem is th choose between the first button which is clicked , whichever is this first, it can be the 3rd button in the line to be clicked first, or the last one in the line, it doesn't matter, the 1st clicked, its value assigned to 1st label, and so on

This post has been edited by Modibbo: 05 May 2007 - 11:29 AM

Was This Post Helpful? 0
  • +
  • -

#9 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: rndom numbers selection and display in VB2005

Posted 05 May 2007 - 12:58 PM

Simply put, you need to check which label has a value stored in it already, in order, from the first label to the last label. You will need to perform this evaluation each time a button is clicked. I would suggest creating a separate procedure/function to do the checking and just call it from inside the click event of each button.

As for your declaration error, not sure why you are getting that error.
Was This Post Helpful? 0
  • +
  • -

#10 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 05 May 2007 - 01:35 PM

View Postjayman9, on 5 May, 2007 - 12:58 PM, said:

Simply put, you need to check which label has a value stored in it already, in order, from the first label to the last label. You will need to perform this evaluation each time a button is clicked. I would suggest creating a separate procedure/function to do the checking and just call it from inside the click event of each button.

As for your declaration error, not sure why you are getting that error.

hands up jayman9 for the declaration error, i've just realised what i did was starting a new line after ending the precedind sub, so VB was telling me hang on you did not declare the new sub you're starting; a proof of how confused i am with this program, still struggling though with it, gonna get there, never give up
in coding terms what does it mean this evaluation; sorry again just remind you i've been programming just 2 months
Was This Post Helpful? 0
  • +
  • -

#11 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 05 May 2007 - 02:04 PM

to be more explicit, i've got 6 buttons with number as caption and 6 labels and i want the number in the button which is clicked the fisrt (no matter which one is this one, be it the second or the fourth in the line) to be stored in the first label, so on
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: rndom numbers selection and display in VB2005

Posted 06 May 2007 - 01:12 AM

Lets start with first how to handle multiple click events from different buttons inside one click event. If you will notice in the following code I have taken the Button1 click event and added multiple buttons after the keyword Handles, this holds true for any controls that are of the same type. This event will now handle Button1, Button2, and Button3 click event.
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
		Handles Button1.Click, Button2.Click, Button3.Click

Inside button click events you will see two arguments that are passed to the event when it executes. The first is sender and the second is e. For this example all you need to be concerned with is sender.

When a button is clicked, sender contains the following information:

Quote

System.Windows.Forms.Button, Text: Button1

It contains the type of control and the name of the control. All you need based on what you are trying to do is the name of the button.

To get that information you will need to extract it from sender. First thing to do is get the index number of where you need to start extracting the information you want. The colon makes a good starting point. Using the IndexOf method, you can find what position in the string the colon is located at, notice I converted it to a string by using the ToString method first. Then add 2 to the Integer that is returned from the IndexOf method, since we just want the name only. So you want to advance the index from the colon to the start of the name, which is 2 characters.
		'get the index number of where to find the name of the button
		Dim index As Integer = sender.ToString.IndexOf(":") + 2

Now that you have the index number of where the name begins, you can extract the name by using the Substring method. Substring takes as a parameter the starting point or index. Now btnName will contain the name of the button clicked.
		'extract the name of the button from the sender object
		Dim btnName As Object = sender.ToString.Substring(index)


Now that you have the name, you just need to check and see which label has no value inside it yet. This is done using a series of If/ElseIf statements.
		'If the label is empty then load the name of the button pressed
		If Label1.Text = "" Then
			'load the name from btnName into the Text property of the Label
			Label1.Text = btnName
		ElseIf Label2.Text = "" Then
			'load the name from btnName into the Text property of the Label
			Label2.Text = btnName
		ElseIf Label3.Text = "" Then
			'load the name from btnName into the Text property of the Label
			Label3.Text = btnName
		End If


Now it doesn't matter which button is clicked first, it will start checking from the first label and adding the name of the button that was clicked. The complete code for the button click events that I used in this example now looks like this:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
		Handles Button1.Click, Button2.Click, Button3.Click

		'get the index number of where to find the name of the button
		Dim index As Integer = sender.ToString.IndexOf(":") + 2
		'extract the name of the button from the sender object
		Dim btnName As Object = sender.ToString.Substring(index)

		'If the label is empty then load the name of the button pressed
		If Label1.Text = "" Then
			'load the name from btnName into the Text property of the Label
			Label1.Text = btnName
		ElseIf Label2.Text = "" Then
			'load the name from btnName into the Text property of the Label
			Label2.Text = btnName
		ElseIf Label3.Text = "" Then
			'load the name from btnName into the Text property of the Label
			Label3.Text = btnName
		End If

	End Sub


That should get you going. You have 6 buttons and 6 labels so you will need to add those buttons in a similar manner as I have shown you.
Was This Post Helpful? 0
  • +
  • -

#13 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 06 May 2007 - 08:00 AM

i'll not thank you enough, jayman9, i'm enjoying it, it worked perfectly

now i want to sort the Nos and display the sorted values in the buttons (from 2 to 6) in order to arrage them in ascending order
here's my code
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		Dim NoOfElements As Integer, I As Integer, J As Integer, Temp As Integer
		NoOfElements = 6
		For I = 1 To (NoOfElements - 1)
			For J = I To (NoOfElements - 1)
				If x(I - 1) > x(J) Then
					Temp = x(I - 1)
					x(I - 1) = x(J)
					x(J) = Temp
				End If
			Next J
		Next I
		Button3.Text = x(0)

		Button4.Text = x(1)

		Button5.Text = x(2)

		Button6.Text = x(3)

		Button7.Text = x(4)

		Button8.Text = x(5)

it is displaying the No in button8 (the last one) in all of the buttons
Was This Post Helpful? 0
  • +
  • -

#14 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: rndom numbers selection and display in VB2005

Posted 06 May 2007 - 11:06 AM

I'm gonna need a little more clarification.

Do you want to sort all of the numbers in the array or just some of them?

Can you describe which numbers you are using in your array and of which of those is the one you say is displayed in button8 and the rest of the buttons?

Perhaps give me an example, step by step, of what is happening. Including the input and output when you run your project.
Was This Post Helpful? 0
  • +
  • -

#15 Modibbo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 29-April 07

Re: rndom numbers selection and display in VB2005

Posted 06 May 2007 - 01:00 PM

View Postjayman9, on 6 May, 2007 - 11:06 AM, said:

I'm gonna need a little more clarification.

Do you want to sort all of the numbers in the array or just some of them?

Can you describe which numbers you are using in your array and of which of those is the one you say is displayed in button8 and the rest of the buttons?

Perhaps give me an example, step by step, of what is happening. Including the input and output when you run your project.


ok

i want to sort all the 6 numbers which were randomly chosen and displayed in the 6 buttons (button3 to button8) at the click of a button (button1) named set.

this is what is happening step by step:
-at the click of a button (button1) named set, 6 numbers are displayed in the the 6 buttons (button3 to button8)
-at the click of a button (button2) named check, the number contained in the last button on the line (button8) is displayed in all the 6 buttons
which is not what i want, i want the numbers to be displayed in ascending order

This post has been edited by Modibbo: 06 May 2007 - 01:01 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2