Solved

Ignore duplicates names in the Listview.

Posted on 2014-02-04
6
210 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

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

Join & Write a Comment

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)…
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 …
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

23 Experts available now in Live!

Get 1:1 Help Now