471,348 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Is there a "Line" in CSharp

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
Jun 27 '08 #1
12 1459
On Apr 14, 12:08*pm, Dom <dolivas...@gmail.comwrote:
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
Jun 27 '08 #2
On Apr 14, 12:08*pm, Dom <dolivas...@gmail.comwrote:
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.SupportsTransparentBackColo r,
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);
}
}

}
}
Jun 27 '08 #3
Dom
On Apr 14, 5:37*pm, Greg <gcad...@gmail.comwrote:
On Apr 14, 12:08*pm, Dom <dolivas...@gmail.comwrote:
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.SupportsTransparentBackColo r,
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
Jun 27 '08 #4
On Mon, 14 Apr 2008 12:27:43 -0700, Greg <gc*****@gmail.comwrote:
On Apr 14, 12:08*pm, Dom <dolivas...@gmail.comwrote:
>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:
Just for the record: both of your examples inherit "UserControl" when in
fact I don't see anything about the original problem or your examples that
actually need the features in the UserControl class.

If you're writing a custom Forms control, the correct base class is simply
Control. UserControl is more appropriate for composite controls,
especially those you create using the Designer (i.e. by dragging
individual controls into the UserControl as necessary...again, if you
don't really want your custom control to be made up of individual
pre-existing controls, then the UserControl class isn't the right base
type to use).

I mention this because it's a very common misconception that custom
controls should be derived from UserControl, and I think it's important to
nip that misconception in the bud any time it shows up. (Likewise the
nearly-as-common misconception that the PictureBox class is a suitable
custom control base class even when you're not specifically displaying an
image in your custom control).

As long as I'm replying, some other comments with respect to the specific
example:
public class Foo : UserControl
{
public Foo()
: base()
Explicitly calling the parameterless base constructor is unnecessary.
This is done automatically if you don't call any specific base constructor.
{

}
Having an empty parameterless constructor is also unnecessary.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (System.Drawing.Pen p = new
System.Drawing.Pen(System.Drawing.Color.Red))
With pre-defined colors, it's better to just use the built-in Pen, rather
than creating your own. For example, "Pens.Red". And of course, no need
to dispose such a built-in Pen when you're done with it too.
{
e.Graphics.DrawLine(p, 0, 0, 200, 200);
}
It would be better to simply draw the line across the width of the custom
control, rather than hard-coding its size.

The other example is better in some ways, but it includes some
redundancies (why set the size of the control twice? why add all the
extra style flags when a basic control would have been fine?) and also
exceeds the OP's question (includes _two_ different text properties, when
all he wanted was a line). That said, it at least handles the basic
functionality better (especially with respect to not hard-coding the line
width :) ), so I'd say on the whole it is in fact a better example.

And I see from Dom's reply that he took what is perhaps the most practical
approach: use an existing control type that will render an acceptable form
of a line when configured appropriately. :)

Pete
Jun 27 '08 #5
On Apr 15, 7:38 am, Dom <dolivas...@gmail.comwrote:
>
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
Jun 27 '08 #6
"Dom" <do********@gmail.comwrote:
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.
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.
Jun 27 '08 #7
On Apr 14, 5:57*pm, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
"Dom" <dolivas...@gmail.comwrote:
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.

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/lib...22(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
Jun 27 '08 #8
On Apr 14, 5:57*pm, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
"Dom" <dolivas...@gmail.comwrote:
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.

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
Jun 27 '08 #9
On Tue, 15 Apr 2008 16:23:20 -0700, Greg <gc*****@gmail.comwrote:
Thanks for the tips Pete; the first example was from an MSDN "How To"
example found here:
http://msdn2.microsoft.com/en-us/lib...22(VS.71).aspx
MSDN's examples are not always the best, especially those from the "early
days" of .NET. :) Of course, it's a good idea to post links to samples,
even if you copy them verbatim. That way people know where they came from
and have a better understanding of related material (and it gives you some
deniability if the example is less than perfect too :) ).
And the second example was from utilizing Lutz Roeders Reflector 5.1
on a 3'rd party control my previous company purchased.
Did you check with the author of the control before posting the code? Or
does the license for the control specifically allow redistribution of the
code? The latter is uncommon. If you didn't do the former, you're likely
in violation of your license.
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.
What seems elementary to some is not so elementary to others. I think if
you don't want to answer his questions, that's your perogative. I'm not
sure what the use there is in calling him a "c# noub" or implying that
posting elementary questions is a waste of time.

Inasmuch as we don't have different newsgroups for different skill levels,
I'm happy to see any question here. Even the most very basic questions
offer an opportunity to help a person know how to find answers on their
own (by showing them links to MSDN :) ).

Pete
Jun 27 '08 #10
On Apr 14, 9:08 pm, Dom <dolivas...@gmail.comwrote:
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 ?
Jun 27 '08 #11
On Apr 16, 9:22*am, "csh...@elfware.za.net" <elfw...@gmail.comwrote:
On Apr 14, 9:08 pm, Dom <dolivas...@gmail.comwrote:
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 ?
Can't you use seperator controller?
Jun 27 '08 #12
Greg wrote:
On Apr 14, 5:57 pm, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
>"Dom" <dolivas...@gmail.comwrote:
>>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.

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.
Did you try different DPI settings? That's what usually breaks Windows
Forms layouts.
>
Greg

Jun 27 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by ncf | last post: by
3 posts views Thread by Colleyville Alan | last post: by
4 posts views Thread by Chris Seymour | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.