• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

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!
0
redman07
Asked:
redman07
  • 3
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can we see some code?

How about starting with the drawing portion?
0
 
redman07Author Commented:
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

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's probably your use of "m_oformGraphics".  You should be using the Graphics supplied in the Paint() event via "e.Graphics".

I'm guessing you used "this.CreateGraphics" and assigned that to the variable?
*That's rarely the right approach...

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

   this.Refresh(); // <-- Force a Repaint
} // KeyPress()

protected void EdntrChildForm_Paint( object s, PaintEventArgs e )
{
   // Use "e.Graphics" to draw with:
   e.Graphics.FillRectangle(
      new SolidBrush( SystemColors.Window ),
      new RectangleF( new Point( 0, 0 ),
         m_oformGraphics.MeasureString( m_sText, m_oFont, Point.Empty,
         StringFormat.GenericTypographic ) ) );
   e.Graphics.DrawString( m_sText, m_oFont,
      new SolidBrush( SystemColors.WindowText ), 0, 0,
      StringFormat.GenericTypographic );
} // Paint()

Open in new window

0
 
redman07Author Commented:
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.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now