11 Replies - 1363 Views - Last Post: 21 May 2013 - 10:12 AM Rate Topic: -----

#1 TheSurvivalInstinct   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 13

Parent issue for dynamically created control

Posted 20 May 2013 - 07:43 PM

First time posting... been lurking and finding answers, but haven't been able to solve this issue.

I have a panel of pictureboxes, that when clicked either change the image or create a new transparent picturebox on top of it to overlay another image. However, when setting the new picturebox's properties and I try to change the parent to sender, it only seems to work when clicking on the first picturebox in the panel and it doesn't respond to the handler set for it either.

            Dim decoration As New PictureBox
            decoration.Image = My.Resources.test1
            decoration.BackColor = Color.Transparent
            decoration.Height = decoration.Image.Height
            decoration.Width = decoration.Image.Width
            decoration.Left = sender.left
            decoration.Top = sender.top
            decoration.SizeMode = PictureBoxSizeMode.CenterImage
            decoration.Tag = "decoration"
            AddHandler decoration.Click, AddressOf decorationclick
            mappanel.Controls.Add(decoration)
            decoration.Parent = sender
            decoration.BringToFront()



If I remove the line where I'm trying to change the parent everything works fine and it creates the transparent picturebox over whichever picture box I click, but the transparent part of the picturebox shows the panel, which is why I want to set the parent to the sender.

Any ideas why it won't change the parent property, properly?

Is This A Good Question/Topic? 0
  • +

Replies To: Parent issue for dynamically created control

#2 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Parent issue for dynamically created control

Posted 20 May 2013 - 09:09 PM

According to the Microsoft Docs, .Parent is a read-only property.
Was This Post Helpful? 0
  • +
  • -

#3 TheSurvivalInstinct   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 13

Re: Parent issue for dynamically created control

Posted 20 May 2013 - 09:47 PM

I can add into the code

[code]
decoration.parent=map4_1
[code]

where map4_1 is a picturebox in the 4th row, 1st column (any of the pictureboxes work) and it will place the transparent image over that picturebox, but only if I click in the first picturebox in the panel.
Was This Post Helpful? 0
  • +
  • -

#4 TheSurvivalInstinct   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 13

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 08:37 AM

Whenever I add in the line to change the parent of the newly created picturebox it only gets handled by the first picturebox in the panel. It works perfectly for that one, showing it underneath the newly created transparent picturebox, but nothing else in the panel seems to respond. Any ideas why?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,539
  • Joined: 12-December 12

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 08:49 AM

Parent is not read-only(?). Control.Parent

Quote

Gets or sets the parent container of the control.

Apparently you need to change the Location as well; see this page. This may correct the click-handler as well.

This post has been edited by andrewsw: 21 May 2013 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#6 TheSurvivalInstinct   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 13

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 09:00 AM

It is changing the location by setting the Left and Top. If I take the line about setting the Parent property out of the code everything works perfectly. I can click on any of the boxes and it creates a transparent picturebox over the picturebox that I click. The only problem is that the newly created picturebox's Parent is the panel (called mappanel in the code) and the transparent part of the image in the new picturebox shows the panel instead of the picturebox I clicked on to create the new picturebox.

What I want to do is change the Parent property of the new picturebox to the one I clicked on. When I add in:
decoration.Parent = sender



It does create the transparent picturebox on top of the one I click, and the transparent parts show the picturebox I clicked instead of the panel, but it now only works with the first picturebox in the panel instead of any of them like before. I'm not sure why setting the Parent property for a dynamically created control causes all of the pictureboxes in the panel to ignore the event handler except the one at location 0,0.
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 09:32 AM

Can you cast sender to a picturebox and then add decoration to its controls collection.

Every test I've done I can't get it to fail so I'm just throwing that out there.

Combine the click handlers

Something like:

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click, PictureBox3.Click, PictureBox2.Click
        Dim MyTestPB As New PictureBox
        MyTestPB.BorderStyle = BorderStyle.FixedSingle
        MyTestPB.BackColor = Color.Red
        Dim thisPicturebox As PictureBox = CType(sender, PictureBox)
        thisPicturebox.Controls.Add(MyTestPB)

    End Sub

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,539
  • Joined: 12-December 12

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 09:33 AM

Quote

It is changing the location by setting the Left and Top.

I mean after changing the Parent. But I may be wrong.
Was This Post Helpful? 0
  • +
  • -

#9 TheSurvivalInstinct   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 13

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 09:45 AM

View PostCharlieMay, on 21 May 2013 - 09:32 AM, said:

    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click, PictureBox3.Click, PictureBox2.Click
        Dim MyTestPB As New PictureBox
        MyTestPB.BorderStyle = BorderStyle.FixedSingle
        MyTestPB.BackColor = Color.Red
        Dim thisPicturebox As PictureBox = CType(sender, PictureBox)
        thisPicturebox.Controls.Add(MyTestPB)

    End Sub

I've tried that and got the same issue. Perhaps it's how I'm combining the click handlers? I have:
 Private Sub ProcessClick(ByVal sender As Object, _
            ByVal e As System.EventArgs) _
            Handles map1_1.Click, map1_2.Click 'for forum purposes I won't post all 720

            Dim decoration As New PictureBox
            decoration.Image = My.Resources.stonepath_vertical
            decoration.BackColor = Color.Transparent
            decoration.Height = decoration.Image.Height
            decoration.Width = decoration.Image.Width
            decoration.Left = sender.left
            decoration.Top = sender.top
            decoration.SizeMode = PictureBoxSizeMode.CenterImage
            decoration.Tag = "decoration"
            AddHandler decoration.Click, AddressOf decorationclick
            Dim senderbox As PictureBox = CType(sender, PictureBox)
            senderbox.Controls.Add(decoration)
            decoration.BringToFront()
 End Sub



The only picturebox that seems to respond to clicking is whatever picturebox is in the top left of the panel. I can move any picturebox to that position and it will create the new picturebox over it, but it only works for that location. If I remove setting the parent and just add the new picturebox to the panel everything works except the transparency issue.
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1730
  • View blog
  • Posts: 5,709
  • Joined: 25-September 09

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 10:05 AM

Try Decoration.Left = 0
Decoration.Top = 0

You always want 0,0 in the new box.

Right now if your picturebox is at location 300,300, you're setting the new picturebox to 300,300 (inside the current picturebox)

Your second picturebox has the new box outside the visible range. That's why it only works in the first box.

This post has been edited by CharlieMay: 21 May 2013 - 10:08 AM

Was This Post Helpful? 1
  • +
  • -

#11 TheSurvivalInstinct   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 13

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 10:08 AM

Ha, wow. I knew it had to be something stupid and simple that I was missing. Thank you so much for all the help guys, I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw   User is offline

  • Stealth IT
  • member icon

Reputation: 6700
  • View blog
  • Posts: 27,539
  • Joined: 12-December 12

Re: Parent issue for dynamically created control

Posted 21 May 2013 - 10:12 AM

View PostCharlieMay, on 21 May 2013 - 05:05 PM, said:

Try Decoration.Left = 0
Decoration.Top = 0

You always want 0,0 in the new box.

Right now if your picturebox is at location 300,300, you're setting the new picturebox to 300,300 (inside the current picturebox)

Your second picturebox has the new box outside the visible range. That's why it only works in the first box.

He, he, this is what I was hinting at ;)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1