[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Ignore duplicates names in the Listview.

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
peterkiers
Asked:
peterkiers
  • 4
  • 2
1 Solution
 
Miguel OzSoftware EngineerCommented:
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
 
peterkiersAuthor Commented:
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
 
peterkiersAuthor Commented:
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
peterkiersAuthor Commented:
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
 
Miguel OzSoftware EngineerCommented:
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
 
peterkiersAuthor Commented:
AH, super you have really helped me. Thank you very much.

Peter Kiers
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now