Solved

Ignore duplicates names in the Listview.

Posted on 2014-02-04
6
214 Views
Last Modified: 2014-02-05
Hi,

I have a Listview with several columns where the user can insert data through an input-form. After filling in the textboxes that resides on the input-form the user closes the input-form and a new row will be created filled with the data of the textboxes. If the user wants to edit the new created row the user just doubleclick on the row and the input-form appears again with the data in the textboxes. Everything works great. Only the user can open the input-form multiple times. When the user doubleclicks on a row the input-form appears. When the user doubleclicks again on the same row I get the same input-form twice !!!

I have though about it how to solve this problem. And i think I got it but don't know if its possible doing it in code. every inputform has an unique date. Is it possible to say in the
DoubleClick event that when the user doubleclick on the row and the input-form of that certain row is allready opened dan ignore. Doing this by compare the date in the row that the user doubleclicks on with the dates on the allready opened input-forms.

        private void lvNotes_DoubleClick(object sender, EventArgs e)
        {
            OpenSticky(lvNotes.SelectedItems[0]);
        }

Who can help me?
Greetings, Peter Kiers

P.S: I have but all the code in a textfile.
0
Comment
Question by:peterkiers
  • 4
  • 2
6 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 39834250
Is this a desktop app? (winform or WPF)
If so, you need to open your input form as a modal form (so that the user cannot go back to the list view until it closes your input form)
For winform, please use ShowDialog method

MSDN definition: "A modal form or dialog box must be closed or hidden before you can continue working with the rest of the application."

Note: If further help needed please post your code how the input form is setup and displayed.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39834268
Hi, changing my input-form as a modal form is not the right solution. I am making a program for sticky notes (just what Microsoft Outlook has). In outlook the user can open multiple notes and that's just what I have. Only Outlook ignores the opening of a note that is allready opened. And I don't know how to do this. Peter

 private void bbNewNote_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            StickyNotes sticky = new StickyNotes();
            sticky.gpclCaption.Text = "New Note";
            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.gpclCaption.Text, 0);
            myItem = lvNotes.Items.Add(myItem);
            myItem.SubItems.Add(sticky.lblCreation.Text);
            myItem.SubItems.Add(DateTime.Now.ToString());
            myItem.SubItems.Add(sticky.rtbContent.Rtf);
            myItem.SubItems.Add(sticky.Size.Width.ToString() + ", " + sticky.Size.Height.ToString());
            myItem.SubItems.Add(sticky.Location.X.ToString() + ", " + sticky.Location.Y.ToString());
            myItem.SubItems.Add(sticky.BackColor.R.ToString() + ";" + sticky.BackColor.G.ToString() + ";" + sticky.BackColor.B.ToString());
            myItem.SubItems.Add(sticky.Opacity.ToString());
            this.lvNotes.SelectedItems.Clear();
            myItem.Selected = true;
            RefreshListView();
            SortListView();
        }

        private void lvNotes_DoubleClick(object sender, EventArgs e)
        {
            OpenSticky(lvNotes.SelectedItems[0]);
        }

        private void OpenSticky(ListViewItem lvi)
        {
            string Title = lvi.Text;
            string Creation = lvi.SubItems[1].Text;
            string Modified = lvi.SubItems[2].Text;
            string Content = lvi.SubItems[3].Text;
            string Size = lvi.SubItems[4].Text;
            string Location = lvi.SubItems[5].Text;
            string[] backColor = lvi.SubItems[6].Text.Split(';');
            string myOpacity = lvi.SubItems[7].Text;
            StickyNotes sticky = null;
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is StickyNotes)
                {
                    sticky = (StickyNotes)frm;
                    if (sticky.gpclCaption.Text == String.Empty)
                    {
                        if (sticky.WindowState == FormWindowState.Minimized)
                        {
                            sticky.WindowState = FormWindowState.Normal;
                        }
                        sticky.BringToFront();
                        sticky.Select();
                        return;
                    }
                }
            }
            sticky = new StickyNotes();
            sticky.gpclCaption.Text = Title;
            sticky.lblCreation.Text = Creation;
            sticky.rtbContent.Text = Content;
            sticky.Tag = lvi;
            sticky.FormClosed += new FormClosedEventHandler(EditSticky_FormClosed);
            sticky.StartPosition = FormStartPosition.Manual;
            System.ComponentModel.TypeConverter converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Point));
            sticky.Location = (Point)converter.ConvertFromString(null, System.Globalization.CultureInfo.InvariantCulture, Location);
            converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(Size));
            sticky.Size = (Size)converter.ConvertFromString(null, System.Globalization.CultureInfo.InvariantCulture, Size);
            sticky.BackColor = Color.FromArgb(int.Parse(backColor[0]), int.Parse(backColor[1]), int.Parse(backColor[2]));
            sticky.Opacity = Convert.ToDouble(myOpacity);
            sticky.Show();
        }

        void EditSticky_FormClosed(object sender, FormClosedEventArgs e)
        {
            StickyNotes sticky = (StickyNotes)sender;
            ListViewItem myItem = (ListViewItem)sticky.Tag;
            myItem.Text = sticky.gpclCaption.Text;
            myItem.SubItems[1].Text = sticky.lblCreation.Text;
            myItem.SubItems[2].Text = (DateTime.Now.ToString());
            myItem.SubItems[3].Text = (sticky.rtbContent.Rtf);
            myItem.SubItems[4].Text = (sticky.Size.Width.ToString() + ", " + sticky.Size.Height.ToString());
            myItem.SubItems[5].Text = (sticky.Location.X.ToString() + ", " + sticky.Location.Y.ToString());
            myItem.SubItems[6].Text = (sticky.BackColor.R.ToString() + ";" + sticky.BackColor.G.ToString() + ";" + sticky.BackColor.B.ToString());
            myItem.SubItems[7].Text = (sticky.Opacity.ToString());
            this.lvNotes.SelectedItems.Clear();
            myItem.Selected = true;
            RefreshListView();
            SortListView();
        }

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 39834548
Is this possible (in pseudo code)

      private void lvNotes_DoubleClick(object sender, EventArgs e)
        {
       if listview[6] equals lblCreation.txt of all open forms then return
            OpenSticky(lvNotes.SelectedItems[0]);
        }
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 1

Author Comment

by:peterkiers
ID: 39837646
I have tried it myself but I get an error:

private void lvNotes_DoubleClick(object sender, EventArgs e)
{
StickyNotes sticky = new StickyNotes();
ListViewItem item = this.lvNotes.Items[1];
foreach(Form StickyNotes in Application.OpenForms)
{
if (sticky.lblCreation.Text != item.ToString())
{
OpenSticky(lvNotes.SelectedItems[0]);
}
}
}

Open in new window


But I get error: Collection was modified; enumeration operation may not execute.

Who can help me to fix this problem?

Peter
0
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 39837764
How do you load the listview? Have the got a unique identifier?
If so, it could be feasible to keep a list of open items?
Pseudo code:
//When the form is setup or loaded the first time
private List<string> opened = new List<string>();
 
private void lvNotes_DoubleClick(object sender, EventArgs e)
 {
    string selectedId = Get UniqueId associated lvNotes.SelectedItems[0]      
    if opened.Contains(selectedId)  then return
    opened.Add(selectedId )
    OpenSticky(lvNotes.SelectedItems[0]);
 }

//when closing sticky forms, remove the associated UniqueId from opened.
Note: Another option: Create a property InUse (boolean) in your listview associated objects and set it to true when open in sticky node and false when closed then you can check at double click if the sticky item is opened before calling OpenSticky.
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39837807
AH, super you have really helped me. Thank you very much.

Peter Kiers
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with deleting records with two duplicate columns using VB.NET 12 43
Memory Usage 2 50
Entity Framework 7 31
Javascript and Jquery not firing 9 39
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

21 Experts available now in Live!

Get 1:1 Help Now