Solved

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

Posted on 2012-04-10
6
308 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
  • 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
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
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 85

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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)…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

708 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

12 Experts available now in Live!

Get 1:1 Help Now