C# Forms/Controls
-
I wish to use the Dock property, but I need a 10 pixel inset on all 4 sides. Let's say I am creating a simple editor. I place a TextBox control on a Form, but I'd like 10 pixel insets on every side of the TextBox. One way: Add a panel to the form, position the panel with 10 pixel insets on all 4 sides, and Anchor to all 4 sides. Then, attach the (Dock=Fill) TextBox to the panel panel. Is there any way to do this without using a Panel like this? I realize that if all I was creating was a text editor, then I could just Anchor the text box to all side of the form, and position the text box correctly, but, my real application is a bit more complicated. There are several controls inside the form - including a splitter. So, I can't really use Anchor for every case. The crux of the problem is that when using the Dock property, the child control is docked right up against the edge of its parent. I was wondering if there was a property that could buffer or inset that child control. I'd like to avoid having to add a Panel to my application everytime I want to position something just the way I like it - while using the Dock property. How about a thick transparent border? FormBorderStyle will not work. I am working inside the form. I'd also like to do this same thing for controls inside the form attached to parent Panels. I see that Panel can have a BorderStyle. Is there a way I can somehow customize the actual border? I google "custom borders in C#" or something. Thanks, -Luther
-
I wish to use the Dock property, but I need a 10 pixel inset on all 4 sides. Let's say I am creating a simple editor. I place a TextBox control on a Form, but I'd like 10 pixel insets on every side of the TextBox. One way: Add a panel to the form, position the panel with 10 pixel insets on all 4 sides, and Anchor to all 4 sides. Then, attach the (Dock=Fill) TextBox to the panel panel. Is there any way to do this without using a Panel like this? I realize that if all I was creating was a text editor, then I could just Anchor the text box to all side of the form, and position the text box correctly, but, my real application is a bit more complicated. There are several controls inside the form - including a splitter. So, I can't really use Anchor for every case. The crux of the problem is that when using the Dock property, the child control is docked right up against the edge of its parent. I was wondering if there was a property that could buffer or inset that child control. I'd like to avoid having to add a Panel to my application everytime I want to position something just the way I like it - while using the Dock property. How about a thick transparent border? FormBorderStyle will not work. I am working inside the form. I'd also like to do this same thing for controls inside the form attached to parent Panels. I see that Panel can have a BorderStyle. Is there a way I can somehow customize the actual border? I google "custom borders in C#" or something. Thanks, -Luther
As for the docking, you could use panels, which derive from
ScrollableControl
which has aDockPadding
property. Dock you panels, set the padding, and you can dock or anchor your controls inside those. In regard to drawing a border, you can use simple owner-drawing by overridingOnPaint
and drawing a rectangle (see theControlPaint
class for some handy helper methods), or go so far as to overrideWndProc
and do it a little lower-level. I'd recommend the managed way for portability and ease, though!-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
As for the docking, you could use panels, which derive from
ScrollableControl
which has aDockPadding
property. Dock you panels, set the padding, and you can dock or anchor your controls inside those. In regard to drawing a border, you can use simple owner-drawing by overridingOnPaint
and drawing a rectangle (see theControlPaint
class for some handy helper methods), or go so far as to overrideWndProc
and do it a little lower-level. I'd recommend the managed way for portability and ease, though!-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
wow.
-
As for the docking, you could use panels, which derive from
ScrollableControl
which has aDockPadding
property. Dock you panels, set the padding, and you can dock or anchor your controls inside those. In regard to drawing a border, you can use simple owner-drawing by overridingOnPaint
and drawing a rectangle (see theControlPaint
class for some handy helper methods), or go so far as to overrideWndProc
and do it a little lower-level. I'd recommend the managed way for portability and ease, though!-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
The first method works, and provides my insets. But regarding option #2, the point of the border was to "pad" the form. I do in fact, get the border to draw, but the child controls still Dock against the edges of the form, overlapping even the border I am drawing. Should I somehow affect the Form.ClientRectangle {get;} before forwarding to base.OnPaint () so that the border I've drawn is not overlapped by child controls? Or shall I work it out by attaching child controls to a singular, centered child Panel again? Also - what is the Form.ClipRectangle represent? The area that needs repainting? In my little app, its always 0,0,0,0. Shouldn't it be equal to the Form.ClientRectangle? It is 0,0,0,0 every time OnPaint gets called. Even when the app is completely hidden by other windows - and then pulled to the front. Regardless, thats just neat stuff. Thanks, -Luther
-
wow.
lutherbaker wrote: wow. Heh, I think that's what we all say to Heath's posts... ;)
When I can talk about 64 bit processors and attract girls with my computer not my car, I'll come out of the closet. Until that time...I'm like "What's the ENTER key?" -Hockey on being a geek
-
The first method works, and provides my insets. But regarding option #2, the point of the border was to "pad" the form. I do in fact, get the border to draw, but the child controls still Dock against the edges of the form, overlapping even the border I am drawing. Should I somehow affect the Form.ClientRectangle {get;} before forwarding to base.OnPaint () so that the border I've drawn is not overlapped by child controls? Or shall I work it out by attaching child controls to a singular, centered child Panel again? Also - what is the Form.ClipRectangle represent? The area that needs repainting? In my little app, its always 0,0,0,0. Shouldn't it be equal to the Form.ClientRectangle? It is 0,0,0,0 every time OnPaint gets called. Even when the app is completely hidden by other windows - and then pulled to the front. Regardless, thats just neat stuff. Thanks, -Luther
You mean the
PaintEventArgs.ClipRectangle
? The clipping region is the area that has been invalidated and needs repainting, yes. This will not affect the controls themselves, however (as you've noticed). In order to do that, you have to position the child controls. Changing this clipping region will have no affect, other than perhaps not painting all the invalidated regions. As far as it being straight zeros all the time, this doesn't seem right. Try overlapping it party with another window, or opening a dialog on your app and see if the clipping region is something else. I've done quite a bit of this and have never seen an "empty" rectangle. My other suspision is that - since the default value for numeric types of whichRectangle
is made is 0, that you might have some bug in your code (no offense - just meant as another possibility). On a side note, clipping regions are often used to make non-rectangular dialogs, like Windows Media Player, RealOne, etc. Windows Layers are a better alternative but is only supported in Win2K and up and isn't as easy to program (though better in the long-run).-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----