locked
how to add dynamic control to dynamically added picturebox? RRS feed

  • Question

  • how to add dynamic control to dynamically added picturebox?
    Wednesday, January 19, 2011 9:29 AM

Answers

  • Here an example

    Public Class Form1
    
      Private Pan1 As Panel
      Private Button1 As Button
      Private Textbox1 As TextBox
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        'add a panel to the form
        Pan1 = New Panel With {.Parent = Me, .Size = New Size(300, 200)}
    
        'add a textbox to the panel
        Textbox1 = New TextBox With {.Parent = Pan1, .Width = 150, .Location = New Point(10, 10)}
    
        'add a button in the textbox
        Button1 = New Button With {.Parent = Textbox1, .Width = 20, .Height = 15, .Text = "", .Cursor = Cursors.Arrow}
    
      End Sub
    End Class
    
    • Proposed as answer by Calvin_Gao Friday, January 21, 2011 7:40 AM
    • Marked as answer by Calvin_Gao Tuesday, January 25, 2011 10:52 AM
    Thursday, January 20, 2011 7:05 PM
  • Let me try it,

    Public Class Form1
      Private WithEvents PictureBox1 As PictureBox
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'VB Net 2008-2010 style
        PictureBox1 = New PictureBox With {.Dock = DockStyle.Fill, .Image = Image.FromFile("C:\test2\Test.jpg")}
        Controls.Add(PictureBox1)
      End Sub
      Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click 'VB 10 SP 1 style
        'Whatever
      End Sub
    End Class
    
    Tested and runs


    Success
    Cor
    • Proposed as answer by Calvin_Gao Friday, January 21, 2011 7:40 AM
    • Marked as answer by Calvin_Gao Tuesday, January 25, 2011 10:52 AM
    Thursday, January 20, 2011 7:24 PM

All replies

  • you can do this like you have added the picturebox to the form by using the controls property of the picturebox
    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 19, 2011 9:34 AM
  • Here an example

    Public Class Form1
    
      Private Pan1 As Panel
      Private Button1 As Button
      Private Textbox1 As TextBox
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        'add a panel to the form
        Pan1 = New Panel With {.Parent = Me, .Size = New Size(300, 200)}
    
        'add a textbox to the panel
        Textbox1 = New TextBox With {.Parent = Pan1, .Width = 150, .Location = New Point(10, 10)}
    
        'add a button in the textbox
        Button1 = New Button With {.Parent = Textbox1, .Width = 20, .Height = 15, .Text = "", .Cursor = Cursors.Arrow}
    
      End Sub
    End Class
    
    • Proposed as answer by Calvin_Gao Friday, January 21, 2011 7:40 AM
    • Marked as answer by Calvin_Gao Tuesday, January 25, 2011 10:52 AM
    Thursday, January 20, 2011 7:05 PM
  • Let me try it,

    Public Class Form1
      Private WithEvents PictureBox1 As PictureBox
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'VB Net 2008-2010 style
        PictureBox1 = New PictureBox With {.Dock = DockStyle.Fill, .Image = Image.FromFile("C:\test2\Test.jpg")}
        Controls.Add(PictureBox1)
      End Sub
      Private Sub PictureBox1_Click(sender As Object, e As System.EventArgs) Handles PictureBox1.Click 'VB 10 SP 1 style
        'Whatever
      End Sub
    End Class
    
    Tested and runs


    Success
    Cor
    • Proposed as answer by Calvin_Gao Friday, January 21, 2011 7:40 AM
    • Marked as answer by Calvin_Gao Tuesday, January 25, 2011 10:52 AM
    Thursday, January 20, 2011 7:24 PM