Solved

Ignore duplicates names in the Listview.

Posted on 2014-02-04
6
222 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
[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
  • 4
  • 2
6 Comments
 
LVL 36

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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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 36

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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

724 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