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.GetContainer Control.Ac tiveContro l.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.Active Control.Ta g 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??
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.GetContainer
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.Active
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??
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.Pictu reBox
' 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.Pictu reBox
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.GetContainer Control.Ac tiveContro l.Tag)
End Sub
--------------------------
'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.Pictu
' 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.Pictu
Me.Controls.Add(myPB)
myPB.Parent = Panel1
myPB.BackgroundImage = Image.FromFile("E:\images\
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.GetContainer
End Sub
Change MsgBox(Panel1.GetContainer Control.Ac tiveContro l.Tag)
to MsgBox(Panel1.GetContainer Control.Ac tiveContro l.Text)
and see what you get. Hopefully, r & ',' & c
I suspect that you might not be able to set that at runtime.
etrain01
to MsgBox(Panel1.GetContainer
and see what you get. Hopefully, r & ',' & c
I suspect that you might not be able to set that at runtime.
etrain01
ASKER
Now if I change everything from PictureBox to Textbox, then this exact code WORKS!
'Change these lines:
Public myPB As System.Windows.Forms.Pictu reBox --> Public myPB As System.Windows.Forms.TextB ox
myPB = New System.Windows.Forms.Pictu reBox --> myPB = New System.Windows.Forms.Textb ox
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.
'Change these lines:
Public myPB As System.Windows.Forms.Pictu
myPB = New System.Windows.Forms.Pictu
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
etrain01
ASKER
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.NullReferenceExcep tion' 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???
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.NullReferenceExcep
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???
ASKER
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.Activ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The reason GetContainerControl.Active Control 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.
ASKER
Idle Mind...
You are a FRIGGIN' Genius!
Enjoy the 500 POINTS. This was driving me Nucking Futz!
:)
-Anthony
You are a FRIGGIN' Genius!
Enjoy the 500 POINTS. This was driving me Nucking Futz!
:)
-Anthony
etrain01