Solved

Double click on listview row opens the same forms more than ones. Part2

Posted on 2012-04-10
6
315 Views
Last Modified: 2012-04-11
Hi,

Hi,

I am programming in C# and I use VS2010.

I have a mainform with a listview called lvNotes on it. And a button for
displaying another form called StickyNotes. On the StickyNotes there
is a textbox where the user can put some text when he/she closes the
form the text of the textbox will be displayed in a new row in the listview.

The code looks like this:

        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)
        {
            StickyNotes sticky = new StickyNotes();
            sticky.lblTitle.Text = lvNotes.SelectedItems[0].Text;
            sticky.rtbContent.Text = lvNotes.SelectedItems[0].SubItems[1].Text;
            sticky.lblCreation.Text = lvNotes.SelectedItems[0].SubItems[2].Text;
            sticky.Show();
        }

Open in new window


But when the user doubleclicks on the row of the listview the stickynotes form
appears again. But when the user clicks again on the same row the stickynotes
form appears twice. So I have to tell in the doubleclick-event that If the user
doubleclicks on the listview row and the Sticky Notes form of that row the user
is clicking on is still open/active then ignore.

How can I do that?

Peter
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
  • 3
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 37831335
Wasn't it solved with the previous question ? What did you already try and what is the issue you're still having ?


I see 2 possibilities:
1. Check if the form is already open:
http://hashfactor.wordpress.com/2009/01/28/c-check-if-a-form-is-already-opened/

2. Move a little bit in the code


        public StickyNotes sticky ;
        private void tbNewNote_ItemClick(object sender, ItemClickEventArgs e)
        {
            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)
        {
            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;
           sticky  = null;
        }
        //*------------------------------------------------------------*//
        private void lvNotes_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            sticky = new StickyNotes();
            sticky.lblTitle.Text = lvNotes.SelectedItems[0].Text;
            sticky.rtbContent.Text = lvNotes.SelectedItems[0].SubItems[1].Text;
            sticky.lblCreation.Text = lvNotes.SelectedItems[0].SubItems[2].Text;
            sticky.Show
        }
0
 
LVL 1

Author Comment

by:peterkiers
ID: 37831354
Hi Dhaest,

I have tried your altered code but still have the same problem.
I didn't tried the first possibility. Because I don't know how to
implement that code.

As you can see in the example.jpg I have still the same problem.
When I click on the icon of the listview more than ones the form
also appear more than ones.

Peter
Example.jpg
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 150 total points
ID: 37832015
I've done some testing. Try this code:
Do you always just want 1 stickynote open or can it be a stickynote (for each lblTitle) as in example ?

        private void lvNotes_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (Application.OpenForms.OfType<StickyNotes>().Where(x => x.lblTitle.Text == lvNotes.SelectedItems[0].Text).Count() == 0)
            {
                StickyNotes sticky = new StickyNotes();
                sticky.lblTitle.Text = lvNotes.SelectedItems[0].Text;
                sticky.rtbContent.Text = lvNotes.SelectedItems[0].SubItems[1].Text;
                sticky.lblCreation.Text = lvNotes.SelectedItems[0].SubItems[2].Text;
                sticky.Show();
            }
            else
            {
                StickyNotes sticky = (StickyNotes)Application.OpenForms.OfType<StickyNotes>().Where(x => x.lblTitle.Text == lvNotes.SelectedItems[0].Text).First();
                sticky.Activate();
            }
        }
0
Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

 
LVL 1

Author Comment

by:peterkiers
ID: 37832559
Hi,

I want my StickyNotes behave like Microsoft Outlook does. So it is possible. When a user press the new button, the StickyNotes form apears (and for every newly created StickyNotes form a new row will be created in the listview. So, if the user pressed the new button the first StickyNotes form appears, if the user presses the new button again a second StickyNotes form appears, but the first StickyNotes form will be minimalized automaticly.
etc...And the user can display multiple forms by doubleclick on the StickyNotes forms that are minimized. This works super. And this behaviour is what Microsoft Outlook does too.

But when you doubleclick on an icon (row) of the listview in Outlook. And the StickyNotes form is allready minimized. Outlook displays the minimized StickyNotes form. In my case
the programm created a second StickyNotes form !!!

I have test the code above from Dhaest.its not 100% but I think where on the right track.
It doesn't display the minimized form as normal, and when I put text into the textbox
of the stickynotes form the text isn't saved anymore. Can I have the code translated because I have no idea what it does!

Peter Kiers
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 350 total points
ID: 37832792
Try this:
        private void lvNotes_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            OpenSticky(lvNotes.SelectedItems[0]);
        }

        private void OpenSticky(ListViewItem lvi)
        {
            // extract info from ListViewItem
            string Title = lvi.Text;
            string Content = lvi.SubItems[1].Text;
            string Creation = lvi.SubItems[2].Text;

            // see if it's already open
            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;
                    }
                }
            }

            // it wasn't found, so create a new one
            sticky = new StickyNotes();
            sticky.lblTitle.Text = Title;
            sticky.rtbContent.Text = Content;
            sticky.lblCreation.Text = Creation;
            sticky.Show();
        }

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 37832824
Thanks Idle_mind this is the right solution. And also thanks Dhaest for responding.

Peter
0

Featured Post

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

627 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