Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects
Referencing dynamically created objects Referencing dynamically created objects
Referencing dynamically created objects
Go Back  Xtreme Visual Basic Talk > > > Referencing dynamically created objects


Reply
 
Thread Tools Display Modes
  #1  
Old 04-26-2011, 02:54 AM
Adri17's Avatar
Adri17 Adri17 is offline
Newcomer
 
Join Date: Apr 2009
Posts: 17
Question Referencing dynamically created objects


All,

I dynamically (by programming text, not drawing in GUI) created a matrix of 10x10 buttons named 'Button1' till 'Button100'. In a formula I calculate a number, say '87'. If I next want to reference 'Button87' and change e.g. the background clolor and text properties, how would I do that?

Many thanks for your help!
I am creating a PocketPC game using VB2005.
Cheers,

Adri
Reply With Quote
  #2  
Old 04-26-2011, 09:20 AM
AtmaWeapon's Avatar
AtmaWeaponReferencing dynamically created objects AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 04-26-2011, 02:32 PM
Adri17's Avatar
Adri17 Adri17 is offline
Newcomer
 
Join Date: Apr 2009
Posts: 17
Default Referencing dynamically created objects - reaction

AtmaWeapon,

I did read the article you have referencd, but I do not want to 'add buttons to the control array at runtime', afterwards. And the index, location and other properties of the already created buttons are basically known to me.
What I do want is to 'select' the button (and next modify the properties), based on a calculated number, e.g. '87' --> 'Button87' out of a set of dynamically created 100 buttons.
Any idea about that?

Adri
Reply With Quote
  #4  
Old 04-28-2011, 07:17 AM
Qua's Avatar
QuaReferencing dynamically created objects Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Quote:
Originally Posted by Adri17 View Post
AtmaWeapon,

I did read the article you have referencd, but I do not want to 'add buttons to the control array at runtime', afterwards. And the index, location and other properties of the already created buttons are basically known to me.
What I do want is to 'select' the button (and next modify the properties), based on a calculated number, e.g. '87' --> 'Button87' out of a set of dynamically created 100 buttons.
Any idea about that?

Adri
Yes - Use an array/list of controls But no, really. You should go with Atma's suggestion.

If you, die hard, refuse to then you could use the Find method to find your control by name.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #5  
Old 04-28-2011, 09:41 AM
AtmaWeapon's Avatar
AtmaWeaponReferencing dynamically created objects AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Programming is an exercise for the brain, and I am the kind of teacher that prefers to put you close to the solution and have you figure the rest out for yourself.

Your problem: you've got a bunch of buttons created at runtime. You want to be able to calculate a number like 87 and then do something to the button with name "button87". I showed you a solution that lets you access a button via a number. What more could you ask for?

A control array lets you access many controls by number. Suppose you had "Button1" through "Button100". If you created them in an array, you'd instead have "buttons(0)" through "buttons(99)". Now if you calculate 87, you know the 87th button is at index 86 in the array, or "button(86)" is the same as "button87".

The control array would also let you avoid all the effort of typing 100 variables, instantiating them, and hand-calculating their position.

Let's make a comparison with a 2x2 grid.
Code:
Private Button1 As Button
...
Private Button4 As Button

Public Sub CreateButtons()
    Button1 = New Button()
    Button1.Name = "Button1"
    Button1.Size = New Size(32, 32)
    Button1.Text = "1"
    Button1.Location = New Point(10, 10)
    Me.Controls.Add(Button1)

    Button2 = New Button()
    ...
End Sub
This leaves you with no easy way to get the button you want. Qua's suggestion of the Find() method will work, but the more controls you have the slower it will go. Let's say you want to set the background color of button 3 to Blue. The most proper way to do so would be this:
Code:
Sub SetButtonToBlue(ByVal buttonNumber As Integer)
    Dim buttonName As String = String.Format("Button{0}", buttonNumber)
    Dim ctrls() As Control = Me.Find(buttonName, True)
    If ctrls.Length > 0 Then
        Dim btn As Button = DirectCast(ctrls(0), btn)
        btn.BackColor = Color.Blue
    End If
End Sub
What if we used arrays? You only need the array variable, and you only need to write the code to create the button once:
Code:
Private _buttons(3) As Button

Public Sub CreateButtons()
    Dim buttonIndex As Integer = 0
    For row As Integer = 0 To 2
        For column As Integer = 0 To 2
            Dim btn As New Button()
            btn.Name = String.Format("Button{0}", buttonIndex + 1)
            btn.Size = New Size(32, 32)
            btn.Text = (buttonIndex + 1).ToString()
            btn.Location = New Point(10 * row, 10 * column)
            Me.Controls.Add(btn)

            buttonIndex += 1
        Next
    Next
End Sub
Now you can directly use the index of the button to set its color to blue:
Code:
Sub SetButtonToBlue(ByVal number As Integer)
    _buttons(number - 1).BackColor = Blue
End Sub
See if you can't figure out how to tweak that to make a 10x10 grid. You should *never* type out 100 variables by hand. If you've got more than 2 or 3, you should almost always use an array.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 04-30-2011, 09:57 AM
Adri17's Avatar
Adri17 Adri17 is offline
Newcomer
 
Join Date: Apr 2009
Posts: 17
Unhappy Referencing dynamically created objects - reaction2

AtmaWeapon,

Be assured: I indeed do not manually create 100 buttons but use a loop with 'row' and 'columns' as in your example.
I ran your last example but it does not work: I get a "NullReferenceException" error... I can image, since the "_buttons(3)" is no-where used or populated with values but next called for in the routine 'SetButtonToBlue'. Is there an assignment missing in the routine "CreateButtons()"? Am I still missing something or doing something wrong?

I, by the way, add also a prefix 'Color.' to the 'Blue' to make this work, as you can see in atached screendump.

Last edited by Adri17; 03-22-2017 at 11:03 AM.
Reply With Quote
  #7  
Old 04-30-2011, 02:57 PM
AtmaWeapon's Avatar
AtmaWeaponReferencing dynamically created objects AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Yes indeed, it looks like I left out an assignment in there. That's all the information you need to fix it.

Sometimes, examples are typed in text editors outside of Visual Studio. If the syntax is completely wrong, I usually go back and fix the errors and apologize. In this case, I don't think it's worth correcting since you just explained how to fix it; obviously the omissions are clear.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply

Tags
aardvarks, dynamically, objects, referencing, vague tags


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects Referencing dynamically created objects
Referencing dynamically created objects
Referencing dynamically created objects
 
Referencing dynamically created objects
Referencing dynamically created objects
 
-->