문제

The following code is taken from a measureup test for the 70-536 Microsoft .NET exam.

private void frmMain_Paint(object sender, PaintEventArgs e)
{
    System.ComponentModel.TypeConverter rectConverter =
        System.ComponentModel.TypeDescriptor.GetConverter(typeof(Rectangle));

    Rectangle rect = (Rectangle)rectConverter.ConvertFromString("50,50,200,200"); //fails
    e.Graphics.DrawRectangle(Pens.Black, rect);
    rect.Inflate(-10, -10);
    e.Graphics.DrawRectangle(Pens.Blue, rect);
}

But the line

Rectangle rect = (Rectangle)rectConverter.ConvertFromString("50,50,200,200");

fails with "50,50,200,200" is not a valid value for Int32". I am not familiar with the TypeConverter class at all. But according to the explanation, this was supposed to draw a black rectangle at the coordinate (50,50) with the size (200,200). Then draw a new smaller blue rectangle inside the black one by using the Inflate method. But why does ConvertFromString fail?

edit:

I guess this is the .NET implementation of the ConvertFromString method regarding the Rectangle class:

http://www.dotnetframework.org/default.aspx/DotNET/DotNET/8@0/untmp/whidbey/REDBITS/ndp/fx/src/CommonUI/System/Drawing/RectangleConverter@cs/1/RectangleConverter@cs

도움이 되었습니까?

해결책

Yeah, I can't reproduce it, but don't get stuck on that minor detail, looks like you're trying to figure out a bigger problem than that, just make the rectangle work and return to this later if it keeps bugging you. Something like this should work..

rect.Height = 200;
rect.Width = 200;
rect.PointToScreen(new Point(50, 50));

다른 팁

Try ConvertFromInvariantString().

I think this is a localization problem. I tried this on a computer that displays numbers as 1,23 rather than 1.23 and I got the same problem

 (Rectangle)r.ConvertFromString(null,
                    new System.Globalization.CultureInfo("en-US"),"0,0,23,50");

works though

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top