Todd MacPherson
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.ImageList 1.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.TabContro l1.Selecte dTab.Contr ols.Add(ta bAttLbl) 'this works fine
clsGlobals.Form1.TabContro l1.Selecte dTab.Contr ols.Add(ta bAttTxt) 'this works fine
clsGlobals.Form1.TabContro l1.Selecte dTab.Contr ols.Add(ta bAttImg) '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
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.ImageList
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.TabContro
clsGlobals.Form1.TabContro
clsGlobals.Form1.TabContro
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.ImageList 1.Images(0 )
tabAttImg.Name = "pic1"
clsGlobals.Form2.TabContro l1.Selecte dTab.Contr ols.Add(ta bAttLbl)
clsGlobals.Form2.TabContro l1.Selecte dTab.Contr ols.Add(ta bAttTxt)
'tabAttImg.SizeMode = PictureBoxSizeMode.Stretch Image ' this will resize the image. You may not need if the image is 16 x 16 already
clsGlobals.Form2.TabContro l1.Selecte dTab.Contr ols.Add(ta bAttImg)
This work very well. Thank you for your help.
PB
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.ImageList
tabAttImg.Name = "pic1"
clsGlobals.Form2.TabContro
clsGlobals.Form2.TabContro
'tabAttImg.SizeMode = PictureBoxSizeMode.Stretch
clsGlobals.Form2.TabContro
This work very well. Thank you for your help.
PB
ASKER
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.
Thanks once again.
ASKER