473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

[Q] drawing box around text in a label

Dear Readers,

I am attempting to draw box around some text using unicode on
multiline label. The label is forty characters wide and 12 lines deep.

I have been trying to draw a box around text (centered in the label)
on this label.
My font on this label is Courier new - hence fixed width character
cells.

I have tried using the Box Drawing unicode in with the Courier new
font, \u250c, \u2500, \u2510 etc.

this.Label.Text="\u250f\u2501\u2513\r\n\u2503 \u2503";

However there is a gap between the characters?

-----------
| Label |
-----------

Is there a way to draw a proper box around text on a label with
Courier new font using unicode?

Alternatively is it possible to draw the text labels yourself like
ownerdrawn menu buttons?

Where I calculate the width and height of the label and my text size
in the required font and then
draw a rectangle around my text in the centre of the label? Any
examples?

Any suggestion most welecome.

Stuie
Nov 16 '05 #1
4 12291
It might seem awfully simple and hence potentially wrong .. but

cant u use the BorderStyle of the label as you box by setting it to
fixedSingle instead on None.

pardon me if this isnt even close to what you wanted :-)

Thank You
rawCoder
"Stuart Norris" <st**********@yahoo.com.au> wrote in message
news:51*************************@posting.google.co m...
Dear Readers,

I am attempting to draw box around some text using unicode on
multiline label. The label is forty characters wide and 12 lines deep.

I have been trying to draw a box around text (centered in the label)
on this label.
My font on this label is Courier new - hence fixed width character
cells.

I have tried using the Box Drawing unicode in with the Courier new
font, \u250c, \u2500, \u2510 etc.

this.Label.Text="\u250f\u2501\u2513\r\n\u2503 \u2503";

However there is a gap between the characters?

-----------
| Label |
-----------

Is there a way to draw a proper box around text on a label with
Courier new font using unicode?

Alternatively is it possible to draw the text labels yourself like
ownerdrawn menu buttons?

Where I calculate the width and height of the label and my text size
in the required font and then
draw a rectangle around my text in the centre of the label? Any
examples?

Any suggestion most welecome.

Stuie

Nov 16 '05 #2
This can be achieved by adding an event handler to the label's paint event
Add the following code to the InitializeComponent() method
this.label1.Paint += new System.Windows.Forms.PaintEventHandler(this.label1 _Paint);

And write code similar to the following in the paint event handler method. You can change the brush and pen and other parameters as per requirement.
The label size should be large enough to accomodate the entire text.

private void label1_Paint(object sender, PaintEventArgs e)
{
//calculate bounds for rectangle
int rectx = 5; //margin from top and bottom of label border
int recty = 5; //margin from left and right sides of label border
int rectWidth = label1.Width - 10;
int rectHeight = label1.Height - 10;
Graphics gr = e.Graphics;

//specify color and width of line to draw rectangle
System.Drawing.Pen pen = new Pen(System.Drawing.Color.BlueViolet, 1.1F);
//this call draws the rectangle with specified color and line width
gr.DrawRectangle(pen, rectx, recty, rectWidth, rectHeight);

//calculate center position for string
String s = "Label \nText";
Font f = new Font("Courier New", 12F); //font name and size
//get the size of string (width and height) in pixels, pass string and its font as parameters
SizeF stringSize = gr.MeasureString(s,f);
//now calculte the starting point (x,y) for the string
//the formula is margin + (rectangle width - string width) / 2
float strX = rectx + (rectWidth - stringSize.Width) / 2;
float strY = recty + (rectHeight - stringSize.Height) / 2;
//choose a brush to draw the string
System.Drawing.Brush br = new SolidBrush(System.Drawing.Color.Chocolate);
//this call draws the string at the specified position with the chosen brush
gr.DrawString(s, f, br, strX, strY);
}

Hope this helps.
--
Sameeksha,
MCP (.NET)
"Stuart Norris" wrote:
Dear Readers,

I am attempting to draw box around some text using unicode on
multiline label. The label is forty characters wide and 12 lines deep.

I have been trying to draw a box around text (centered in the label)
on this label.
My font on this label is Courier new - hence fixed width character
cells.

I have tried using the Box Drawing unicode in with the Courier new
font, \u250c, \u2500, \u2510 etc.

this.Label.Text="\u250f\u2501\u2513\r\n\u2503 \u2503";

However there is a gap between the characters?

-----------
| Label |
-----------

Is there a way to draw a proper box around text on a label with
Courier new font using unicode?

Alternatively is it possible to draw the text labels yourself like
ownerdrawn menu buttons?

Where I calculate the width and height of the label and my text size
in the required font and then
draw a rectangle around my text in the centre of the label? Any
examples?

Any suggestion most welecome.

Stuie

Nov 16 '05 #3
Hello,
Where I calculate the width and height of the label and my text size
in the required font and then
draw a rectangle around my text in the centre of the label? Any
examples?

I'm not sure whether it is exactly what you are looking for, nevertheless it
might be helpful: There is something like preferredWidth and
preferredHeight. I used it to calculate minimum size (actually height since
width was fixed) of multiline label.

Regards,
Piotrek Stachowicz
Nov 16 '05 #4
Hi Sameeksha -

It seems like if you're going to go to all the trouble of drawing the text
yourself you're better off using the StringFormat object to manage the
formatting. To get the string centered in a specific area set the Alignment
and LineAlignment properties and then pass the StringFormat object to the
DrawString method of the graphics object:
StringFormat sf = new StringFormat();

Label MyLabel = (Label)sender;

sf.Alignment = StringAlignment.Center;

sf.LineAlignment = StringAlignment.Center;

e.Graphics.DrawString (MyLabel.Text,MyLabel.Font,new SolidBrush
(Color.Black),MyLabel.ClientRectangle,sf);

You could draw the rectangle with:

e.Graphics.DrawRectangle (new
Pen(Color.Black,1),MyLabel.ClientRectangle);

But if you're using a label control it's probably better to use the
ControlPaint object:

ControlPaint.DrawBorder(e.Graphics, MyLabel.ClientRectangle,
Color.Black,ButtonBorderStyle.Solid);

Hope this helps.
- Rhy

"Sameeksha" <Sa*******@discussions.microsoft.com> wrote in message
news:C5**********************************@microsof t.com...
This can be achieved by adding an event handler to the label's paint event
Add the following code to the InitializeComponent() method
this.label1.Paint += new
System.Windows.Forms.PaintEventHandler(this.label1 _Paint);

And write code similar to the following in the paint event handler method.
You can change the brush and pen and other parameters as per requirement.
The label size should be large enough to accomodate the entire text.

private void label1_Paint(object sender, PaintEventArgs e)
{
//calculate bounds for rectangle
int rectx = 5; //margin from top and bottom of label border
int recty = 5; //margin from left and right sides of label
border
int rectWidth = label1.Width - 10;
int rectHeight = label1.Height - 10;
Graphics gr = e.Graphics;

//specify color and width of line to draw rectangle
System.Drawing.Pen pen = new
Pen(System.Drawing.Color.BlueViolet, 1.1F);
//this call draws the rectangle with specified color and line
width
gr.DrawRectangle(pen, rectx, recty, rectWidth, rectHeight);

//calculate center position for string
String s = "Label \nText";
Font f = new Font("Courier New", 12F); //font name and size
//get the size of string (width and height) in pixels, pass
string and its font as parameters
SizeF stringSize = gr.MeasureString(s,f);
//now calculte the starting point (x,y) for the string
//the formula is margin + (rectangle width - string width) / 2
float strX = rectx + (rectWidth - stringSize.Width) / 2;
float strY = recty + (rectHeight - stringSize.Height) / 2;
//choose a brush to draw the string
System.Drawing.Brush br = new
SolidBrush(System.Drawing.Color.Chocolate);
//this call draws the string at the specified position with the
chosen brush
gr.DrawString(s, f, br, strX, strY);
}

Hope this helps.
--
Sameeksha,
MCP (.NET)
"Stuart Norris" wrote:
Dear Readers,

I am attempting to draw box around some text using unicode on
multiline label. The label is forty characters wide and 12 lines deep.

I have been trying to draw a box around text (centered in the label)
on this label.
My font on this label is Courier new - hence fixed width character
cells.

I have tried using the Box Drawing unicode in with the Courier new
font, \u250c, \u2500, \u2510 etc.

this.Label.Text="\u250f\u2501\u2513\r\n\u2503 \u2503";

However there is a gap between the characters?

-----------
| Label |
-----------

Is there a way to draw a proper box around text on a label with
Courier new font using unicode?

Alternatively is it possible to draw the text labels yourself like
ownerdrawn menu buttons?

Where I calculate the width and height of the label and my text size
in the required font and then
draw a rectangle around my text in the centre of the label? Any
examples?

Any suggestion most welecome.

Stuie

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Toby Mathews | last post by:
Hi, In an ASP.Net application I want to convert open create a FileStream object from a System.Drawing.Image - is this possible? I create an instance of an Image object using the FromFile method,...
2
by: James Coburn's Grey Helmet Hair | last post by:
When I try to compile my GTK# app, the compiler says: jbailo@linux:~/mono> mcs buttons.cs -r gtk-sharp.dll -r glib-sharp.dll -r -o buttons.exe error CS2001: Source file 'buttons.exe' could not...
3
by: Paul | last post by:
Hi just wondering if there is an easy way to a box around a bunch of controls like ----------------------------------------------------- | control1 control2 control3 | | ...
2
by: John B | last post by:
I need to create a label with the last character drawn in red. My idea was to create a label and override OnPaint. So far so good. I then measure the character range for the last character. All...
3
by: John B | last post by:
I need to create a label with the last character drawn in red. My idea was to create a label and override OnPaint. So far so good. I then measure the character range for the last character. All...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
1
by: Andrew | last post by:
Hello Everyone I am receiving an error in an application I am working on. The application when its done will be a Dungeons and Dragons Network game. I am having problems with the Networked...
3
by: Rinaldo | last post by:
Hi, I have a label on my dialogbox who has to change text while running. This is what I do: lblBackup.Text = "Bezig met de backup naar " + F1.FTPserver; but the text does'nt appear, only if...
3
by: Not-So-Lazy | last post by:
Hi In delphi we have something like Labeled Edit which is basically text box which draws label side by it. Is it possible in C# when inheriting from textbox class? ( not from UserControl...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.