Drawstring?

If you need help with a project or need to know how to do something specific in VB.NET then please ask your questions in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
5 posts Page 1 of 1
Contributors
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Drawstring?
Dummy1912
Hello,

after a long search i didn't found any solution
can somebody helpme to split the long text to show at 2 lines
only when its to large
Code: Select all
fnt = New Font("Arial", 7)
            Dim descSize As SizeF = e.Graphics.MeasureString(tr(i).trDescr, fnt)
Dim replaceDesc As String
            replaceDesc = tr(i).trDescr
            replaceDesc = Strings.Replace(replaceDesc, " ", vbCrLf) 'example from google

 If replaceDesc.Length > 70 Then
e.Graphics.DrawString(replaceDesc , fnt, Brushes.Black, (sSize.Width * 26 + sPoint.X) - (strSize.Width + sSize.Width * 26) / 80, (sSize.Height / 5 + sPoint.Y + 200), SF)
Else
e.Graphics.DrawString(tr(i).trDescr, fnt, Brushes.Black, (sSize.Width * 26 + sPoint.X) - (strSize.Width + sSize.Width * 26) / 80, (sSize.Height / 5 + sPoint.Y + 200), SF)
 End If

problem is that it split almost every word :(
i just want to split the line that has more then 70 length of text

anyone?

Thanks
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Drawstring?
CodenStuff
I found this annoying to do when using drawstring myself in the past and I'm still not sure how to do it in a fool proof way.

I did a bit of searching and found this function that may help point you in the right direction:
Code: Select all
private void panel1_Paint(object sender, PaintEventArgs e)
{
  string header2 = "This is a much, much longer Header";
  string description = "This is a description of the header.";

  RectangleF header2Rect = new RectangleF();
  using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Bold))
  {
    header2Rect.Location = new Point(30, 105);
    header2Rect.Size = new Size(600, ((int)e.Graphics.MeasureString(header2, useFont, 600, StringFormat.GenericTypographic).Height));
    e.Graphics.DrawString(header2, useFont, Brushes.Black, header2Rect);
  }

  RectangleF descrRect = new RectangleF();
  using (Font useFont = new Font("Gotham Medium", 28, FontStyle.Italic))
  {
    descrRect.Location = new Point(30, (int)header2Rect.Bottom);
    descrRect.Size = new Size(600, ((int)e.Graphics.MeasureString(description, useFont, 600, StringFormat.GenericTypographic).Height));
    e.Graphics.DrawString(description.ToLower(), useFont, SystemBrushes.WindowText, descrRect);
  }

}
Hope it helps :duh;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Drawstring?
Dummy1912
yea i found this also
but didn't worked what i needed :(

i didn't get it at the location i wanted my text to show
or didn't cut the text or even showed it completed.

sad to say i also find it annoying :waaa;


Edit:

i did try this and seems it cut the text but don't show the completed string :( at the second line
any ideas?
Code: Select all
Dim descSize As SizeF = e.Graphics.MeasureString(tr(i).trDescr, fnt)
                Dim Rectf As RectangleF = New Rectangle(sSize.Width * 25.2 + sPoint.X - sSize.Width / 2, sSize.Height / 5 + sPoint.Y + 200, descSize.Width / 2, sSize.Height)
                e.Graphics.DrawString(tr(i).trDescr, fnt, Brushes.Black, Rectf, SF)

if i use this
Code: Select all
                Dim Rectf As RectangleF = New Rectangle(sSize.Width * 25.2 + sPoint.X - sSize.Width / 2, sSize.Height / 5 + sPoint.Y + 200, descSize.Width / 2, descSize.Height / 2)

it seems it cut off the string from the bottom lol

also tried:
Code: Select all
                    Dim Rectf As RectangleF = New Rectangle(sSize.Width * 25.2 + sPoint.X - sSize.Width / 2, sSize.Height / 5 + sPoint.Y + 200, descSize.Width - sPoint.X, sSize.Height)
but it seems it shows 2 lines of string but one of the strings on the previous line don't show the completed string :(
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Drawstring?
CodenStuff
Where or on what are you drawing these strings. Can you show us a picture or something?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
Dummy1912
VIP - Donator
VIP - Donator
Posts: 1969
Joined: Sat Aug 21, 2010 2:17 pm

Re: Drawstring?
Dummy1912
just to print a page

Edit:

We have finally found the solution
we played with the code and come up with this
Code: Select all
replaceDesc = Strings.Replace(replaceDesc, vbNewLine, " ")
                    Dim Rectf As RectangleF = New Rectangle(sSize.Width * 25.2 + sPoint.X - sSize.Width / 2, sSize.Height / 5 + sPoint.Y + 200, descSize.Width - sPoint.X - 95, sSize.Height + 5)
                    e.Graphics.DrawString(replaceDesc, fnt, Brushes.Black, Rectf, SF)
and seems it did the trick

Thanks for the support ;)
visit us on:


http://www.softpedia.com/get/System/Lau ... -Run.shtml
Check it out ! http://www.softpedia.com/publisher/I-A- ... 90017.html
Check it out ! http://www.softpedia.com/get/Desktop-En ... lock.shtml
5 posts Page 1 of 1
Return to “Coding Help & Support”