Solved

Help needed with MouseDoubleClick-event of the Listview.

Posted on 2012-04-12
9
378 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
Comment Utility
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
Comment Utility
Hey Idle_Mind thanks for your solution.
I will test this tommorrow.

Greetings,

Peter Kiers
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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
Comment Utility
In OpenSticky(), did you add line #29?

            sticky.Tag = lvi; // <-- save a reference to the ListViewItem
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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
Comment Utility
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
Comment Utility
I will test it tonight because I am at work.

Peter
0
 
LVL 1

Author Comment

by:peterkiers
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

17 Experts available now in Live!

Get 1:1 Help Now