Save and load formsize/position when using multilpe forms.

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

I am programming in C# and I use VS2010.

I want to make an application just like the Notes-part of Microsoft Outlook.
So I have a mainForm with a Listview on it called lvNotes and a button. When
a user presses that button a form appears called StickyNotes that has a text-
box on it. When the user put some text in it and presses the close-button of
the StickyNotes form a new row will be created in the Listview with the text
that the user had entered in the textbox.

Thanks to the solution of Idle_mind I now have the same form-mechanisme that
Microsoft Outlook has.

Now I have added 2 more listview-columns to the listview called lvcSize and
lvcLocation to save and load each size and location of the StickyNotes-form
the user creates. Because this code is to sophisticated for me I hope someone
can help me with this final step.

 //*------------------------------------------------------------*//
        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.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.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®
Peter KiersOperator

Author

Commented:
Hi,

Is it possible to store the size and location of the StickyNotes form to the listview columns
called Size and Location.

Like this:

        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); <======================
            myItem.SubItems.Add(sticky.Location); <=======================

        }

Open in new window


But I get errors like:

1. The best overloaded method match for 'System.Windows.Forms.ListViewItem.ListViewSubItemCollection.Add(string)' has some invalid arguments      
2. cannot convert from 'System.Drawing.Size' to 'string'      

Peter
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Call ToString() on your Size/Location properties:

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

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
Peter KiersOperator

Author

Commented:
Thanks Idle_mind.

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