Is there a "Line" in CSharp

  • Thread starter Thread starter Dom
  • Start date Start date
D

Dom

VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox. Did CSharp drop
this?

Dom
 
VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox.  Did CSharp drop
this?

Dom

Hi Dom,

There is no built-in C# line control, however, it's fairly straight-
forward:
Just make a usercontrol and override the OnPaint:

public class Foo : UserControl
{
public Foo()
: base()
{

}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (System.Drawing.Pen p = new
System.Drawing.Pen(System.Drawing.Color.Red))
{
e.Graphics.DrawLine(p, 0, 0, 200, 200);
}
}
}


Cheers,

Greg
 
VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox.  Did CSharp drop
this?

Dom

Here is a better example if you want to show a 3DLine effect.

public class Foo : UserControl
{

int mSpacing = 0;
Border3DStyle mBorderStyle =
(Border3DStyle)Border3DStyle.Etched;
private System.ComponentModel.IContainer components = null;


#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Line
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.Name = "Line";
this.Size = new System.Drawing.Size(150, 16);
this.ResumeLayout(false);

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#endregion

public Foo()
{

InitializeComponent();

SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.SupportsTransparentBackColor,
true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.ResizeRedraw, true);

Name = "3DLine";
this.Size = new System.Drawing.Size(150, 16);
UpdateStyles();
}

public override string Text
{
get{ return base.Text;}
set { base.Text = value; }
}

public string HeaderText
{
get { return Text; }
set { Text = value; Invalidate(); }
}

public Border3DStyle LineStyle
{
get { return mBorderStyle; }
set
{
if (value != mBorderStyle) { mBorderStyle = value;
Invalidate(); }
}
}

public int Spacing
{
get { return mSpacing; }
set
{
if (value != mSpacing) { mSpacing = value;
Invalidate(); }
}
}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);

using (Brush b = new SolidBrush(ForeColor))
{
StringFormat sf = StringFormat.GenericTypographic;
SizeF s = e.Graphics.MeasureString(Text, Font, Width);
e.Graphics.DrawString(Text, Font, b, 0, (Height / 2) -
(s.Height / 2), sf);
if (s.Width + Spacing < Width)
{
Point p = new Point(Convert.ToInt32(s.Width +
Spacing), Convert.ToInt32(s.Height / 2));
ControlPaint.DrawBorder3D(e.Graphics, p.X,
(Height / 2), (Width - p.X), 5, mBorderStyle, Border3DSide.Top);
}
}

}
}
 
Here is a better example if you want to show a 3DLine effect.

    public class Foo : UserControl
    {

        int mSpacing = 0;
        Border3DStyle mBorderStyle =
(Border3DStyle)Border3DStyle.Etched;
        private System.ComponentModel.IContainer components = null;

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            //
            // Line
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F,
13F);
            this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "Line";
            this.Size = new System.Drawing.Size(150, 16);
            this.ResumeLayout(false);

        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should
be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion

        public Foo()
        {

            InitializeComponent();

            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor,
true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ResizeRedraw, true);

            Name = "3DLine";
            this.Size = new System.Drawing.Size(150, 16);
            UpdateStyles();
        }

        public override string Text
        {
            get{ return base.Text;}
            set { base.Text = value; }
        }

        public string HeaderText
        {
            get { return Text; }
            set { Text = value; Invalidate(); }
        }

        public Border3DStyle LineStyle
        {
            get { return mBorderStyle; }
            set
            {
                if (value != mBorderStyle) { mBorderStyle = value;
Invalidate(); }
            }
        }

        public int Spacing
        {
            get { return mSpacing; }
            set
            {
                if (value != mSpacing) { mSpacing = value;
Invalidate(); }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            using (Brush b = new SolidBrush(ForeColor))
            {
                StringFormat sf = StringFormat.GenericTypographic;
                SizeF s = e.Graphics.MeasureString(Text,Font, Width);
                e.Graphics.DrawString(Text, Font, b, 0, (Height / 2) -
(s.Height / 2), sf);
                if (s.Width + Spacing < Width)
                {
                    Point p = new Point(Convert.ToInt32(s.Width +
Spacing), Convert.ToInt32(s.Height / 2));
                    ControlPaint.DrawBorder3D(e.Graphics, p.X,
(Height / 2), (Width - p.X), 5, mBorderStyle, Border3DSide.Top);
                }
            }

        }
    }

Wow. That's a lot of work. Here is what I ended up doing. I used a
GroupBox with no title, and a height of 5.

Dom
 
Wow. That's a lot of work. Here is what I ended up doing. I used a
GroupBox with no title, and a height of 5.

Dom
That's a good short-term one-off solution - but adding a control to
your arsenal (IMHO) is better for the long term - as you can add to
its functionality as your needs grow (for example, you might need a
vertical version, or even an 'any angle' version - either of which
can be easily built up from the original control.

..\\axxx
 
For someone who's just seen the problems with different screen sizes,
you should be aware that this will look like crap on other systems.

Eq.

Thanks for the tips Pete; the first example was from an MSDN "How To"
example found here:
http://msdn2.microsoft.com/en-us/library/aa287522(VS.71).aspx

And the second example was from utilizing Lutz Roeders Reflector 5.1
on a 3'rd party control my previous company purchased.

In regards to Dom's 2nd post, it appears to be a rhetorical question.
Either he's a c# noub, or simply likes to waste time posting elementry
questions. It seems like every 5 posts are from him. LOL.
Maybe there should be a 'beginner, intermidiate, advanced, extream'
news groups for CSharp, or mabye there already is and I just havn't
seen them yet...
At any rate, I agree with your comments about the base() class stuff
and the 'using' statement.

Cheers,
Greg
 
For someone who's just seen the problems with different screen sizes,
you should be aware that this will look like crap on other systems.

Eq.

oops, forgot to add one more thing before I posted...
That is, I don't agree with the screen-sizes comment.
Only because the usercontrols mentioned earlier seem to look fine
ranging from 2560x1600 down to 800x600.

Greg
 
VB had a "line" control, just a simple line that let you separate
controls without the wasted space of a Groupbox. Did CSharp drop
this?

Dom

Just make a Label. set the height to 5 or something, then set the
backcolor to black and you have a line ?
 
Greg said:
oops, forgot to add one more thing before I posted...
That is, I don't agree with the screen-sizes comment.
Only because the usercontrols mentioned earlier seem to look fine
ranging from 2560x1600 down to 800x600.

Did you try different DPI settings? That's what usually breaks Windows
Forms layouts.
 
Back
Top