1

I've a UserControl and I need to get input from a dialog. However, when I try to do this:

        AddPassword addPassword = new AddPassword();

        if(addPassword.ShowDialog() == == DialogResult.GetValueOrDefault())
        {
            if (addPassword.sTitle != ""
                && addPassword.sUser != ""
                && addPassword.sPass != "")
            {
                LogIn login = new LogIn(addPassword.sTitle, addPassword.sUser, addPassword.sPass);
                LogIns.Add(login);

                lstbxLogins.Items.Add(login.GetTitle());
            }
        }

It doesn't find the DialogResult.

Can I actually do this?

Thanks

HoBa
  • 3,442
  • 5
  • 26
  • 33
  • 1
    If you're going to rank down a new user at least have the courtesy to say why. +1 – 4imble Mar 16 '11 at 09:21
  • What do you mean "doesn't find the DialogResult"? You cannot compile this or you don't get expected result from "addPassword.ShowDialog()"? – zendar Mar 16 '11 at 09:35
  • Doesn't compile when I use DialogResult.GetValueOrDefault(). Now, if I just do this if-statement if(addPassword.ShowDialog()), the values I get back from the window are null. – HoBa Mar 16 '11 at 23:54

1 Answers1

1

Method ShowDialog returns bool?, so you should check the result like this:

if(addPassword.ShowDialog().GetValueOrDefault())
{
   ...
}
Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
  • If I use that, I get an exception, InvalidOperationException: Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed. – HoBa Mar 16 '11 at 21:59
  • @Homero - This means that you are calling ShowDialog() after the window is closed (are you calling it multiple times on the same instance of the window?). But I don't see it in the code you provided, so the problem is probably with something else. – Pavlo Glazkov Mar 17 '11 at 07:17