Dynamic Add Control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Dear All

I`m tring to add a PictureBox to a Form. My code as below.

Dim myPic as new PictureBox
myPic.Location = new Point(10,10)
myPic.Size = new Size(100,100)
FrmMain.Controls.Add(myPic)

Then I got a "ArgumentException" at this line
FrmMain.Controls.Add(myPic)

Somebody help?
Thanks a lot !!

Joey
 
FrmMain is an instance of your main Form, not just the name? If you're in
the Form code itself, it should be Me.Controls.Add(...)

-Chris
 
Hi~ Chris.

The problem is I wrote those code in another thread......
But now I have another problem.
I added this to my code.

AddHandler myPic.Click, AddressOf myPic_Click

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

The event "myPic_Click" can be fired but sender is nothing @@"
How can I get this "sender" then I can know which one of PictureBox fire
this event?

Thanks a lot !!
 
Joey,

You really want to only change controls, add controls, or add handlers for
controls on the same thread that the controls were created on. Look at
Control.Invoke to see what you can safely do to controls from outside the
thread that created them.
 
Back
Top