• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

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
0
PBLack
Asked:
PBLack
  • 3
1 Solution
 
PBLackAuthor Commented:
PS I am using VB.Net Visual Studio 2003
0
 
jrschererCommented:
Hi PBLack
A Label and a TextBox are Controls, but an Image is not. So you can't add it like a control
Load the Image into a Picturebox and add this to your Tab in Form1. A PictureBox is a Control.

Dim tabAttImg As New Picturebox
        tabAttImg.Size = New Size(16, 16) ' error will disappear
        tabAttImg.SizeMode = PictureBoxSizeMode.StretchImage ' this will resize the image. You may not need if the image is 16 x 16 already
        tabAttImg.Image = clsGlobals.Form1.ImageList1.Images(0) - The image index I wish to add
        clsGlobals.Form1.TabControl1.SelectedTab.Controls.Add(tabAttImg)  ' Add the control

I don't have my engineering computer available, so I could not test it.
However, this will be very close of what you need. (I hope)

Jack.net
0
 
PBLackAuthor Commented:
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
0
 
PBLackAuthor Commented:
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.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now