Save and load formsize/position when using multiple forms.

Peter Kiers
Peter Kiers used Ask the Experts™
on
Dear Experts,

In my previous question I have asked how to load and save the size and location
of multiple forms into listview columns called lvcSize and lvcLocation.

For the saving-part I have received this solution:

=> myItem.SubItems.Add(sticky.Size.ToString());
=> myItem.SubItems.Add(sticky.Location.ToString());

And I have added these lines above to my code below and
and marked them with //ADD

For the loading-part I received this:

Then to convert those strings back later, use the respective Size.Parse() and Point.Parse() methods:
http://msdn.microsoft.com/en-us/library/system.windows.size.parse(VS.85).aspx
http://msdn.microsoft.com/en-us/library/system.windows.point.parse.aspx

But I don't know how to implement the loading-part solution into my code below.
The only thing I know (I think) is that the methode OpenSticky does the loading of
the StickyNotes-form, so the loading-solution has to be implemented in this methode
Because I have difficulty to understand the code. Can someone help me to implement
the loading-part-solution?

   //*------------------------------------------------------------*//
        private void tbNewNote_ItemClick(object sender, ItemClickEventArgs e)
        {
            StickyNotes sticky = new StickyNotes();
            sticky.lblTitle.Text = "";
            sticky.rtbContent.Clear();
            sticky.lblCreation.Text = (DateTime.Now.ToString());
            sticky.FormClosed += new FormClosedEventHandler(sticky_FormClosed);
            sticky.Show();
        }
        //*------------------------------------------------------------*//
        void sticky_FormClosed(object sender, FormClosedEventArgs e)
        {
            StickyNotes sticky = (StickyNotes)sender;
            ListViewItem myItem = new ListViewItem(sticky.lblTitle.Text, 0);
            myItem = lvNotes.Items.Add(myItem);
            myItem.SubItems.Add(sticky.rtbContent.Text);
            myItem.SubItems.Add(sticky.lblCreation.Text);
            myItem.SubItems.Add(sticky.Size.ToString());       //ADD
            myItem.SubItems.Add(sticky.Location.ToString());     //ADD
            myItem.Selected = true;
        }
        //*------------------------------------------------------------*//
        private void lvNotes_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            OpenSticky(lvNotes.SelectedItems[0]);
        }
        //*------------------------------------------------------------*//
        private void OpenSticky(ListViewItem lvi)
        {
            string Title = lvi.Text;
            string Content = lvi.SubItems[1].Text;
            string Creation = lvi.SubItems[2].Text;
            StickyNotes sticky = null;
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is StickyNotes)
                {
                    sticky = (StickyNotes)frm;
                    if (sticky.lblTitle.Text == Title)
                    {
                        if (sticky.WindowState == FormWindowState.Minimized)
                        {
                            sticky.WindowState = FormWindowState.Normal;
                        }
                        sticky.BringToFront();
                        sticky.Select();
                        return;
                    }
                }
            }
            sticky = new StickyNotes();
            sticky.lblTitle.Text = Title;
            sticky.rtbContent.Text = Content;
            sticky.lblCreation.Text = Creation;
            sticky.Tag = lvi;
            sticky.FormClosed += new FormClosedEventHandler(EditSticky_FormClosed);
            sticky.Show();
        }
        //*------------------------------------------------------------*//
        void EditSticky_FormClosed(object sender, FormClosedEventArgs e)
        {
            StickyNotes sticky = (StickyNotes)sender;
            ListViewItem myItem = (ListViewItem)sticky.Tag;
            myItem.Text = sticky.lblTitle.Text;
            myItem.SubItems[1].Text = sticky.rtbContent.Text;
            myItem.SubItems[2].Text = sticky.lblCreation.Text;
            myItem.SubItems[3].Text = (sticky.Size.ToString());    //ADD
            myItem.SubItems[4].Text = (sticky.Location.ToString());    //ADD
            myItem.Selected = true;
        }

Open in new window



Greetings,

Peter Kiers
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
We'll need to store the Location/Size differently using this code instead:
        void EditSticky_FormClosed(object sender, FormClosedEventArgs e)
        {
            StickyNotes sticky = (StickyNotes)sender;
            ListViewItem myItem = (ListViewItem)sticky.Tag;
            myItem.Text = sticky.lblTitle.Text;
            myItem.SubItems[1].Text = sticky.rtbContent.Text;
            myItem.SubItems[2].Text = sticky.lblCreation.Text;
            myItem.SubItems[3].Text = (sticky.Size.Width.ToString() + ", " + sticky.Size.Height.ToString());    //ADD
            myItem.SubItems[4].Text = (sticky.Location.X.ToString() + ", " + sticky.Location.Y.ToString());    //ADD
            myItem.Selected = true;
        }

Open in new window


Then, when you open this sticky again, you can do:
        private void OpenSticky(ListViewItem lvi)
        {
            string Title = lvi.Text;
            string Content = lvi.SubItems[1].Text;
            string Creation = lvi.SubItems[2].Text;
            string Size = lvi.SubItems[3].Text; // ADD
            string Location = lvi.SubItems[4].Text; // ADD

            StickyNotes sticky = null;
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is StickyNotes)
                {
                    sticky = (StickyNotes)frm;
                    if (sticky.lblTitle.Text == Title)
                    {
                        if (sticky.WindowState == FormWindowState.Minimized)
                        {
                            sticky.WindowState = FormWindowState.Normal;
                        }
                        sticky.BringToFront();
                        sticky.Select();
                        return;
                    }
                }
            }
            sticky = new StickyNotes();
            sticky.lblTitle.Text = Title;
            sticky.rtbContent.Text = Content;
            sticky.lblCreation.Text = Creation;
            sticky.Tag = lvi;
            sticky.FormClosed += new FormClosedEventHandler(EditSticky_FormClosed);
            
            // add begin
            sticky.StartPosition = FormStartPosition.Manual;
            System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Point));
            sticky.Location = (Point)converter.ConvertFromString(Location);
            converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Size));
            sticky.Size = (Size)converter.ConvertFromString(Size);
            // add end

            sticky.Show();
        }

Open in new window

Peter KiersOperator

Author

Commented:
Thanks Idle_Mind.

500 points are comming to you...

Greetings,

Peter Kiers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial