Solved

Help needed with MouseDoubleClick-event of the Listview.

Posted on 2012-04-12
9
379 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now