Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create lines in windows form with visual studio 2010?

I wonder if it's possible to add a line to the design in a windows form? I can't find any tool for this in the toolbox? Or is there some other way to do this in visual studio or in code?

like image 699
3D-kreativ Avatar asked Dec 21 '22 19:12

3D-kreativ


1 Answers

There isn't a built-in control for WinForms to do this. You can use the GroupBox control though, set the Text property to an empty string, and set it's height to 2. This will mimic a embossed line. Otherwise, you need to create a custom control and paint the line yourself.

For a custom control, here's an example.

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication12
{
    public partial class Line : Control
    {
        public Line() {
            InitializeComponent();
        }        

        private Color m_LineColor = Color.Black;
        /// <summary>
        /// Gets or sets the color of the divider line
        /// </summary>
        [Category("Appearance")]
        [Description("Gets or sets the color of the divider line")]
        public Color LineColor {
            get {
                return m_LineColor;
            }
            set {
                m_LineColor = value;
                Invalidate();
            }
        }

        protected override void OnPaint(PaintEventArgs pe) {
            using (SolidBrush brush = new SolidBrush(LineColor)) {
                pe.Graphics.FillRectangle(brush, pe.ClipRectangle);
            }
        }
    }
}

It simply fills the ClientRectangle with the specified LineColor, so the height and width of the line is that of the control itself. Adjust accordingly.

like image 156
David Anderson Avatar answered Jan 18 '23 22:01

David Anderson