VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Variable ObjectName ? Posted by PoRrEkE on 15 Nov 2003 at 4:30 PM
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 ..


Report
Re: Variable ObjectName ? Posted by Psightoplazm on 16 Nov 2003 at 8:39 PM
Why don't you make your textboxes into a control array?
></\/~Psightoplasm`~

Report
Re: Variable ObjectName ? Posted by PoRrEkE on 17 Nov 2003 at 5:21 PM
: Why don't you make your textboxes into a control array?
: ></\/~Psightoplasm`~
:
:


what do you mean with a "control array" ? i can't find any data on it in the help of VS.Net.

the thing I do now is use an array which I fill with a loop with all the possible names of the pictureboxes.

it's an array with 3 dimentions and it works fine, i've tested it with throwing data from it into a textbox and there is no problem there.

Private Sub VulPictureArrays()
Dim iRij As Integer 'iRow
Dim iKolom As Integer 'iColumn
Dim iNummer As Integer = 1 'iNumber
Dim iPoort As Integer 'iPort
Do While iPoort < 3
Do While iKolom < 8
Do While iRij < 5
aPicturesLPT(iKolom, iRij, iPoort) = "PictureBox" + CStr(iNummer)
iRij += 1
iNummer += 2
Loop
iRij = 0
iKolom += 1
Loop
iKolom = 0
iPoort += 1
Loop

End Sub


However, the data i get from that array are all strings and I can't change the .Visible property of a string ... :/
Report
Re: Variable ObjectName ? Posted by iwilld0it on 17 Nov 2003 at 9:26 PM
: 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 :)
Report
Re: Variable ObjectName ? Posted by Psightoplazm on 17 Nov 2003 at 11:20 PM
That's a really good solution. I am learning more about .NET everyday :) thanks
></\/~Psightoplasm`~

Report
Re: Variable ObjectName ? Posted by PoRrEkE on 18 Nov 2003 at 3:24 AM
that looks very clever indeed, but is not at all what i was looking for

i don't need to set visible=false of 120 pictureboxes at 1 time.

i need to be able to change visibility from each one separately at any random moment .. u c ?

but i think i got the sln via an other forum ;)

i'll let ya know
Report
Re: Variable ObjectName ? Posted by iwilld0it on 18 Nov 2003 at 6:28 AM
: that looks very clever indeed, but is not at all what i was looking for
:
: i don't need to set visible=false of 120 pictureboxes at 1 time.
:
: i need to be able to change visibility from each one separately at any random moment .. u c ?
:
: but i think i got the sln via an other forum ;)
:
: i'll let ya know
:

If you need to randomly need to set visibility of each picture-box then ...

Dim ctrl As Control

For Each ctrl In Me.Controls
   If TypeOf ctrl Is PictureBox Then
      Call System.Threading.Thread.Sleep(2)
      Dim rnd As New Random(System.Environment.TickCount)
      Dim flag As Integer = rnd.Next(0, 1)
      ctrl.Visible = CBool(flag)
   End If
Next




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.