Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hey

How I can draw a simple line in windows form? Something like this 2 lines in this image:

http://www.windows7howto.net/wp-content/uploads/2010/07/Windows-7-Keyboard-Shortcuts.png
Posted
Updated 24-Mar-11 9:45am
v3

Handle Control.Paint event (for a control or a form) or override Control.OnPaint. In both cases, use System.Drawing.Graphics instance obtained from even argument. Using this instance, use System.Drawing.Graphics.Draw... methods to render whatever you want.

[EDIT]

After clarification by OP:

My method will work for this. You can use custom Control and override OnPaint or, more simply, use just Panel and Paint event. When you change the key combination, don't forget: you need to call Control.Invalidate. That's it. It will do the trick.

Alternative suggestion: may be you graphical element is too simple. Try to find some good character in Unicode (use standard "Character Map" application to see what's available) and use just a read-only TextBox and just text in it. For a vertical line, you can use code point '|': 0x007C :-) or '∣' 0x2223 (Mathematical Operations — "Divides" or even '│' 0x2502 or '┃' 0x2503 (Box Drawing, Light Vertical / Heavy Vertical).
Why making things more complex than they can be?

—SA
 
Share this answer
 
v4
Comments
Saman With You 24-Mar-11 15:50pm    
I want something like GroupBox but only one line not a shape like that.
http://www.windows7howto.net/wp-content/uploads/2010/07/Windows-7-Keyboard-Shortcuts.png
Sergey Alexandrovich Kryukov 24-Mar-11 16:11pm    
Understand. See the clarification in my updated Answer.
Also, consider the alternative.
--SA
Saman With You 24-Mar-11 16:24pm    
Thank you very much Sir.
Sergey Alexandrovich Kryukov 24-Mar-11 16:27pm    
You're welcome. Please see my update with Unicode code point.
Are you going to formally accept my Answer now?
I think you have all you need...
--SA
Sergey Alexandrovich Kryukov 24-Mar-11 16:28pm    
Great, thanks.
Good luck, call again.
--SA
You could also consider the various shape controls available:
Shape Control for .NET[^]
Advanced Shape Control[^]

Or if you want to do something really fancy - take a look at:
Sprite Editor for .NET[^].

You can overlay your forms with sprites as is shown in an earlier article:
FormSprite: I Love You Jessica![^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Mar-11 0:00am    
Here is my 5 for the sprite idea.
--SA
Espen Harlinn 25-Mar-11 4:25am    
Thank you, SAKryukov!
JF2015 28-Mar-11 1:34am    
Good alternative answer!
Espen Harlinn 28-Mar-11 18:27pm    
Thank you, JF2015!
Sergey Alexandrovich Kryukov 30-Mar-11 15:33pm    
>5ed - but: Why making things more complex than they can be? Well, sometimes it's fun, as in what OP could do with sprites :) [--Espen Harlinn]

Fun detected! My 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900