From: Tony Johansson on 11 Apr 2010 12:11 Hi! When I run the code below I get runtime error saying 10,10,100,200 is not a valid value for Int32. But I mean this method ConvertFromString can take a string and convert that to a rectangle that's what the docs say So is there a bug or have I missunderstood this ConvertFromString ? protected override void OnPaint(PaintEventArgs e) { string rectInput = "10,10,100,200"; Graphics g = e.Graphics; RectangleConverter converter = new RectangleConverter(); Rectangle outerRect = (Rectangle) converter.ConvertFromString(rectInput); Rectangle innerRect = new Rectangle(outerRect.X + 2, outerRect.Y + 2, outerRect.Width - 4, outerRect.Height - 4); Region reg = new Region(innerRect); g.DrawRectangle(Pens.Black, outerRect); g.FillRegion(Brushes.AliceBlue, reg); } //Tony //Tony
From: Martin Honnen on 11 Apr 2010 12:53 Tony Johansson wrote: > Hi! > > When I run the code below I get runtime error saying > 10,10,100,200 is not a valid value for Int32. > But I mean this method ConvertFromString can take a string and convert that > to a rectangle that's what the docs say > So is there a bug or have I missunderstood this ConvertFromString ? > > protected override void OnPaint(PaintEventArgs e) > { > string rectInput = "10,10,100,200"; > Graphics g = e.Graphics; > RectangleConverter converter = new RectangleConverter(); > Rectangle outerRect = (Rectangle) > converter.ConvertFromString(rectInput); It might depend on the current CultureInfo how that string is parsed respectively what format is expected. Does using ConvertFromInvariantString give you the result you want? -- Martin Honnen --- MVP Data Platform Development http://msmvps.com/blogs/martin_honnen/
From: Tony Johansson on 11 Apr 2010 13:17 "Martin Honnen" <mahotrash(a)yahoo.de> skrev i meddelandet news:%23zRqOeZ2KHA.5400(a)TK2MSFTNGP02.phx.gbl... > Tony Johansson wrote: >> Hi! >> >> When I run the code below I get runtime error saying >> 10,10,100,200 is not a valid value for Int32. >> But I mean this method ConvertFromString can take a string and convert >> that to a rectangle that's what the docs say >> So is there a bug or have I missunderstood this ConvertFromString ? >> >> protected override void OnPaint(PaintEventArgs e) >> { >> string rectInput = "10,10,100,200"; >> Graphics g = e.Graphics; >> RectangleConverter converter = new RectangleConverter(); >> Rectangle outerRect = (Rectangle) >> converter.ConvertFromString(rectInput); > > It might depend on the current CultureInfo how that string is parsed > respectively what format is expected. Does using > ConvertFromInvariantString give you the result you want? > > > -- > > Martin Honnen --- MVP Data Platform Development > http://msmvps.com/blogs/martin_honnen/ If I use ConvertFromInvariantString it works but I mean a rectangle always look the same no matter what culture we use. //Tony
From: Tony Johansson on 11 Apr 2010 13:53 "Martin Honnen" <mahotrash(a)yahoo.de> skrev i meddelandet news:%23zRqOeZ2KHA.5400(a)TK2MSFTNGP02.phx.gbl... > Tony Johansson wrote: >> Hi! >> >> When I run the code below I get runtime error saying >> 10,10,100,200 is not a valid value for Int32. >> But I mean this method ConvertFromString can take a string and convert >> that to a rectangle that's what the docs say >> So is there a bug or have I missunderstood this ConvertFromString ? >> >> protected override void OnPaint(PaintEventArgs e) >> { >> string rectInput = "10,10,100,200"; >> Graphics g = e.Graphics; >> RectangleConverter converter = new RectangleConverter(); >> Rectangle outerRect = (Rectangle) >> converter.ConvertFromString(rectInput); > > It might depend on the current CultureInfo how that string is parsed > respectively what format is expected. Does using > ConvertFromInvariantString give you the result you want? > > > -- > > Martin Honnen --- MVP Data Platform Development > http://msmvps.com/blogs/martin_honnen/ If I use ConvertFromInvariantString it works but I mean a rectangle always look the same no matter what culture we use. //Tony
From: Peter Duniho on 11 Apr 2010 14:13 Tony Johansson wrote: > If I use ConvertFromInvariantString it works but I mean a rectangle always > look the same no matter what culture we use. Ah, but that's where you're wrong. A rectangle doesn't "look" like _anything_ until you render it somehow. Typically, you render it graphically, which produces something that has the geometric shape of the rectangle you're rendering. Even there, there will be differences depending on whether you render it with a blank pen or a red one, with a 1 pixel pen or a 10 pixel one, etc. You could even describe those differences as "cultural". But in the case of the string, you're dealing with a rectangle that has been rendered textually. And that rendering depends very much on the current culture. How things look as text is one of the most significant things culture settings on the computer give us. The rectangle your string "rectInput" attempts to describe can be stored in a string in a variety of ways: "10,10,100,200" "10, 10, 100, 200" "10.10.100.200" "{10,10,100,200}" "10;10;100;200" etc. (�and all of the above examples assume a specific order of the location and size of the rectangle, while even that is not guaranteed; I could just as easily store a rectangle as text by writing the size first, then the location, or any other arrangement). The RectangleConverter class can only successfully "un-render" a string describing a rectangle if it knows what format was used to create the string in the first place. And it does that by looking at the current culture, or using the invariant culture if you call the ConvertFromInvariantString() method instead. As it happens, the string you're using matches the format expected by the invariant culture. So it works when you use the ConvertFromInvariantString() method. You could also get it to work by determining what your current culture is and figuring out what format that culture expects (though, if you're doing this for serialization purposes, the invariant culture is actually a more appropriate choice anyway). Don't confuse the rectangle � that is, the abstract idea of a four-sided polygon that has 90-degree angles at each corner � with the various ways such a rectangle can be represented. And don't forget that text is just as valid a representation as anything else, and text _does_ depend very much on culture. Pete
|
Next
|
Last
Pages: 1 2 Prev: about painting where I don't specify a rectangle Next: PGP keys in C# app |