Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help needed with MouseDoubleClick-event of the Listview.

Posted on 2012-04-12
9
381 Views
Last Modified: 2012-04-13
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
Comment
Question by:peterkiers
  • 6
  • 3
9 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37839058
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
 
LVL 1

Author Comment

by:peterkiers
ID: 37839179
Hey Idle_Mind thanks for your solution.
I will test this tommorrow.

Greetings,

Peter Kiers
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37841346
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37842285
In OpenSticky(), did you add line #29?

            sticky.Tag = lvi; // <-- save a reference to the ListViewItem
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37842319
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 37842343
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
 
LVL 1

Author Comment

by:peterkiers
ID: 37842441
I will test it tonight because I am at work.

Peter
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37842566
Ahh, yes that was very dumb of me now I see what i have done wrong.

I will test it tonight.

Peter
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37844221
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question