By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,705 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,705 IT Pros & Developers. It's quick & easy.

Need Your Advice on "DrawString on Panel with AutoScroll"

P: n/a
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.

Here is the simplicied section of drawing code:

private void panel_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
panel.AutoScroll = true;
Font font = new System.Drawing.Font( "Arial", 18.0f );
SolidBrush brush = new System.Drawing.SolidBrush( Color.Black );
string s = "this is a very very long long long string string.";
e.Graphics.DrawString( s, font, brush, 0, 0);
}

The text did draw on the panel. However, this long string was
truncated without triggering the scrollBar.

What's wrong with my code?

Thank you for your help!
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi,

If you are going to draw manually you will need to make the needed
calculations, this include split the string as needed. you do not need to do
this I think.

I had to do something similar in a pocketPC application, I need to
dynamically insert text in a Panel, this is done by inserting Label controls
as needed (simpler than draw the string manually) being the trick
calculating the size of the label . if the panel has the autoScrool ( the CF
does not has it so another solution is needed) it will handle the scrolling
without further asistance.

The code below will show you how to do this , please note that this was
programmed to run on a Pockect PC app. maybe there is a simpler way to
calculate the size required for an a string in the desktop framework.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Label text = new Label();
text.Text = row["Text"].ToString();
text.Top = topY;
text.Width = InteriorItemsPanel.Width - 10;

SizeF size = this.MeasureStringExtend(g, text.Text, text.Font,
text.Width);
text.Height = Convert.ToInt32( size.Height+1 );

panel.Controls.Add ( text);

public SizeF MeasureStringExtend(Graphics g, string text, Font font, int
desWidth)
{
string tempString = text;
string workString = "";
int npos = 1;
int sp_pos = 0;
int line = 0;
int nWidth = 0;

//get original size
SizeF size = g.MeasureString(text, font);

if (size.Width > desWidth)
{
while(tempString.Length > 0)
{
//Check for the last lane
if (npos > tempString.Length)
{
line++;
break;
}
workString = tempString.Substring(0, npos);
//get the current width
nWidth = (int)g.MeasureString(workString, font).Width;
//check if we've got out of the destWidth
if (nWidth > desWidth)
{
//try to find a space
sp_pos = workString.LastIndexOf(" ");
if (sp_pos > 0)
{
//cut out the wrap lane we've found
tempString = tempString.Substring((sp_pos + 1), tempString.Length -
(sp_pos + 1));
line++;
npos = 0;
}
else //no space
{
tempString = tempString.Substring(npos, tempString.Length - npos);
line++;
npos = 0;
}
}
npos++;
}
return new SizeF(desWidth, (line*size.Height)) ;
}
else
return size;

}

"John" <jo***********@hotmail.com> wrote in message
news:d7**************************@posting.google.c om...
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.

Here is the simplicied section of drawing code:

private void panel_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
panel.AutoScroll = true;
Font font = new System.Drawing.Font( "Arial", 18.0f );
SolidBrush brush = new System.Drawing.SolidBrush( Color.Black );
string s = "this is a very very long long long string string.";
e.Graphics.DrawString( s, font, brush, 0, 0);
}

The text did draw on the panel. However, this long string was
truncated without triggering the scrollBar.

What's wrong with my code?

Thank you for your help!

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.