[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Drawing to and resizing Windows Form object

Posted on 2011-02-28
5
Medium Priority
?
365 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

650 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