472,958 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1551
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
2
by: Andr? Roberge | last post by:
I want to "import and execute" a python program (input_test.py below) within another program (execute_test.py below) and "watch it" being executed. By watching it, I mean to display the file...
3
by: ncf | last post by:
I'm having an odd problem. I'm getting an error from IDLE saying "End Of Line detected while scanning single-quoted string." Odd thing is, it's not single-quoted, it's one of the doc-strings (if...
3
by: Colleyville Alan | last post by:
I am constructing a SQL command from a function. Some code builds the WHERE clause in a looping structure and passes that as an argument to the SQL-building function. But the results do not...
10
by: Rafi B. | last post by:
I'm running this on a Linux/CentOs/cPanel server, trying to add caching to my PHP5 code, using ADOdb. I have two Linux servers, one of them is running perfect, the second one, just crashes without...
4
by: Hexes | last post by:
Ok i am making program wicth has from txt file put lines from first to last. well If i code it like this: ---- nFileNum = FreeFile Open "C:/VB/VB.txt" For Input As nFileNum FreeNum = 1
5
by: Nathan Sokalski | last post by:
I have an ASP.NET application which is giving the following JavaScript error: 'theForm' is undefined However, when I do a View Source one of the <scriptelements is as follows: <script...
4
by: Chris Seymour | last post by:
Hi All, I am working on a python script for my colleague that will walk a directory and search in the different files for a specific string. These pieces I am able to do. What my colleague wants...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.