We help IT Professionals succeed at work.

Save/load formsize/position when using multiple forms. Part2

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

Yesterday I have asked how to save and load the size and position when using
multiple forms.

I got a good solution but closed the question to early. Because after testing it
I get this error:

690, 361 is not a valid value for Int32 in the methode OpenSticky
at the line that I have marked with an arrow.

    //*------------------------------------------------------------*//
        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.Width.ToString() + ", " + sticky.Size.Height.ToString());  
            myItem.SubItems.Add(sticky.Location.X.ToString() + ", " + sticky.Location.Y.ToString());   
            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;
            string Size = lvi.SubItems[3].Text; 
            string Location = lvi.SubItems[4].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.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);
            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.Width.ToString() + ", " + sticky.Size.Height.ToString());    
            myItem.SubItems[4].Text = (sticky.Location.X.ToString() + ", " + sticky.Location.Y.ToString());   
            myItem.Selected = true;
        }

Open in new window


Who can help me to fix this problem?


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:
It works fine for me in testing:
        private void button1_Click(object sender, EventArgs e)
        {
            string Location = "690, 361";
            string Size = "400, 300";

            StickyNotes sticky = new StickyNotes();
            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);

            sticky.Show();
        }

Open in new window


Not sure where the failure is coming from.

Perhaps the values in "Location" and "Size" are not what you think they are when the error is occurring?
Peter KiersOperator

Author

Commented:
Thank you for the information.

Greetings,

Peter Kiers