Rule-Lines in Dialog Boxes
Example:
Does anyone know how t开发者_运维知识库hese dialog box rule-lines are created? Can they be created at design-time using a common control or are they custom-controls?I'd like to simply drag one on my form and position it, but I cannot find the control (if it does exist).
Anyone have any experience creating these?
(I'm using Visual Studio .NET 2010; C#)
Okay guys, thanks to the information gleaned from all three answers above (Hans Passant, Reddog, and itowlson), I have hacked together a working solution. I provide full details below for future enquirers.
Basically, it's a subclassed GroupBox control where only the top border is drawn (using ControlPaint.DrawBorder3D) and the GroupBox.Text property is set, by default, to emtpy.
Create a class file (say, Seperator.cs), add it to your project and paste the following inside it:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace your_namespace
{
public class Separator : GroupBox
{
[DefaultValue("")]
public override String Text
{
get {return String.Empty;}
}
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder3D(
e.Graphics,
this.ClientRectangle,
Border3DStyle.Etched,
Border3DSide.Top
);
}
}
}
Compile your project. Once you've compiled your project, a "Seperator" component is going to show up in the "your_namespace Components" section of the Toolbox in the Visual Studio designer. You can then just drag the "Seperator" component onto your form, position and shape it to your pleasing.
Thanks again to everyone who took the time to reply, and I hope this helps anyone in the future looking to solve this problem.
Use the ControlPaint.DrawBorder3D method:
private void Form1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder3D(
e.Graphics,
new Rectangle(10, 10, 300, 10),
Border3DStyle.Etched,
Border3DSide.Top);
}
Note the use of Border3DSide.Top. This avoids the visual glitch you get at the right hand end if you use the "group box with minimal height" trick.
As per Hans' answer, a GroupBox will usually suffice and will theme itself nicely with the OS.
We keep the following in our control library:
public class Separator : GroupBox
{
// Methods
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
base.SetBoundsCore(x, y, width, 3, specified);
}
// Properties
[DefaultValue("")]
public override string Text
{
get
{
return string.Empty;
}
set
{
}
}
}
精彩评论