Link to home
Start Free TrialLog in
Avatar of redman07
redman07

asked on

Drawing to and resizing Windows Form object

I have a Form object that is the child of another Form object.  The child form captures key strokes and places them in a string object.  Using Graphics.DrawString() to display the contents of the string in the child form works fine, until the child form is resized to be wider than its initial width.  After that, DrawString ceases to display the part of the string that is beyond the child form's original width.  Any ideas?  I am displaying the string in the upper-left corner of the child form, from left to right.

Thanks!
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Can we see some code?

How about starting with the drawing portion?
Avatar of redman07
redman07

ASKER

Here are the places where the DrawString happens.  I've removed some non-essential code that deals with the caret and some command keys.

private void EdntrChildForm_KeyPress(object sender, KeyPressEventArgs e)
{
   switch ( e.KeyChar )
   {
      case '\b':
      case '\r':
      case '\n':
         break;
      default:			
         m_sText += e.KeyChar;
   }

   m_oformGraphics.FillRectangle(
      new SolidBrush( SystemColors.Window ),
      new RectangleF( new Point( 0, 0 ),
         m_oformGraphics.MeasureString( m_sText, m_oFont, Point.Empty,
         StringFormat.GenericTypographic ) ) );

   m_oformGraphics.DrawString( m_sText, m_oFont,
      new SolidBrush( SystemColors.WindowText ), 0, 0,
      StringFormat.GenericTypographic );

} // KeyPress()

protected void EdntrChildForm_Paint( object s, PaintEventArgs e )
{

   m_oformGraphics.DrawString( m_sText, m_oFont,
      new SolidBrush( SystemColors.WindowText ),
      0, 0 ), StringFormat.GenericTypographic );

} // Paint()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's it.  I did use CreateGraphics().  Thanks for the insightful solution, and the tip!

Now if I can just find out how to get characters to display in a consistently spaced-out manner, and place the caret between them accordingly.   I remember in the old-days that GetTextExtent2 did a great job of helping with that kind of thing.
You can use a monospaced font...

Or possibly use the Graphics.MeasureCharacterRanges() function to get bounding rectangles for each letter in your string:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.measurecharacterranges.aspx