Link to home
Start Free TrialLog in
Avatar of afsanchez001
afsanchez001

asked on

:: URGENT :: Dynamically Created Controls at Run-Time. Cannot access thier Index or assigned value (.TAG) property when clicked.

I have a panel (Panel1) which serves as my container, which houses a collection of tiled pictureboxes.  These pictureboxes are Dynamically Created as controls during Run-Time.  I use a nested For...Next environment to accomplish the Row-Column Tiling effect.  THAT IS NOT THE PROBLEM.

The problem is during the For...Next iteration (when I build the Dynamic Controls) I use:

AddHandler myPB.Click, AddressOf myPB_Click    <<-- This creates a Click Event for the newly created Control.

'--------------------------------------------------------------------------------------------------------------------
    Private Sub myPB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(Panel1.GetContainerControl.ActiveControl.Tag)    <<-- Breaks here...
    End Sub
'--------------------------------------------------------------------------------------------------------------------

When I dynamically create TEXTBOXES the Click event gives me the (.TAG) information, but when I Dynamically create PICTUREBOXES (which is what I need to use) the GetContainerControl.ActiveControl.Tag RETURNS a NULL VALUE (Nothing) and breaks.

Any Ideas???

Is there a better way to detect which control I am clicking and successfully returning the Index or “assigned value” like in the .TAG property??
Avatar of etrain01
etrain01

Have you tried setting the TAG property when you create the PICTUREBOX.

etrain01
Avatar of afsanchez001

ASKER

Yes I do...  Here is the actual code.

--------------------------------------------------------------

'1. Create a blank form in VB.NET
'2. Place a panel on the form named Panel1
'3. Place this in your Declarations:  

                       Public myPB As System.Windows.Forms.PictureBox

' 4. Start the program using this:

                       loadPictureBoxArray(6, 30) 'rows, columns

'5. Place in form:

Public Sub loadPictureBoxArray(ByVal row As Int32, ByVal col As Int32)

        Dim r As Int32 = 0
        For row = 0 To row - 1

            Dim c As Int32 = 0
            For col = 0 To col - 1

                myPB = New System.Windows.Forms.PictureBox
                Me.Controls.Add(myPB)

                myPB.Parent = Panel1

                myPB.BackgroundImage = Image.FromFile("E:\images\40x40.bmp")
                myPB.Width = 40
                myPB.Height = 40

                myPB.Text = r & ", " & c
                myPB.Tag = "row: " & r & " col: " & c

                myPB.Top = myPB.Height * r
                myPB.Left = myPB.Width * c

                AddHandler myPB.Click, AddressOf myPB_Click

                c = c + 1
            Next

            r = r + 1
        Next

    End Sub

    Private Sub myPB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        MsgBox(Panel1.GetContainerControl.ActiveControl.Tag)

    End Sub
Change MsgBox(Panel1.GetContainerControl.ActiveControl.Tag)
to MsgBox(Panel1.GetContainerControl.ActiveControl.Text)
and see what you get.  Hopefully, r & ',' & c

I suspect that you might not be able to set that at runtime.

etrain01
Now if I change everything from PictureBox to Textbox, then this exact code WORKS!

'Change these lines:

Public myPB As System.Windows.Forms.PictureBox   -->   Public myPB As System.Windows.Forms.TextBox
myPB = New System.Windows.Forms.PictureBox      -->   myPB = New System.Windows.Forms.Textbox

The code will work...

Is there a way to get the .TAG from the picturebox?  The Textbox allows this but the pictureBox doesn't.

Thanks.
Also you could setup a break point in your program to see if it is actually setting the TAG property.  This will tell you if it will work or not.

etrain01
No, no, no...

the properties are not returning at all when I use dynamically create picturebox. TAG, TEXT nothing returns.  

This is what I get:

       An unhandled exception of type 'System.NullReferenceException' occurred in CellarMaster.exe
       Additional information: Object reference not set to an instance of an object.

But if I switch it to a TextBox then it works.  What is the big difference between the Dynamically created PictureBox and the Dynamically created TextBox???

Within the embedded "For ... Next" the tag does indeed set properly.  It always does.  I verified this.

That is not where the problem is...

The program is breaking when I use the "GetContainerControl.ActiveControl" for the PictureBox.  This only seems to work for the TextBoxes...
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The reason GetContainerControl.ActiveControl works for the TextBox and not the PictureBox is a TextBox can actually have the focus, while a PictureBox cannot.  When using the PictureBoxes, there is no control with the focus, ActiveControl is then returning Nothing causing your "Object reference not set to an instance of an object" error.
Idle Mind...

You are a FRIGGIN' Genius!

Enjoy the 500 POINTS.  This was driving me Nucking Futz!

:)

-Anthony