Greetings,
I am writing a software for work in Microsoft Visual Studios 2010. I am currently trying to place a image of the logo at the top of the software and keep it centered when form is maximized. I seem to be able to keep the buttons i make move along with the size of the form to expand outward or inward, but the picturebox is not as easy for me.
So I was hoping for help in setting the photo to move, staying centered above the controls with the rest of the form as the window size changes.
7 Replies - 2258 Views - Last Post: 06 January 2013 - 06:46 PM
#1
Centering PictureBox in Visual Studios 2010 C# Problem
Posted 05 January 2013 - 11:23 PM
Replies To: Centering PictureBox in Visual Studios 2010 C# Problem
#2
Re: Centering PictureBox in Visual Studios 2010 C# Problem
Posted 06 January 2013 - 03:28 AM
you just have to calculate the central point within the resizing event and set the picture position to it.
look here: http://msdn.microsof...rol.resize.aspx
look here: http://msdn.microsof...rol.resize.aspx
#3
Re: Centering PictureBox in Visual Studios 2010 C# Problem
Posted 06 January 2013 - 03:59 AM
You need to consider two things. First, where is the center (horizontally) of the form and second, the offset of the picturebox from the center. You need to offset the picturebox because if you just put the corner at the center, it will be on the right side of the center line (think about it
).
So, you need the width of the form, divide that by two to get the center. Then you need the width of the picturebox, also divided by two. Subtract that from the form center, and that's where the corner of the picturebox should be.
So, you need the width of the form, divide that by two to get the center. Then you need the width of the picturebox, also divided by two. Subtract that from the form center, and that's where the corner of the picturebox should be.
#4
Re: Centering PictureBox in Visual Studios 2010 C# Problem
Posted 06 January 2013 - 06:16 AM
Does the Control.Resize event works for Maximize and Minimize?
#5
Re: Centering PictureBox in Visual Studios 2010 C# Problem
Posted 06 January 2013 - 06:18 AM
To the OP: it sounds like WinForms project, but if you decide to move to WPF, resizing and positioning would be a piece of cake.
#6
Re: Centering PictureBox in Visual Studios 2010 C# Problem
Posted 06 January 2013 - 08:22 AM
lucky3 is right that WPF would make this a lot easier. And since it is all equally new to you why not just start with WPF as it is the future of Windows programming and WinForms is the past?
But if you are stuck doing this in WinForm you can also look at the .Anchor property. By default it is set to Left and Top. But you can turn on Right as well so a control tries to maintain spacing on both sides.
But if you are stuck doing this in WinForm you can also look at the .Anchor property. By default it is set to Left and Top. But you can turn on Right as well so a control tries to maintain spacing on both sides.
#7
Re: Centering PictureBox in Visual Studios 2010 C# Problem
Posted 06 January 2013 - 05:28 PM
I can't easily switch to WPF, we have a half year's worth of code invested in winforms.
Momerath i'll try that out and post back once i do it.
tlhIn`toq: i had anchor set to left top and right, it didn't work
Momerath i'll try that out and post back once i do it.
tlhIn`toq: i had anchor set to left top and right, it didn't work
#8
Re: Centering PictureBox in Visual Studios 2010 C# Problem
Posted 06 January 2013 - 06:46 PM
This pseudo code maybe helpful:
Something similar can be done with the height.
pictureBox.X = (pictureBox.Parent.Width - pictureBox.Width) / 2;
Something similar can be done with the height.
Page 1 of 1

New Topic/Question
Reply



MultiQuote




|