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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make a form resize according to the size of a picture in a picturebox! 2

Status
Not open for further replies.

Kimo9909

Programmer
Jun 15, 2003
2
US
I made a picturebox programmatically resize to a picture. How do I make the form resize according to the picturebox?

Thanks in advance,
Kimo
 
You can change the size of the form in the same way you change the size of the picture box.

ie.
Code:
Int32 width = 140;
			Int32 height = 70;
			
			pictureBox1.Location = new Point(0,0);
			pictureBox1.Size = new Size(width,height); //picture box
			this.Size  = new Size(width,height);				   //form

forms also have a client size attribute which allows us to set the size of the form withouth having to worry about the title bar.

Code:
Int32 width = 140;
			Int32 height = 70;
			
			pictureBox1.Location = new Point(0,0);
			pictureBox1.Size = new Size(width,height); //picture box
			this.ClientSize  = new Size(width,height);				   //form

Regards

 
Oh crikey C# code instead of VB.net

VB code

Code:
Dim width As Int32 = 140
Dim height As Int32 = 70

PictureBox1.Location = New Point(0, 0)
PictureBox1.Size = New Size(Width, Height) 'picture box
        ClientSize = New Size(Width, Height) 'form

 
You can do it somewhat easier, just as you would have in VB 6.

Lets say you always want the form to be 1.5 times the size of the picture box:

Private Sub PictureBox1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Resize
Me.Height = PictureBox1.Height * 1.5
Me.Width = PictureBox1.Width * 1.5
End Sub

Or, of course, if you want a border around it to remain the same, you can add to the height and width.
 
Normally using the Height and Width values of a control is fine, but if you do this with a form the height includes the title bar and I assume the height and width both include any borders the form has.

Cheers
 
If you are increasing the height and width by a static amount as the picture box resizes, then it doesn't matter if it includes the title bar, as the increase in pixels will be added to the form part itself.
 
Thanks for the insight...both of you! I'm trying to learn as much as I can as fast as I can so I can get into the field of programming as soon as possible! Take care!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top