Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating picturebox via a class

Status
Not open for further replies.

solo1234

Programmer
Jun 25, 2007
39
NL
Hello everybody,

We are trying to get a picturebox on a form via a class. The reason is that we need sometimes more then one time the same picture on the form.
The mouse pointer must give the picture the right place.

Can someone tell us how to make the syntax because the next doesn't work:


Public Class Join
Dim pict As New PictureBox
pict.Location = New Point(256, 64)
pict.Size = New Size(120, 40)
End Class

Thanks for reading,
Michelle.
 
I am assuming i am not looking at all the code, because somewhere you will have to add a picture to the picture box, and somewhere you will need to add the INSTANCE of the picture box to the form, then set its location.

-The answer to your problem may not be the answer to your question.
 
solo1234,

You need something like this code block to accomplish your task perfectly.

Code:
	Public Class CustomPictureBox
		Private Shared m_PictureBox As PictureBox
		Private Shared m_PictureBoxNumber As Integer = 0

		Public Shared Sub PaintPictureBox(ByVal containerFormArg As Form, ByVal sourcePictureBoxArg As PictureBox, ByVal onTopArg As Boolean)
			m_PictureBox = New PictureBox()

			With m_PictureBox
				m_PictureBoxNumber += 1
				.Name = "CustomPictureBox" & m_PictureBoxNumber
				.Image = sourcePictureBoxArg.Image
				.BorderStyle = sourcePictureBoxArg.BorderStyle
				.Size = sourcePictureBoxArg.Size
				.SizeMode = sourcePictureBoxArg.SizeMode
				.Location = containerFormArg.PointToClient(System.Windows.Forms.Form.MousePosition)
				containerFormArg.Controls.Add(m_PictureBox)

				If (onTopArg = True) Then
					.BringToFront()
				ElseIf (onTopArg = False) Then
					.SendToBack()
				End If
			End With
		End Sub
	End Class
Code:
Private Sub Form3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
		CustomPictureBox.PaintPictureBox(Me, PictureBox1, True)
	End Sub

Code is self explanatory. I think one can understand the above code easily.

Hope it helps.

 
solo1234,

I'm sorry, I forgot to mention that I assumed you have a picturebox named PictureBox1 on Form3 when using above mentioned 2nd code of block.

 
Changed code:

Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
Dim picbox As New PictureBox
picbox.Image = Drawing.Image.FromFile("D:\test.jpg")
CustomPictureBox.PaintPictureBox(Me, picbox, True)
End Sub



Public Class CustomPictureBox
Private Shared m_PictureBox As PictureBox
Private Shared m_PictureBoxNumber As Integer = 0

Public Shared Sub PaintPictureBox(ByVal containerFormArg As Form, ByVal sourcePictureBoxArg As PictureBox, ByVal onTopArg As Boolean)
m_PictureBox = New PictureBox
Dim sis = New Size(50, 50)
With m_PictureBox
m_PictureBoxNumber += 1
.Name = "CustomPictureBox" & m_PictureBoxNumber
.Image = sourcePictureBoxArg.Image
.BorderStyle = sourcePictureBoxArg.BorderStyle
.Size = sis
.SizeMode = sourcePictureBoxArg.SizeMode

.Location = containerFormArg.PointToClient(System.Windows.Forms.Form.MousePosition)
containerFormArg.Controls.Add(m_PictureBox)

If (onTopArg = True) Then
.BringToFront()
ElseIf (onTopArg = False) Then
.SendToBack()
End If
End With
End Sub

That's what we want!!

Thanks,
Michelle.
End Class
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top