Mysterious exception during OnTextChanged and OnPaint.

T

Tom P.

I am controlling the painting of my custom control. When the text
changes I grow the control and then repaint it accordingly.

I have several other processes that are running on independent threads
but they are updating objects in the listview, not my custom control.

I am getting "System.ArgumentException: Parameter is not valid."
during the following two lines (in two different methods no less):

OnTextChanged:
int StringWidth = CurrentGraphic.MeasureString(this.Text,
this.Font).ToSize().Width;

OnPaint:
pevent.Graphics.DrawString(this.Text, this.Font, Brushes.Black,
this.ClientRectangle.X, this.ClientRectangle.Y + 4);

But when the Debugger stops the code everything I point to has value.
How do I find my problem and what could be causing it yet not show up
in the debugger?

Should I not nest? Do each step one by one?

Thanks for anything.
Tom P.
 
M

Marc Bernard

I am controlling the painting of my custom control. When the text
changes I grow the control and then repaint it accordingly.

I have several other processes that are running on independent threads
but they are updating objects in the listview, not my custom control.

I am getting "System.ArgumentException: Parameter is not valid."
during the following two lines (in two different methods no less):

OnTextChanged:
int StringWidth = CurrentGraphic.MeasureString(this.Text,
this.Font).ToSize().Width;

OnPaint:
pevent.Graphics.DrawString(this.Text, this.Font, Brushes.Black,
this.ClientRectangle.X, this.ClientRectangle.Y + 4);

But when the Debugger stops the code everything I point to has value.
How do I find my problem and what could be causing it yet not show up
in the debugger?

Should I not nest? Do each step one by one?

Thanks for anything.
Tom P.


I ran into this myself just this week. I had disposed a font, and the
"parameter is not valid" refers to the font being used in the
MeasureString.

http://nomagichere.blogspot.com/2009/04/sawing-off-branch-i-sitting-on.html

There may be other ways to get to the same exception, but that did the
trick for me.

Marc
 
T

Tom P.

I ran into this myself just this week.  I had disposed a font, and the
"parameter is not valid" refers to the font being used in the
MeasureString.

http://nomagichere.blogspot.com/2009/04/sawing-off-branch-i-sitting-o...

There may be other ways to get to the same exception, but that did the
trick for me.

Marc

I'll have to check it out when I get home (can't get to blogs from
work).

But, that helpped me figure out that it was the font. Then I tried to
create a Clone of the font and it failed too.

Finally I created a new font from the old font and the
FontType.Regular and it works fine now. I wonder what made this crop
up all of the sudden. I've been using this app for about three months
now and this just now started showing up. Probably some 3.5 upgrade I
didn't know about.

Thanks for the help,
Tom P.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top