Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-04-10
6
Medium Priority
?
316 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 600 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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 1400 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

730 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