7 Replies - 2258 Views - Last Post: 06 January 2013 - 06:46 PM Rate Topic: -----

#1 CMDDrake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-June 10

Centering PictureBox in Visual Studios 2010 C# Problem

Posted 05 January 2013 - 11:23 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Centering PictureBox in Visual Studios 2010 C# Problem

#2 Anthonidas   User is offline

  • D.I.C Head

Reputation: 32
  • View blog
  • Posts: 240
  • Joined: 25-April 11

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
Was This Post Helpful? 0
  • +
  • -

#3 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

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.
Was This Post Helpful? 0
  • +
  • -

#4 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

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?
Was This Post Helpful? 0
  • +
  • -

#5 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

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.
Was This Post Helpful? 1
  • +
  • -

#6 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6538
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#7 CMDDrake   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 08-June 10

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
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7915
  • View blog
  • Posts: 26,425
  • Joined: 05-May 12

Re: Centering PictureBox in Visual Studios 2010 C# Problem

Posted 06 January 2013 - 06:46 PM

This pseudo code maybe helpful:
pictureBox.X = (pictureBox.Parent.Width - pictureBox.Width) / 2;


Something similar can be done with the height.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1