Solved

Help needed with MouseDoubleClick-event of the Listview.

Posted on 2012-04-12
9
383 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
[X]
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
  • 6
  • 3
9 Comments
 
LVL 86

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 86

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 86

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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