1

I am looking for a solution to not close my dialog window when there is a duplicate ID. I guess it has to do with DialogResult, however, I cannot make neither true or false work.

I am opening the "New Store"-window from a button click in my main window.

NewStoreWindow newStore = new NewStoreWindow();
newStore.ShowDialog();

As you can see on the screenshots below, you can fill in the ID and the Name. I have successfully made an input validation, so you cannot press "OK" when the 2 textboxes are empty.

App

When you press "OK" and the ID is already in my listview (gridview), it will give the user the below error message. However, it will also close the "New Store"-window. As said, I would like that the window is kept open until it successfully is added, so that the user can just edit the ID instead of having to open the "New Store"-window again and typing it again.
It should only close the window when it is successfully added or cancel/X is pressed. I have tried to play around with closing-event for "New Store", but it does not seem to work.

Error message

Is this just the designed behavior? And is there a way to bypass/work around it?

CS

public partial class NewStoreWindow : Window
        {

        public static bool itemAlreadyAdded;
        //public MainWindow mainWin = new MainWindow();

        public bool IsDefault { get; set; }

        public NewStoreWindow()
        {
            InitializeComponent();

            //SetButtonState();
        }

        // Data Binding
        public static readonly DependencyProperty SidProperty = DependencyProperty.Register("Sid", typeof(string), typeof(NewStoreWindow), new UIPropertyMetadata(String.Empty));
        public string Sid
        {
            get { return (string)GetValue(SidProperty); }
            set { SetValue(SidProperty, value); }
        }

        // Data Binding
        public static readonly DependencyProperty SNameProperty = DependencyProperty.Register("SName", typeof(string), typeof(NewStoreWindow), new UIPropertyMetadata(String.Empty));
        public string SName
        {
            get { return (string)GetValue(SNameProperty); }
            set { SetValue(SNameProperty, value); }
        }

private void cmdOK_Click(object sender, RoutedEventArgs e)
        {


            DialogResult = true;

            MainWindow mainWin = new MainWindow();

            // Check if any items exist in listview
            if (MainWindow._list.Count == 0)
            {
                MainWindow._list.Add(new MainWindow.Data() { Sid = Sid, SName = SName });
            }
            else // items exist
            {
                itemAlreadyAdded = false; // use for boolean checking
                foreach (var item in mainWin.lvStores.Items.OfType<MainWindow.Data>()) // loop through all items in listview
                {


                    if (item.Sid == Sid) // Check if new item already exists
                    {
                        itemAlreadyAdded = true;
                    }

                    if (itemAlreadyAdded) // Show messagebox if it exists
                    {
                        MessageBox.Show("ID needs to be unique, please respecify!", "Duplicate ID",
                            MessageBoxButton.OK, MessageBoxImage.Error);

                        break;
                    }
                }

                if (!itemAlreadyAdded) // If it does not already exist, add it
                {
                    // if it does not exist, create it from the textbox values
                    MainWindow._list.Add(new MainWindow.Data() { Sid = Sid, SName = SName });

                    // Refresh listview
                    mainWin.lvStores.Items.Refresh();
                   // Close Window
                   Close();
                }
            }  
        }

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;
    this.Hide();

}

public void Close()
{
    this.Closing -= Window_Closing;
    base.Close();
}
Niclas
  • 1,069
  • 4
  • 18
  • 33
  • where is you code for the Ok button event the window should not close itself unless you are telling it to close when you click OK – npo Aug 08 '17 at 09:38
  • @user5195490 updated with code now. I do not have the `Closing` in my XAML, because it will not work properly. The SName will not get read from the `textbox`, when I do so. – Niclas Aug 08 '17 at 09:43
  • Why are you calling this.Hide() in the Window_Closing event handler? – mm8 Aug 08 '17 at 10:13
  • @mm8 to see if I could keep the values there. But it doesn't matter if I have it there or not. – Niclas Aug 08 '17 at 10:53

0 Answers0