iMash Posted May 20, 2011 Posted May 20, 2011 I am seriously confused as to why this isn't working. I'm trying to create a resize sub for my form so that when you resize the form, all of the content resizes and places itself in the correct position. I have a statement that is supposed to centre a label (not resized) within a panel on resize. But it always ends up significantly further right than I calculate it should be. The offending line of code is...... lblRoomSelect.Left = pnlRoomSelect.Left + ((pnlRoomSelect.Width - lblRoomSelect.Width) / 2) Can anyone see what I am doing wrong? Or even suggest a better way of doing this? Thanks in advance, Ash
LosOjos Posted May 20, 2011 Posted May 20, 2011 I suspect it's because the position refers to the top-left corner of the control, not it's centre, so you'd need to use (something like, might be dodgy code) this: lblRoomSelect.Left = pnlRoomSelect.Left + ((pnlRoomSelect.Width / 2) - (lblRoomSelect.Width / 2)) As I say, not sure the code's exactly right, but you need to take in to account the control's width as well as the width of the panel 1
iMash Posted May 20, 2011 Author Posted May 20, 2011 (edited) Thanks for the suggestion Los0jos. I have tried it but it results in the same. I'll continue to play and see if I can find out why it is doing it. I have just tried setting the left of the label to the left of the panel and the label ends up about 10 - 20 pixels away from the left of the panel. Grrrr why cant left actually mean left not "left plus a bit" lblRoomSelect.Left = pnlRoomSelect.Left That would be where my problem is coming from. Edited May 20, 2011 by iMash Found problem
Steve21 Posted May 20, 2011 Posted May 20, 2011 Thanks for the suggestion Los0jos. I have tried it but it results in the same. I'll continue to play and see if I can find out why it is doing it. I have just tried setting the left of the label to the left of the panel and the label ends up about 10 - 20 pixels away from the left of the panel. Grrrr why cant left actually mean left not "left plus a bit" lblRoomSelect.Left = pnlRoomSelect.Left That would be where my problem is coming from. You sure you haven't set some floating margins etc? Left is left on my VB Steve
iMash Posted May 20, 2011 Author Posted May 20, 2011 (edited) You sure you haven't set some floating margins etc? Left is left on my VB Steve Does your left mean left within a panel or a tab control? My left means left if I'm referencing say another textbox or another label But all of my margins are set as 0 and all padding is set as 0 and still when I reference the left of the panel its left plus a bit. EDIT: My bad I'm just being a complete tit and forgetting that when I reference within a panel that the left of the panel is 0 regardless of what the location of the panel is in reference to everything else. Its been a while since I wrote anything in VB. Solved it. Edited May 20, 2011 by iMash Solved
LosOjos Posted May 20, 2011 Posted May 20, 2011 We might need to see a bit more code... would you be willing to share the entire chunk related to form resizing so we can see if there's anything odd going on with it?
iMash Posted May 20, 2011 Author Posted May 20, 2011 (edited) Thanks Los0jos but I have solved it now. In the 6 months that I haven't used Visual Studio I forgot that when you are referencing something within a panel that the top left of the panel is always 0, 0 regardless of where the panel sits within the form. Schoolboy error. The corrected and properly functioning line is now........... lblRoomSelect.Left = (pnlRoomSelect.Width / 2) - (lblRoomSelect.Width / 2) Thanks for the help anyway Edited May 20, 2011 by iMash
Jamo Posted June 12, 2011 Posted June 12, 2011 Hi, Just thought I would mention that its not a good way of using the controls by manually doing that. There are dedicated position controls such as the layout panels in VB.NET which you can use to automatically position your controls. This will sort out all the postitioning for you and give you a consistent interface in which you won't have to duplicate all the code above. J
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now