Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

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

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
peterkiers
Asked:
peterkiers
  • 3
  • 2
2 Solutions
 
DhaestCommented:
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
 
peterkiersAuthor Commented:
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
 
DhaestCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
peterkiersAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
peterkiersAuthor Commented:
Thanks Idle_mind this is the right solution. And also thanks Dhaest for responding.

Peter
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now