I am trying to save a form location to a database whenever the LocationChanged event is triggered on that form.

private void DisplayForm_LocationChanged(object sender, EventArgs e)
        {
            DSLocation = this.Location;
            ((Form1)this.MdiParent).NotifyChanges(this); // notify parent form that there are changes on this child
        }
childForm.Location = StringToPoint((string)row["location"]);

DSLocation is a property that I'm trying to fill with data from this.Location in order to pass it back to the parent form and ultimately the database. Whenever I try to compile, I get the error: Cannot implicitly convert type 'System.Drawing.Point' to 'string'

I know that I need to somehow break the coordinates coming from this.Location into two pieces and convert them into int and then recombine them into a string, but I have no idea how to go about this. My searches have only found tutorials and sites that talk about going from String to Point, not the reverse... Using those tutorials and examples I was able to get the coordinates formatted coming from the database:

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);
        }

...but I am not nearly knowledgeable enough to make the reverse happen.

Any of you bright people know how to juggle backwards?
-Charlie

Recommended Answers

All 2 Replies

If I understand you, you would like to remember the X,Y position of the windows form.
So when the form is re-opened, it has to appear on the same spot as it was last time, righ?

If so, here is an exmaple how to get the points and insert them into DB:

private void SavingCoordinatesIntoDB()
        {
            int[] XY = GetFormCoordinates();
            using (SqlConnection sqlConn = new SqlConnection("yourConnectionString"))
            {
                string insertString = "INSERT INTO .... VALUES(@x, @y)";
                using (SqlComand cmd = new SqlCommand(insertString, sqlConn))
                {
                    cmd.Parameters.Add("@x", SqlDbType.Int).Value = XY[0];
                    cmd.Parameters.Add("@y", SqlDbType.Int).Value = XY[1];
                    sqlConn.Open();
                    cmd.ExecuteNonQuery();
                    sqlConn.Close();
                    //you can put upper line into try, catch statement, to capture the errors (if any occures).
                }
            }
        }


        private int[] GetFormCoordinates()
        {
            int deskWidth = Screen.PrimaryScreen.Bounds.Width;
            int deskHeight = Screen.PrimaryScreen.Bounds.Height;

            int appWidth = this.Width;
            int appHeight = this.Height;

            int spX = (deskWidth - appWidth) / 2;
            int spY = (deskHeight - appHeight) / 2;

            return new int[] { spX, spY };
        }

Hope it helps, and this is what you meant. If now, let me know, I`ll try to help you...
bye
Mitja

Mitja, you're solution is awesome and it does work. But it also grabs more information than I need.

I ended up going with the bare minimum to make it work (to keep my code small)

DSLocation = String.Format("{0},{1}", this.Location.X, this.Location.Y);

Thank you for pointing me in the right direction!
-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.