0

I have a WPF window and I'm calling it from a C++/cli code and it works fine. What I need is what's the best way to intercept user action I mean whether he click OK or Cancel. Wath I think to do is to define a Boolean property in my window and set it depends on user action. Here is my code :

MyWindowView^ window = gcnew MyWindowView();
System::Windows::Interop::WindowInteropHelper^ windowInteropHelper = gcnew System::Windows::Interop::WindowInteropHelper(window);
windowInteropHelper->Owner = (IntPtr)AfxGetMainWnd()->m_hWnd;
window->WindowStartupLocation = System::Windows::WindowStartupLocation::CenterScreen;
window->ShowDialog();
if ()
{
    //some action
} 
else
{
    //
}

Also I want to know do I need to delete the window object ?

MRebai
  • 5,344
  • 3
  • 33
  • 52

1 Answers1

4

Window.ShowDialog returns dialog result. I can't see any reason to get this result in some way, that differs from C# or VB .NET:

System::Nullable<System::Boolean> result = window->ShowDialog();
if (result.HasValue)
{
    // OK or Cancel
    if (result.Value)
    {
        // OK clicked
    }
    else
    {
        // Cancel clicked
    }
}
else
{
    // dialog closed via system menu or Alt+F4
}

do I need to delete the window object ?

No, you don't. See this answer.

Community
  • 1
  • 1
Dennis
  • 37,026
  • 10
  • 82
  • 150
  • Hi Dennis actually for the Ok and Cancel buttons are custom not the defined ones for dialog result so do you think that ShowDialog can get the result of that buttons – MRebai Dec 01 '15 at 08:33
  • 1
    @MoezRebai: what do you mean by "custom buttons"? Anyway, if your buttons will set `Window.DialogResult` property, you'll get a proper return value. – Dennis Dec 01 '15 at 08:34