Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

Adding an image to a tab in another form programatically

Hello

I am trying to build controls on a tab in form1 from a click event on form2. Thanks to some previous help on here I am able to add a label and a texbox. I tried applying the same syntax to adding an image from an imagelist but it will not work. I am stuck on how to proceed.

Here is what I have so far:
Private Sub butAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butAdd.Click
        Dim tabAttLbl As New Label
        Dim tabAttTxt As New TextBox
        Dim tabAttImg As New Image 'New gives an error = New can not be used on a class that is declared 'MustInherit so I removed New
        tabAttLbl.Size = New Size(48, 16)
        tabAttTxt.Size = New Size(136, 20)
        tabAttImg.Size = New Size(16, 16) 'error is read only so I dropped New and still an error says it can not be indexed
        tabAttImg = clsGlobals.Form1.ImageList1.Images(0) - The image index I wish to add
        tabAttLbl.Location = New Point(8, 8)
        tabAttTxt.Location = New Point(64, 8)
        tabAttImg.Location = New Point(208,8) 'error Location is not part of the colllection of events
        tabAttLbl.Name = txtName.Text
        tabAttLbl.Text = txtName.Text
        tabAttTxt.Name = txtName.Text
        clsGlobals.Form1.TabControl1.SelectedTab.Controls.Add(tabAttLbl) 'this works fine
        clsGlobals.Form1.TabControl1.SelectedTab.Controls.Add(tabAttTxt) 'this works fine
        clsGlobals.Form1.TabControl1.SelectedTab.Controls.Add(tabAttImg) 'error system drawing image cannot be converted to system windows form control
End Sub

So although it does not work I think you can see what I am attempting - I want a picture in ImageList1 Index 0 on Form1 to be placed at Location 208, 8 Size 16, 16 on TabControl1 Selected Tab on Form1. All of this is being done from a click event on Form2.

I have more questions and help required on building a click event programatically but I will save that for another question.

Any help is appreciated.

Thanks

PB
Avatar of Todd MacPherson
Todd MacPherson
Flag of Canada image

ASKER

PS I am using VB.Net Visual Studio 2003
ASKER CERTIFIED SOLUTION
Avatar of jrscherer
jrscherer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Finished product below:

        Dim tabAttLbl As New Label
        Dim tabAttTxt As New TextBox
        Dim tabAttImg As New PictureBox
        tabAttLbl.Size = New Size(48, 16)
        tabAttTxt.Size = New Size(136, 20)
        tabAttImg.Size = New Size(16, 16)
        tabAttLbl.Location = New Point(8, 8)
        tabAttTxt.Location = New Point(64, 8)
        tabAttImg.Location = New Point(208, 8)
        tabAttLbl.Name = txtName.Text
        tabAttLbl.Text = txtName.Text
        tabAttTxt.Name = txtName.Text
        tabAttImg.Image = clsGlobals.Form2.ImageList1.Images(0)
        tabAttImg.Name = "pic1"
        clsGlobals.Form2.TabControl1.SelectedTab.Controls.Add(tabAttLbl)
        clsGlobals.Form2.TabControl1.SelectedTab.Controls.Add(tabAttTxt)
        'tabAttImg.SizeMode = PictureBoxSizeMode.StretchImage ' this will resize the image. You may not need if the image is 16 x 16 already
        clsGlobals.Form2.TabControl1.SelectedTab.Controls.Add(tabAttImg)

This work very well. Thank you for your help.

PB
I should point out that the click event is now on form 7 and the destination of the event is form 2 hence the change in form numbers in the final sample.

Thanks once again.