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

Need Your Advice on "DrawString on Panel with AutoScroll"

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
1 9326
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: adolf garlic | last post by:
Currently we have an app (asp.net 1.1) that has a dynamic content client scripted treeview which will allow the display of reporting services reports in the right hand part of the screen. My...
0
by: VB Programmer | last post by:
I wanted to access some properties of a panel control that is on a different form. So, I went to the (Declarations) section of my main form (the form with the panel) and changed the panel (called...
1
by: Edward | last post by:
When designing a page in VS2003, in the toolbox, tab HTML there was a "Grid Layout Panel". I cannot find this control in VS2005. Where can I find the control now?
3
by: Tom Bianchi | last post by:
Hi, I have a Panel with the AutoScroll property set to True and many TextBoxes into it, with only some of them positioned in the visible portion of the panel. Is there a Panel event that I can...
1
by: TheSteph | last post by:
Hi ! I would like to create a UserControl that act as a « Collapsible Panel ». So I have a UserControl with two panels : a "Header panel" at the top, and a "Container Area Panel"...
1
by: Jefferds.A | last post by:
Hi, I'm putting together a little database of individuals categorized by family. I have two lists, one that contains family names and one that contains individuals' names. When you click on a link...
5
by: Curious | last post by:
I installed a service program. It contained executables in the form of .exe and .dll files. And the service was started automatically after the install. Then, I UN-installed the program, but the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.