Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

Help needed with MouseDoubleClick-event of the Listview.

Hi,

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. But it has one little bug in it.

When I create a new StickyNotes-form and put f.e. "Hello" in. And then close
the StickyNotes-form the "Hello" will be written to the listview. This works
OK. But when I doubleclick on the icon of the listview to display the Sticky-
Notes-form again with the word "Hello", and change the text f.e. to "Hello You".
And closes and reopen the Sticky-Notes form, it displays just "Hello". So the
text is not written back to the listview when the user doubleclicks on the
icon of the listview and then closes it.

So, there is something not correct in the MouseDoubleClick-event or the FormClose-
event from the StickyNotes-form.

        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.Show();
        }
        //*------------------------------------------------------------*//

Open in new window


Who knows the answer and is willing to help me?


Greetings,


Peter Kiers
0
peterkiers
Asked:
peterkiers
  • 6
  • 3
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Use the same technique of handling the FormClosed() event just like you did with a new sticky...

See if this works:
        //*------------------------------------------------------------*//
        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; // <-- save a reference to the ListViewItem
            sticky.FormClosed += new FormClosedEventHandler(EditSticky_FormClosed); // subscribe to the FormClosed() event
            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


Note the addition of lines #29 and #30 before the Show() call, along with the new EditSticky_FormClosed() method.
0
 
peterkiersAuthor Commented:
Hey Idle_Mind thanks for your solution.
I will test this tommorrow.

Greetings,

Peter Kiers
0
 
peterkiersAuthor Commented:
Hi Idle_mind i get an error: "Object reference not set to an instance of an object"
at the line that i have marked with an arrow.

        void sticky_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


Peter
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
In OpenSticky(), did you add line #29?

            sticky.Tag = lvi; // <-- save a reference to the ListViewItem
0
 
peterkiersAuthor Commented:
Yes, I have copied your code into my application.


I have only changed this line:
   sticky.FormClosed += new FormClosedEventHandler(EditSticky_FormClosed);
to this:
   sticky.FormClosed += new FormClosedEventHandler(sticky_FormClosed);
 
for the rest the code is the same you posted.

Peter Kiers
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Ah...there was already a sticky_FormClosed() which had a different purpose with a NEW StickyNote.

I had created a new method, EditSticky_FormClosed(), specifically for dealing with the EDITING of an existing Note.

So you need both the old sticky_FormClosed() from your original post, and my new EditSticky_FormClosed() method.

Hopefully that fixes it...
0
 
peterkiersAuthor Commented:
I will test it tonight because I am at work.

Peter
0
 
peterkiersAuthor Commented:
Ahh, yes that was very dumb of me now I see what i have done wrong.

I will test it tonight.

Peter
0
 
peterkiersAuthor Commented:
I have tested the code and it works super.

thank you idle_mind.

500 points are comming your way...

Greetings,

Peter Kiers
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now