I am trying to pull coordinates for a form location out of a database and insert them into the Location property:

childForm.Location = new Point(/* coordinates go here */);

I have tried doing a direct replacement with the DataSet (which didn't work):

childForm.Location = new Point((string)row["location"]);

And I have tried using a string to hold the coordinates:

string Loc = (string)row["location"];
childForm.Location = new Point(Loc);

...which also didn't work. If I print 'Loc' to the screen, I can see it passing valid coordinates (in this case "100, 200" and "300, 600"), but it errors out when I try to pass that information to childForm.Location.

Obviously I'm doing something very wrong here, but I was unable to find any examples of using non-static or non-relative coordinates. If you know, could you please tell me what my mistake is?

Thanks!
-Charlie

Dani AI

Generated

Short summary: was right to turn the stored string into a Point — splitting the comma and parsing integers solves the immediate problem. A few practical refinements and alternatives make that approach safer and easier to maintain.

A clean, built-in alternative is the PointConverter accessed via TypeDescriptor: it converts the common "x, y" text form to a System.Drawing.Point and back, so you can serialize with the converter and read it back without writing your own parser. Example (WinForms/System.Drawing):

using System.ComponentModel;
using System.Drawing;

if (row["location"] != DBNull.Value) {
    string text = ((string)row["location"]).Trim();
    var conv = TypeDescriptor.GetConverter(typeof(Point));
    childForm.Location = (Point)conv.ConvertFromString(text);
}

PointConverter expects the culture list-separator (eg. "200, 200" in en-US) so use the converter’s invariant methods or explicitly trim/normalise the string if your app runs under different locales. (learn.microsoft.com)

Caveats and edge cases: System.Drawing.Point.ToString() returns a brace format like "{X=100,Y=200}" which PointConverter.ConvertFromString does not accept, so don’t store Point.ToString() directly if you expect to parse it back later — either use the PointConverter to produce the storable string or handle the brace format with a custom parser. If you’re in WPF, System.Windows.Point exposes a Parse method you can use instead. (gist.github.com)

Practical tips: always check for DBNull/null, Trim the string, prefer int.TryParse (or ConvertFromInvariantString) to avoid exceptions, and fail gracefully if parsing fails. For long-term storage and querying, store X and Y as numeric columns (or use DB spatial types) rather than ad-hoc comma strings — it’s simpler and far less brittle. (stackoverflow.com)

Problem solved! In case someone else needs a hint in the future, here's the code I used (thanks for the help, Chris!):

private Point StringToPoint(string str)
        {
            string[] s = str.Split(',');
            int x = Int32.Parse(s[0]);
            int y = Int32.Parse(s[1]);
            return new Point(x, y);
        }

And later:

childForm.Location = StringToPoint((string)row["location"]);

-Charlie

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.