Solved

Drawing to and resizing Windows Form object

Posted on 2011-02-28
5
342 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
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
Can we see some code?

How about starting with the drawing portion?
0
 

Author Comment

by:redman07
Comment Utility
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 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
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
Comment Utility
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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now