:
This message was edited by PoRrEkE at 2003-11-15 16:32:7
: Does anyone know if it is possible to eg. change the Visible property of an object. Knowing that this object is not each time the same. So a Private Sub would create a (string) name and the property of the object with that name should change it's visibility.
:
: If this would be possible, then I can make my code super much shorter.
:
: I tried something like:
:
: Dim iNumber as Integer = 1
:
: Private Sub Button1_Click etc..
: __Dim x as ?? (object ?? ...)
: __x = "PictureBox" + CStr(iNumber)
: __'x.Name = etc >> doesn't work either
: __x.Visible = false
: __iNumber +=1
: End Sub
:
:
: If this would work I could make 1 small Sub which can change visibility of my 120 PictureBoxes .. Otherwise I'll have to write a looooooooooooooooooooooong code ..
:
:
:
Your thinking too much out of the box. This is VB.NET the age of Inheritence for Visual Basic. Here is a possible solution:
Sub AllPictureBoxes(ByVal visible As Boolean)
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf ctrl Is PixtureBox Then
ctrl.Visible = visible
End If
Next
End Sub
...
Call AllPictureBoxes(True)
This assumes that your picture-boxes are all on a form. Now an even smarter thing to do is to place all your picture-boxes within the control collection of a panel control which in turn is in the control collection of the form. So if you make the panel invisible then all the picture-boxes will become invisible. This holds true for any container control. Setting a container controls visibility will affect all of it's child controls visibility and this works recursively.
Here is a demo i put together ...
Private Const PB_COUNT As Integer = 120
Private Const PB_WIDTH As Integer = 50
Private Const PB_HEIGHT As Integer = 50
Public Pnl As Panel
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create and add Panel
Pnl = New Panel
Me.WindowState = FormWindowState.Maximized
Me.Controls.Add(Pnl)
' Set panel dimensions to the form client Area
Pnl.Width = Me.ClientSize.Width
Pnl.Height = Me.ClientSize.Height
' Create and display picture boxes in grid for demo purposes
Dim i As Integer
Dim posX As Integer = 0
Dim posY As Integer = 0
For i = 1 To PB_COUNT
' Determine when to start another row
If posX >= Pnl.Width Then
posY += PB_HEIGHT
posX = 0
End If
' Init size and location for picture box
Dim sz As New Size(PB_WIDTH, PB_HEIGHT)
Dim lo As New Point(posX, posY)
' Add New Picture Box to Panel
Pnl.Controls.Add(CreatePicture(sz, lo))
posX += PB_WIDTH
Next
End Sub
' Creates A Picture Box
Public Function CreatePicture(ByVal sz As Size, ByVal p As Point) As PictureBox
CreatePicture = New PictureBox
With CreatePicture
.Size = sz
.Location = p
' Random Color
.BackColor = Color.FromArgb(ColorPart, ColorPart, ColorPart)
End With
End Function
' Generate Random Number Within RGB Color Range
Public Function ColorPart() As Integer
Dim rnd As New Random(System.Environment.TickCount)
Call System.Threading.Thread.Sleep(3)
Return rnd.Next(0, 255)
End Function
' This code makes all of the picture-Boxes invisible with one line
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Pnl.Visible = Not Pnl.Visible
End Sub
I actually was in one of those moods to play with some code. Hope this helps :)