Solved

Drawing to and resizing Windows Form object

Posted on 2011-02-28
5
346 Views
Last Modified: 2012-05-11
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
Comment
Question by:redman07
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35001400
Can we see some code?

How about starting with the drawing portion?
0
 

Author Comment

by:redman07
ID: 35002317
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 35002880
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
 

Author Closing Comment

by:redman07
ID: 35003041
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35003063
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question