Results 1 to 7 of 7

Thread: PictureBox Inside a Panel That is Inside a TabControl TabPage

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    PictureBox Inside a Panel That is Inside a TabControl TabPage

    Hi,

    I created a PictureBox inside a Panel that is in a TabPage of a TabControl. The problem is that when I do the BringToFront() command the PictureBox will always stays in the front and wont hide when a different TabPage is selected. Here is the code I'm using to test this part of the program. Any help is greatly appreciated. I suspect that the problem has something to do with the Parent declaration highlighted in red below.

    Thanks,

    Robert

    Code:
    Public SquarePic As PictureBox
    
    SquarePic = New PictureBox
    SquarePic.Name = "SquareTesting1"
    
    SquarePic.Image = Image.FromFile(Application.StartupPath & "\images\BlackSquare.bmp")
    
    SquarePic.Parent = pnlSquarePanel
    
    SquarePic.Location = New Point(10,10)
    SquarePic.Visible = True
    Me.Controls.Add(SquarePic)
    SquarePic.BringToFront()

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: PictureBox Inside a Panel That is Inside a TabControl TabPage

    Code:
    Me.Controls.Add(SquarePic)
    This more or less does the same thing as setting Parent. When you do this, you remove the picture box from the panel and reparent it under the form.

    That said, most people don't muck with the Parent property as messing with the Controls collection is a little more intuitive. So replace the line that sets the Parent property with:
    Code:
    pnlSquarePanel.Controls.Add(SquarePic)
    ...and delete the line that adds it to the form.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Re: PictureBox Inside a Panel That is Inside a TabControl TabPage

    Hi, thanks for your explanation. Unfortunately, with the new code below the PictureBox doesn't show at all. With the previous code the PictureBox was showing on top of every TabPage. Now, it doesn't show at all.

    Code:
    Public SquarePic As PictureBox
    
    SquarePic = New PictureBox
    SquarePic.Name = "SquareTesting1"
    
    SquarePic.Image = Image.FromFile(Application.StartupPath & "\images\BlackSquare.bmp")
    
    pnlSquarePanel.Controls.Add(SquarePic)
    
    SquarePic.Location = New Point(10,10)
    SquarePic.Visible = True
    
    SquarePic.BringToFront()

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: PictureBox Inside a Panel That is Inside a TabControl TabPage

    Firstly, there should be no need to set the Visible property or call BringToFront. True is the default value for the Visible property and BringToFront can only be useful if the PictureBox overlaps other controls.

    As for not being able to see the PictureBox, if you're adding the PictureBox to the Panel and you can't see the PictureBox then that means that you can't see the Panel. I don't know how pnlSquarePanel was added or configured but if it's visible to the user then any control you Add to it will also be visible. You can prove that to yourself with a test project. I just create a Windows Forms app project, added a TabControl and a Button and then added a Panel to the second TabPage. I set the Dock property of the Panel to Fill and then added this code:
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     Dim pb As New PictureBox
    3.  
    4.     pb.BorderStyle = BorderStyle.FixedSingle
    5.  
    6.     Panel1.Controls.Add(pb)
    7. End Sub
    If you run the project, select the second tab and click the Button, you'll see the PictureBox appear. If you don't then something on your system is broken.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Re: PictureBox Inside a Panel That is Inside a TabControl TabPage

    Thank you guys. Yes, you are right. The problem that I was having was with the declaration SquarePic.Location = New Point(......). I wasn't doing it correctly.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: PictureBox Inside a Panel That is Inside a TabControl TabPage

    Quote Originally Posted by VB-MCU-User View Post
    Thank you guys. Yes, you are right. The problem that I was having was with the declaration SquarePic.Location = New Point(......). I wasn't doing it correctly.
    That's odd because that line looks OK to me, although it should have been before the control was added.

    That said, there's not a declaration. For it to be a declaration, you'd have to be declaring something, which you're not. That line creates a Point value and assigns it to a property. A declaration would require a Dim keyword.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Re: PictureBox Inside a Panel That is Inside a TabControl TabPage

    No, the line that I actually used wasn't SquarePic.Location = New Point(10,10) with the 10,10. I used something else instead of the 10,10 that was messing everything up. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width