473,323 Members | 1,547 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,323 software developers and data experts.

Need to clip strings when drawing on a panel

As a simple example suppose I want to draw two columns of strings.

I know where each column starts and the widths.

So before I print a string I measure it and if it is longer than the column
width I remove a character at the end and measure it again. I repeat this
until the string is not longer than the desired width.

I'm drawing the strings on panel and wonder if there is a faster way.

Thanks
Oct 22 '07 #1
5 1253
"Academia" <ac************@a-znet.comschrieb
As a simple example suppose I want to draw two columns of strings.

I know where each column starts and the widths.

So before I print a string I measure it and if it is longer than
the column width I remove a character at the end and measure it
again. I repeat this until the string is not longer than the desired
width.

I'm drawing the strings on panel and wonder if there is a faster
way.

You can use an overloaded DrawString version specifiying the
"LayoutRectangle" and formatting options:

Dim rect As RectangleF
Dim sf As StringFormat

'--- 50 is the column width ---
rect = New RectangleF(30, 40, 50, 0)
sf = New StringFormat(StringFormatFlags.NoWrap)

'optional:
'sf.Trimming = StringTrimming.None

e.Graphics.DrawRectangle(Pens.Blue, Rectangle.Round(rect))
e.Graphics.DrawString( _
"very long text that does not fit into the column", _
Panel1.Font, Brushes.Black, rect, sf _
)
Armin

Oct 22 '07 #2
"Armin Zingler" <az*******@freenet.deschrieb
e.Graphics.DrawRectangle(Pens.Blue, Rectangle.Round(rect))
Leave out this line. Was for testing only.
Armin
Oct 22 '07 #3
Thanks I'll try it now.

BTW - does Rectangle.Round(rect)) draw the rectangle inside the space
defined?

for example, suppose the pen width is 3 pixels wide, what gets drawn where?
I'm assuming the width can be in pixels. I've just looked at the Pen doc and
it doesn't give a clue as to the units for the width.

Thanks a lot
"Armin Zingler" <az*******@freenet.dewrote in message
news:ub**************@TK2MSFTNGP04.phx.gbl...
"Armin Zingler" <az*******@freenet.deschrieb
> e.Graphics.DrawRectangle(Pens.Blue, Rectangle.Round(rect))

Leave out this line. Was for testing only.
thanks for going to all that trouble!
>

Armin

Oct 22 '07 #4
"Academia" <ac************@a-znet.comschrieb
Thanks I'll try it now.

BTW - does Rectangle.Round(rect)) draw the rectangle inside the
space defined?
Rectangle.Round converts a RectangleF to a Rectangle, rounding the
left/top/width/height values.
for example, suppose the pen width is 3 pixels wide, what gets drawn
where?
I'm assuming the width can be in pixels. I've just looked at the Pen
doc and it doesn't give a clue as to the units for the width.

I don't know.
Armin

Oct 22 '07 #5
thanks for the help

"Armin Zingler" <az*******@freenet.dewrote in message
news:ef**************@TK2MSFTNGP05.phx.gbl...
"Academia" <ac************@a-znet.comschrieb
>Thanks I'll try it now.

BTW - does Rectangle.Round(rect)) draw the rectangle inside the
space defined?

Rectangle.Round converts a RectangleF to a Rectangle, rounding the
left/top/width/height values.
>for example, suppose the pen width is 3 pixels wide, what gets drawn
where?
I'm assuming the width can be in pixels. I've just looked at the Pen
doc and it doesn't give a clue as to the units for the width.


I don't know.
Armin

Oct 23 '07 #6

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

Similar topics

21
by: | last post by:
Hi, I am setting the NumericUpDown .Value property and the ValueChanged event is NOT being fired. Does this ONLY get fired when I change it on the UI and not programatically? Thanks
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
1
by: John | last post by:
I'm trying to use the DrawText() method to draw some very long string text on the Panel with AutoScroll enabled. However, for some unknown reasons, I could not trigger the ScrollBar to show up. ...
3
by: Martin | last post by:
I'm trying to paint my own caption bar/title bar and also my own frame border, but I'm having problems with modifying the clip region so windows doesn't paint over my stuff when I call base.WndProc...
3
by: B Maxey | last post by:
I am storing images in a database and need a way to browse those files. I can load the files into an image list or whatever the control needs, but I need a control. It doesn't seem that any of...
2
by: Justin Lazanowski | last post by:
I have a form that programmatticly creates 5 labels. Then programatticly should dispose them, when this happens when it gets down to two lables left it says that I get a null ref exception.. and I...
7
by: Galen Somerville | last post by:
I'm doing something wrong. I made up a test app and put the ZIP file on my website http://home.surewest.net/galen/index.html under Downloads. Basically I have a Panel that will get continuous...
1
by: MrNobody | last post by:
I want to have a boxed off area on my GUI which will behave essentially like a very simple ListBox, with items stacking vertically in a list with a scroll bar if the stacked items go beyond the...
0
by: GamingGeek | last post by:
Alright, I'm working on a project for class that I need to use both .gifs and .wavs. I've gotten the .wavs to work, but some of the gifs bring up a GDI+ error when I run the program. Here's the error...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.