adding proper number of pictureboxes to array control [VB 2008]

how to do that and how add exactly pb-s to array

Page 1 of 1

4 Replies - 4707 Views - Last Post: 13 August 2009 - 01:45 AM Rate Topic: -----

#1 AleXBoy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-August 09

adding proper number of pictureboxes to array control [VB 2008]

Posted 10 August 2009 - 04:40 AM

My application has no static number of pictureboxes.

I want to add picturebox to array control.

So, I do this:
Public pb(25) As PictureBox
after that, somewhere, I put this
 pb(1) = PictureBox1
		pb(2) = PictureBox2
		pb(3) = PictureBox3
		pb(4) = PictureBox4
		pb(5) = PictureBox5
		pb(6) = PictureBox6
		pb(7) = PictureBox7
		pb(8) = PictureBox8
		pb(9) = PictureBox9
		pb(10) = PictureBox10
		pb(11) = PictureBox11
		pb(12) = PictureBox12
		pb(13) = PictureBox13
		pb(14) = PictureBox14
		pb(15) = PictureBox15
		pb(16) = PictureBox16
		pb(17) = PictureBox17
		pb(18) = PictureBox18


You have noticed that I have to type it manually.
But, problem is that if I have only 6 pictureboxes, others from 7 to 18 will be underlined and application will not start.
Errors are: picturebox 7 is not declared, picturebox 8 is not declared...

How to fix this?

All that should have been done is to check how many pictureboxes are there (i did this
Dim kontrola As Control
		For Each kontrola In Me.Controls
			If TypeOf kontrola Is PictureBox Then
				b = Mid(kontrola.Name, 11)
				Exit For
			End If
		Next
, so b is number of existing pictureboxes) and to declare as many pictureboxes as there is.

How to do that?

Is This A Good Question/Topic? 0
  • +

Replies To: adding proper number of pictureboxes to array control [VB 2008]

#2 Asscotte   User is offline

  • D.I.C Addict
  • member icon

Reputation: 35
  • View blog
  • Posts: 610
  • Joined: 08-February 09

Re: adding proper number of pictureboxes to array control [VB 2008]

Posted 10 August 2009 - 04:56 AM

When VB says that something is not declared it means that it does not exist or does not exist with that name. you could dynamicly create the picture boxes if you needed to (saves code space :P)

Have a look here for some help on dynamicly creating something, the other options are to create more picture boxes or remove the excess code.
Was This Post Helpful? 0
  • +
  • -

#3 AleXBoy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-August 09

Re: adding proper number of pictureboxes to array control [VB 2008]

Posted 11 August 2009 - 02:34 PM

You haven't understood me.
All I want is to declare pictureboxes to arrays, so if there are 6 pictureboxes, picturebox1 will be array pb(1), picturebox2 will be array pb(2)...

How to do that?

By the way, I am making snake, so if snake detectes some picturebox in front of her way, she will stop move. Because of that, I can't do that what you recommended to me.
Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley   User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: adding proper number of pictureboxes to array control [VB 2008]

Posted 11 August 2009 - 02:44 PM

Counting Pictureboxes using
Dim b as Integer = 0
Dim kontrola As Control
		For Each kontrola In Me.Controls
			If TypeOf kontrola Is PictureBox Then
				b = b + 1
			End If
		Next



That will count the pictureboxes - all your code would do is get the number of the FIRST picture box it found in the controls array - could be any one of the pictureboxes.

Once you have the count, you can declare an exact sized array of pictureboxes..

An alternative is to define the array for the maximum number of pictureboxes and assign only those entries you are using with PictureBox1..25 being defined and those not used are set to Hide().

The collision test is if the picturebox is Visible() and positioned in front of the snake.

This post has been edited by mark.bottomley: 11 August 2009 - 02:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 AleXBoy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 10-August 09

Re: adding proper number of pictureboxes to array control [VB 2008]

Posted 13 August 2009 - 01:45 AM

OK. Can you declare as many pictureboxes as integer b is? Do it for me.

By the way, my snake will be smart. She will find the apple by her own. That is the difference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1