|
-
Jan 30th, 2007, 04:05 PM
#1
Thread Starter
Member
[RESOLVED] How do I programmatically set a control's margins?
Hello everybody,
I understand the difference between margins and padding in that a margin controls the external distances between elements on a form while padding is used to control the internal distances of a control's content.
Programmatically I am able to set a control's padding like this:
Code:
PictureBox myPictureBox = new PictureBox();
myPictureBox.Width = 100;
myPictureBox.Height = 100;
Padding myPadding = new Padding();
myPadding.All = 0;
myPictureBox.Padding = myPadding;
The above code compiles, but I am having no luck in setting the margins in the same manner as the padding. If somebody could post an example I'd be grateful. Thank you.
Regards,
The Thing.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Jan 30th, 2007, 04:49 PM
#2
Re: How do I programmatically set a control's margins?
The Margin property makes no difference at run time. Where it makes a difference is how your controls will be positioned at design time by snap lines as you drag them on a form. If you are creating controls at run time and want to position them according to their Margins then you can do so but it is up to you to write the code because it doesn't happen automatically, e.g.
Code:
Button btn = new Button();
btn.Margin = this.Button1.Margin;
btn.Location = new Point(this.Button1.Right + this.Button1.Margin.Right + btn.Margin.Left, _
this.Button1.Top);
this.Controls.Add(btn);
-
Jan 30th, 2007, 06:09 PM
#3
Thread Starter
Member
Re: How do I programmatically set a control's margins?
Thanks for clearing that up for me jmcilhinney.
Base upon the example code you've posted above I think I will tackle this from a different angle - I will add my controls at design time, and then in my code I will build up an array which references the controls found on the form.
I'm thinking of something along the lines of the following as part of a loop.
myPictureBoxes[0] = (PictureBox)tableLayoutPanel1.GetControlFromPosition(x, y); //Why do I need to use casting?
Is this ok or is there a better way to build up an array to work with the controls found on a form?
Could you explain the difference between Image and BackgroundImage in relation to controls such as panels and pictureboxes, etc.
Thanks again for your help.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Jan 30th, 2007, 07:18 PM
#4
Re: How do I programmatically set a control's margins?
If you feel that you need a separate array rather than just getting controls as needed from the TLP then what you've shown would basically do the job.
As for why you need to cast, GetControlFromPosition returns a Control reference, which may refer to any type of control. You cannot assign just any type of control to an element of a PictureBox array. You can only assign PictureBoxes. By casting the result of GetControlFromPosition you are telling the compiler that you will do what is required to make sure that the actual control referred to IS a PictureBox. If at run time the control isn't a PictureBox then your app will crash, but it will be your fault and not the compiler's. The compiler tries to help you do the right thing but it's still up to you to do it.
As for Image and BackgroundImage, the BackgroundImage is generally displayed behind all other visual components of a control. Its purpose would normally be as a texture or something like that. The Image on the other hand will be displayed in the foreground, over the BackgroundImage. The Image is usually more of a focal point of the control.
-
Feb 1st, 2007, 05:21 PM
#5
Thread Starter
Member
Re: How do I programmatically set a control's margins?
Thanks for the helpful reply jmcilhinney, I'll mark this thread as Resolved.
Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0
~ Man Is The Warmest Place To Hide ~
-
Jan 6th, 2013, 01:58 AM
#6
New Member
Re: [RESOLVED] How do I programmatically set a control's margins?
PictureBox myPictureBox = new PictureBox();
myPictureBox.Width = 100;
myPictureBox.Height = 100;
Padding myMargin = new Padding();
myMargin.All = 0;
myPictureBox.Margin = myMargin;
' Note that a Margin object is defined from the padding class like this: Public Property Margin As System.Windows.Forms.Padding
' So when you are defining new instance of a margin object you must use the Padding class.
-
Jan 6th, 2013, 02:02 AM
#7
New Member
Re: [RESOLVED] How do I programmatically set a control's margins?
Code:
PictureBox myPictureBox = new PictureBox();
myPictureBox.Width = 100;
myPictureBox.Height = 100;
Padding myMargin = new Padding();
myMargin.All = 0;
myPictureBox.Margin = myMargin;
Note that a Margin object is defined from the padding class: "Public Property Margin As System.Windows.Forms.Padding"
So when you are defining new instance of a margin object you must use the Padding class.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|