We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Winforms stack trace dialog buttons are unreliable
### Steps to reproduce

Open the `dialogs` example, and click "Stack Trace". The dialog appears, and then 1 second later, this appears in the console:
```
Task was destroyed but it is pending!
task: <Task pending name='Task-1' coro=<handler_with_cleanup() done, defined at C:\Users\smith\cygwin\git\beeware\toga\core\src\toga\handlers.py:36> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000002612A605880>()]>>
```

Click the OK button. Nothing happens.

Click the close button. The dialog closes.

This only happens the first time you open the dialog in a process. In all subsequent dialogs, the OK button closes the dialog but the close button does not.

### Expected behavior

Based on the code, it looks like the intention is that the OK button closes the dialog but the close button does not.

### Environment

- Operating System: Windows 10
- Python version: 3.8
- Software versions:
  - Toga: 4a27e6e79b39aa3120ee255b2eec1da27bdf30be

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `winforms/src/toga_winforms/dialogs.py`
Content:
```
1 import asyncio
2 from abc import ABC
3 from pathlib import Path
4 
5 import System.Windows.Forms as WinForms
6 from System.Drawing import (
7     ContentAlignment,
8     Font as WinFont,
9     FontFamily,
10     FontStyle,
11     SystemFonts,
12 )
13 from System.Windows.Forms import DialogResult, MessageBoxButtons, MessageBoxIcon
14 
15 from .libs.wrapper import WeakrefCallable
16 
17 
18 class BaseDialog(ABC):
19     def __init__(self, interface):
20         self.interface = interface
21         self.interface._impl = self
22 
23     # See libs/proactor.py
24     def start_inner_loop(self, callback, *args):
25         asyncio.get_event_loop().start_inner_loop(callback, *args)
26 
27     def set_result(self, result):
28         self.interface.set_result(result)
29 
30 
31 class MessageDialog(BaseDialog):
32     def __init__(
33         self,
34         interface,
35         title,
36         message,
37         buttons,
38         icon,
39         success_result=None,
40     ):
41         super().__init__(interface)
42 
43         def show():
44             return_value = WinForms.MessageBox.Show(message, title, buttons, icon)
45             if success_result:
46                 self.set_result(return_value == success_result)
47             else:
48                 self.set_result(None)
49 
50         self.start_inner_loop(show)
51 
52 
53 class InfoDialog(MessageDialog):
54     def __init__(self, interface, title, message):
55         super().__init__(
56             interface,
57             title,
58             message,
59             MessageBoxButtons.OK,
60             MessageBoxIcon.Information,
61         )
62 
63 
64 class QuestionDialog(MessageDialog):
65     def __init__(self, interface, title, message):
66         super().__init__(
67             interface,
68             title,
69             message,
70             MessageBoxButtons.YesNo,
71             MessageBoxIcon.Information,
72             success_result=DialogResult.Yes,
73         )
74 
75 
76 class ConfirmDialog(MessageDialog):
77     def __init__(self, interface, title, message):
78         super().__init__(
79             interface,
80             title,
81             message,
82             MessageBoxButtons.OKCancel,
83             MessageBoxIcon.Warning,
84             success_result=DialogResult.OK,
85         )
86 
87 
88 class ErrorDialog(MessageDialog):
89     def __init__(self, interface, title, message=None):
90         super().__init__(
91             interface,
92             title,
93             message,
94             WinForms.MessageBoxButtons.OK,
95             WinForms.MessageBoxIcon.Error,
96         )
97 
98 
99 class StackTraceDialog(BaseDialog):
100     def __init__(self, interface, title, message, content, retry):
101         super().__init__(interface)
102 
103         self.native = WinForms.Form()
104         self.native.MinimizeBox = False
105         self.native.FormBorderStyle = self.native.FormBorderStyle.FixedSingle
106         self.native.MaximizeBox = False
107         self.native.FormClosing += WeakrefCallable(self.winforms_FormClosing)
108         self.native.Width = 540
109         self.native.Height = 320
110         self.native.Text = title
111 
112         # The top-of-page introductory message
113         textLabel = WinForms.Label()
114         textLabel.Left = 10
115         textLabel.Top = 10
116         textLabel.Width = 520
117         textLabel.Alignment = ContentAlignment.MiddleCenter
118         textLabel.Text = message
119 
120         self.native.Controls.Add(textLabel)
121 
122         # A scrolling text box for the stack trace.
123         trace = WinForms.RichTextBox()
124         trace.Left = 10
125         trace.Top = 30
126         trace.Width = 504
127         trace.Height = 210
128         trace.Multiline = True
129         trace.ReadOnly = True
130         trace.Font = WinFont(
131             FontFamily.GenericMonospace,
132             float(SystemFonts.DefaultFont.Size),
133             FontStyle.Regular,
134         )
135         trace.Text = content
136 
137         self.native.Controls.Add(trace)
138 
139         # Add acceptance/close buttons
140         if retry:
141             retry = WinForms.Button()
142             retry.Left = 290
143             retry.Top = 250
144             retry.Width = 100
145             retry.Text = "&Retry"
146             retry.Click += WeakrefCallable(self.winforms_Click_retry)
147 
148             self.native.Controls.Add(retry)
149 
150             quit = WinForms.Button()
151             quit.Left = 400
152             quit.Top = 250
153             quit.Width = 100
154             quit.Text = "&Quit"
155             quit.Click += WeakrefCallable(self.winforms_Click_quit)
156 
157             self.native.Controls.Add(quit)
158         else:
159             accept = WinForms.Button()
160             accept.Left = 400
161             accept.Top = 250
162             accept.Width = 100
163             accept.Text = "&OK"
164             accept.Click += WeakrefCallable(self.winforms_Click_accept)
165 
166             self.native.Controls.Add(accept)
167 
168         self.start_inner_loop(self.native.ShowDialog)
169 
170     def winforms_FormClosing(self, sender, event):
171         # If the close button is pressed, there won't be a future yet.
172         # We cancel this event to prevent the dialog from closing.
173         # If a button is pressed, the future will be set, and a close
174         # event will be triggered.
175         try:
176             self.interface.future.result()
177         except asyncio.InvalidStateError:  # pragma: no cover
178             event.Cancel = True
179 
180     def set_result(self, result):
181         super().set_result(result)
182         self.native.Close()
183 
184     def winforms_Click_quit(self, sender, event):
185         self.set_result(False)
186 
187     def winforms_Click_retry(self, sender, event):
188         self.set_result(True)
189 
190     def winforms_Click_accept(self, sender, event):
191         self.set_result(None)
192 
193 
194 class FileDialog(BaseDialog):
195     def __init__(
196         self,
197         native,
198         interface,
199         title,
200         initial_directory,
201         *,
202         filename=None,
203         file_types=None,
204     ):
205         super().__init__(interface)
206         self.native = native
207 
208         self._set_title(title)
209         if filename is not None:
210             native.FileName = filename
211 
212         if initial_directory is not None:
213             self._set_initial_directory(str(initial_directory))
214 
215         if file_types is not None:
216             filters = [f"{ext} files (*.{ext})|*.{ext}" for ext in file_types] + [
217                 "All files (*.*)|*.*"
218             ]
219 
220             if len(file_types) > 1:
221                 pattern = ";".join([f"*.{ext}" for ext in file_types])
222                 filters.insert(0, f"All matching files ({pattern})|{pattern}")
223 
224             native.Filter = "|".join(filters)
225 
226         def show():
227             response = native.ShowDialog()
228             if response == DialogResult.OK:
229                 self.set_result(self._get_filenames())
230             else:
231                 self.set_result(None)
232 
233         self.start_inner_loop(show)
234 
235     def _set_title(self, title):
236         self.native.Title = title
237 
238     def _set_initial_directory(self, initial_directory):
239         self.native.InitialDirectory = initial_directory
240 
241 
242 class SaveFileDialog(FileDialog):
243     def __init__(self, interface, title, filename, initial_directory, file_types):
244         super().__init__(
245             WinForms.SaveFileDialog(),
246             interface,
247             title,
248             initial_directory,
249             filename=filename,
250             file_types=file_types,
251         )
252 
253     def _get_filenames(self):
254         return Path(self.native.FileName)
255 
256 
257 class OpenFileDialog(FileDialog):
258     def __init__(
259         self,
260         interface,
261         title,
262         initial_directory,
263         file_types,
264         multiple_select,
265     ):
266         super().__init__(
267             WinForms.OpenFileDialog(),
268             interface,
269             title,
270             initial_directory,
271             file_types=file_types,
272         )
273         if multiple_select:
274             self.native.Multiselect = True
275 
276     def _get_filenames(self):
277         if self.native.Multiselect:
278             return [Path(filename) for filename in self.native.FileNames]
279         else:
280             return Path(self.native.FileName)
281 
282 
283 class SelectFolderDialog(FileDialog):
284     def __init__(self, interface, title, initial_directory, multiple_select):
285         super().__init__(
286             WinForms.FolderBrowserDialog(),
287             interface,
288             title,
289             initial_directory,
290         )
291 
292         # The native dialog doesn't support multiple selection, so the only effect
293         # this has is to change whether we return a list.
294         self.multiple_select = multiple_select
295 
296     def _get_filenames(self):
297         filename = Path(self.native.SelectedPath)
298         return [filename] if self.multiple_select else filename
299 
300     def _set_title(self, title):
301         self.native.Description = title
302 
303     def _set_initial_directory(self, initial_directory):
304         self.native.SelectedPath = initial_directory
305
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/winforms/src/toga_winforms/dialogs.py b/winforms/src/toga_winforms/dialogs.py
--- a/winforms/src/toga_winforms/dialogs.py
+++ b/winforms/src/toga_winforms/dialogs.py
@@ -165,7 +165,10 @@
 
             self.native.Controls.Add(accept)
 
-        self.start_inner_loop(self.native.ShowDialog)
+        def show():
+            self.native.ShowDialog()
+
+        self.start_inner_loop(show)
 
     def winforms_FormClosing(self, sender, event):
         # If the close button is pressed, there won't be a future yet.

{"golden_diff": "diff --git a/winforms/src/toga_winforms/dialogs.py b/winforms/src/toga_winforms/dialogs.py\n--- a/winforms/src/toga_winforms/dialogs.py\n+++ b/winforms/src/toga_winforms/dialogs.py\n@@ -165,7 +165,10 @@\n \n             self.native.Controls.Add(accept)\n \n-        self.start_inner_loop(self.native.ShowDialog)\n+        def show():\n+            self.native.ShowDialog()\n+\n+        self.start_inner_loop(show)\n \n     def winforms_FormClosing(self, sender, event):\n         # If the close button is pressed, there won't be a future yet.\n", "issue": "Winforms stack trace dialog buttons are unreliable\n### Steps to reproduce\r\n\r\nOpen the `dialogs` example, and click \"Stack Trace\". The dialog appears, and then 1 second later, this appears in the console:\r\n```\r\nTask was destroyed but it is pending!\r\ntask: <Task pending name='Task-1' coro=<handler_with_cleanup() done, defined at C:\\Users\\smith\\cygwin\\git\\beeware\\toga\\core\\src\\toga\\handlers.py:36> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x000002612A605880>()]>>\r\n```\r\n\r\nClick the OK button. Nothing happens.\r\n\r\nClick the close button. The dialog closes.\r\n\r\nThis only happens the first time you open the dialog in a process. In all subsequent dialogs, the OK button closes the dialog but the close button does not.\r\n\r\n### Expected behavior\r\n\r\nBased on the code, it looks like the intention is that the OK button closes the dialog but the close button does not.\r\n\r\n### Environment\r\n\r\n- Operating System: Windows 10\r\n- Python version: 3.8\r\n- Software versions:\r\n  - Toga: 4a27e6e79b39aa3120ee255b2eec1da27bdf30be\n", "before_files": [{"content": "import asyncio\nfrom abc import ABC\nfrom pathlib import Path\n\nimport System.Windows.Forms as WinForms\nfrom System.Drawing import (\n    ContentAlignment,\n    Font as WinFont,\n    FontFamily,\n    FontStyle,\n    SystemFonts,\n)\nfrom System.Windows.Forms import DialogResult, MessageBoxButtons, MessageBoxIcon\n\nfrom .libs.wrapper import WeakrefCallable\n\n\nclass BaseDialog(ABC):\n    def __init__(self, interface):\n        self.interface = interface\n        self.interface._impl = self\n\n    # See libs/proactor.py\n    def start_inner_loop(self, callback, *args):\n        asyncio.get_event_loop().start_inner_loop(callback, *args)\n\n    def set_result(self, result):\n        self.interface.set_result(result)\n\n\nclass MessageDialog(BaseDialog):\n    def __init__(\n        self,\n        interface,\n        title,\n        message,\n        buttons,\n        icon,\n        success_result=None,\n    ):\n        super().__init__(interface)\n\n        def show():\n            return_value = WinForms.MessageBox.Show(message, title, buttons, icon)\n            if success_result:\n                self.set_result(return_value == success_result)\n            else:\n                self.set_result(None)\n\n        self.start_inner_loop(show)\n\n\nclass InfoDialog(MessageDialog):\n    def __init__(self, interface, title, message):\n        super().__init__(\n            interface,\n            title,\n            message,\n            MessageBoxButtons.OK,\n            MessageBoxIcon.Information,\n        )\n\n\nclass QuestionDialog(MessageDialog):\n    def __init__(self, interface, title, message):\n        super().__init__(\n            interface,\n            title,\n            message,\n            MessageBoxButtons.YesNo,\n            MessageBoxIcon.Information,\n            success_result=DialogResult.Yes,\n        )\n\n\nclass ConfirmDialog(MessageDialog):\n    def __init__(self, interface, title, message):\n        super().__init__(\n            interface,\n            title,\n            message,\n            MessageBoxButtons.OKCancel,\n            MessageBoxIcon.Warning,\n            success_result=DialogResult.OK,\n        )\n\n\nclass ErrorDialog(MessageDialog):\n    def __init__(self, interface, title, message=None):\n        super().__init__(\n            interface,\n            title,\n            message,\n            WinForms.MessageBoxButtons.OK,\n            WinForms.MessageBoxIcon.Error,\n        )\n\n\nclass StackTraceDialog(BaseDialog):\n    def __init__(self, interface, title, message, content, retry):\n        super().__init__(interface)\n\n        self.native = WinForms.Form()\n        self.native.MinimizeBox = False\n        self.native.FormBorderStyle = self.native.FormBorderStyle.FixedSingle\n        self.native.MaximizeBox = False\n        self.native.FormClosing += WeakrefCallable(self.winforms_FormClosing)\n        self.native.Width = 540\n        self.native.Height = 320\n        self.native.Text = title\n\n        # The top-of-page introductory message\n        textLabel = WinForms.Label()\n        textLabel.Left = 10\n        textLabel.Top = 10\n        textLabel.Width = 520\n        textLabel.Alignment = ContentAlignment.MiddleCenter\n        textLabel.Text = message\n\n        self.native.Controls.Add(textLabel)\n\n        # A scrolling text box for the stack trace.\n        trace = WinForms.RichTextBox()\n        trace.Left = 10\n        trace.Top = 30\n        trace.Width = 504\n        trace.Height = 210\n        trace.Multiline = True\n        trace.ReadOnly = True\n        trace.Font = WinFont(\n            FontFamily.GenericMonospace,\n            float(SystemFonts.DefaultFont.Size),\n            FontStyle.Regular,\n        )\n        trace.Text = content\n\n        self.native.Controls.Add(trace)\n\n        # Add acceptance/close buttons\n        if retry:\n            retry = WinForms.Button()\n            retry.Left = 290\n            retry.Top = 250\n            retry.Width = 100\n            retry.Text = \"&Retry\"\n            retry.Click += WeakrefCallable(self.winforms_Click_retry)\n\n            self.native.Controls.Add(retry)\n\n            quit = WinForms.Button()\n            quit.Left = 400\n            quit.Top = 250\n            quit.Width = 100\n            quit.Text = \"&Quit\"\n            quit.Click += WeakrefCallable(self.winforms_Click_quit)\n\n            self.native.Controls.Add(quit)\n        else:\n            accept = WinForms.Button()\n            accept.Left = 400\n            accept.Top = 250\n            accept.Width = 100\n            accept.Text = \"&OK\"\n            accept.Click += WeakrefCallable(self.winforms_Click_accept)\n\n            self.native.Controls.Add(accept)\n\n        self.start_inner_loop(self.native.ShowDialog)\n\n    def winforms_FormClosing(self, sender, event):\n        # If the close button is pressed, there won't be a future yet.\n        # We cancel this event to prevent the dialog from closing.\n        # If a button is pressed, the future will be set, and a close\n        # event will be triggered.\n        try:\n            self.interface.future.result()\n        except asyncio.InvalidStateError:  # pragma: no cover\n            event.Cancel = True\n\n    def set_result(self, result):\n        super().set_result(result)\n        self.native.Close()\n\n    def winforms_Click_quit(self, sender, event):\n        self.set_result(False)\n\n    def winforms_Click_retry(self, sender, event):\n        self.set_result(True)\n\n    def winforms_Click_accept(self, sender, event):\n        self.set_result(None)\n\n\nclass FileDialog(BaseDialog):\n    def __init__(\n        self,\n        native,\n        interface,\n        title,\n        initial_directory,\n        *,\n        filename=None,\n        file_types=None,\n    ):\n        super().__init__(interface)\n        self.native = native\n\n        self._set_title(title)\n        if filename is not None:\n            native.FileName = filename\n\n        if initial_directory is not None:\n            self._set_initial_directory(str(initial_directory))\n\n        if file_types is not None:\n            filters = [f\"{ext} files (*.{ext})|*.{ext}\" for ext in file_types] + [\n                \"All files (*.*)|*.*\"\n            ]\n\n            if len(file_types) > 1:\n                pattern = \";\".join([f\"*.{ext}\" for ext in file_types])\n                filters.insert(0, f\"All matching files ({pattern})|{pattern}\")\n\n            native.Filter = \"|\".join(filters)\n\n        def show():\n            response = native.ShowDialog()\n            if response == DialogResult.OK:\n                self.set_result(self._get_filenames())\n            else:\n                self.set_result(None)\n\n        self.start_inner_loop(show)\n\n    def _set_title(self, title):\n        self.native.Title = title\n\n    def _set_initial_directory(self, initial_directory):\n        self.native.InitialDirectory = initial_directory\n\n\nclass SaveFileDialog(FileDialog):\n    def __init__(self, interface, title, filename, initial_directory, file_types):\n        super().__init__(\n            WinForms.SaveFileDialog(),\n            interface,\n            title,\n            initial_directory,\n            filename=filename,\n            file_types=file_types,\n        )\n\n    def _get_filenames(self):\n        return Path(self.native.FileName)\n\n\nclass OpenFileDialog(FileDialog):\n    def __init__(\n        self,\n        interface,\n        title,\n        initial_directory,\n        file_types,\n        multiple_select,\n    ):\n        super().__init__(\n            WinForms.OpenFileDialog(),\n            interface,\n            title,\n            initial_directory,\n            file_types=file_types,\n        )\n        if multiple_select:\n            self.native.Multiselect = True\n\n    def _get_filenames(self):\n        if self.native.Multiselect:\n            return [Path(filename) for filename in self.native.FileNames]\n        else:\n            return Path(self.native.FileName)\n\n\nclass SelectFolderDialog(FileDialog):\n    def __init__(self, interface, title, initial_directory, multiple_select):\n        super().__init__(\n            WinForms.FolderBrowserDialog(),\n            interface,\n            title,\n            initial_directory,\n        )\n\n        # The native dialog doesn't support multiple selection, so the only effect\n        # this has is to change whether we return a list.\n        self.multiple_select = multiple_select\n\n    def _get_filenames(self):\n        filename = Path(self.native.SelectedPath)\n        return [filename] if self.multiple_select else filename\n\n    def _set_title(self, title):\n        self.native.Description = title\n\n    def _set_initial_directory(self, initial_directory):\n        self.native.SelectedPath = initial_directory\n", "path": "winforms/src/toga_winforms/dialogs.py"}], "after_files": [{"content": "import asyncio\nfrom abc import ABC\nfrom pathlib import Path\n\nimport System.Windows.Forms as WinForms\nfrom System.Drawing import (\n    ContentAlignment,\n    Font as WinFont,\n    FontFamily,\n    FontStyle,\n    SystemFonts,\n)\nfrom System.Windows.Forms import DialogResult, MessageBoxButtons, MessageBoxIcon\n\nfrom .libs.wrapper import WeakrefCallable\n\n\nclass BaseDialog(ABC):\n    def __init__(self, interface):\n        self.interface = interface\n        self.interface._impl = self\n\n    # See libs/proactor.py\n    def start_inner_loop(self, callback, *args):\n        asyncio.get_event_loop().start_inner_loop(callback, *args)\n\n    def set_result(self, result):\n        self.interface.set_result(result)\n\n\nclass MessageDialog(BaseDialog):\n    def __init__(\n        self,\n        interface,\n        title,\n        message,\n        buttons,\n        icon,\n        success_result=None,\n    ):\n        super().__init__(interface)\n\n        def show():\n            return_value = WinForms.MessageBox.Show(message, title, buttons, icon)\n            if success_result:\n                self.set_result(return_value == success_result)\n            else:\n                self.set_result(None)\n\n        self.start_inner_loop(show)\n\n\nclass InfoDialog(MessageDialog):\n    def __init__(self, interface, title, message):\n        super().__init__(\n            interface,\n            title,\n            message,\n            MessageBoxButtons.OK,\n            MessageBoxIcon.Information,\n        )\n\n\nclass QuestionDialog(MessageDialog):\n    def __init__(self, interface, title, message):\n        super().__init__(\n            interface,\n            title,\n            message,\n            MessageBoxButtons.YesNo,\n            MessageBoxIcon.Information,\n            success_result=DialogResult.Yes,\n        )\n\n\nclass ConfirmDialog(MessageDialog):\n    def __init__(self, interface, title, message):\n        super().__init__(\n            interface,\n            title,\n            message,\n            MessageBoxButtons.OKCancel,\n            MessageBoxIcon.Warning,\n            success_result=DialogResult.OK,\n        )\n\n\nclass ErrorDialog(MessageDialog):\n    def __init__(self, interface, title, message=None):\n        super().__init__(\n            interface,\n            title,\n            message,\n            WinForms.MessageBoxButtons.OK,\n            WinForms.MessageBoxIcon.Error,\n        )\n\n\nclass StackTraceDialog(BaseDialog):\n    def __init__(self, interface, title, message, content, retry):\n        super().__init__(interface)\n\n        self.native = WinForms.Form()\n        self.native.MinimizeBox = False\n        self.native.FormBorderStyle = self.native.FormBorderStyle.FixedSingle\n        self.native.MaximizeBox = False\n        self.native.FormClosing += WeakrefCallable(self.winforms_FormClosing)\n        self.native.Width = 540\n        self.native.Height = 320\n        self.native.Text = title\n\n        # The top-of-page introductory message\n        textLabel = WinForms.Label()\n        textLabel.Left = 10\n        textLabel.Top = 10\n        textLabel.Width = 520\n        textLabel.Alignment = ContentAlignment.MiddleCenter\n        textLabel.Text = message\n\n        self.native.Controls.Add(textLabel)\n\n        # A scrolling text box for the stack trace.\n        trace = WinForms.RichTextBox()\n        trace.Left = 10\n        trace.Top = 30\n        trace.Width = 504\n        trace.Height = 210\n        trace.Multiline = True\n        trace.ReadOnly = True\n        trace.Font = WinFont(\n            FontFamily.GenericMonospace,\n            float(SystemFonts.DefaultFont.Size),\n            FontStyle.Regular,\n        )\n        trace.Text = content\n\n        self.native.Controls.Add(trace)\n\n        # Add acceptance/close buttons\n        if retry:\n            retry = WinForms.Button()\n            retry.Left = 290\n            retry.Top = 250\n            retry.Width = 100\n            retry.Text = \"&Retry\"\n            retry.Click += WeakrefCallable(self.winforms_Click_retry)\n\n            self.native.Controls.Add(retry)\n\n            quit = WinForms.Button()\n            quit.Left = 400\n            quit.Top = 250\n            quit.Width = 100\n            quit.Text = \"&Quit\"\n            quit.Click += WeakrefCallable(self.winforms_Click_quit)\n\n            self.native.Controls.Add(quit)\n        else:\n            accept = WinForms.Button()\n            accept.Left = 400\n            accept.Top = 250\n            accept.Width = 100\n            accept.Text = \"&OK\"\n            accept.Click += WeakrefCallable(self.winforms_Click_accept)\n\n            self.native.Controls.Add(accept)\n\n        def show():\n            self.native.ShowDialog()\n\n        self.start_inner_loop(show)\n\n    def winforms_FormClosing(self, sender, event):\n        # If the close button is pressed, there won't be a future yet.\n        # We cancel this event to prevent the dialog from closing.\n        # If a button is pressed, the future will be set, and a close\n        # event will be triggered.\n        try:\n            self.interface.future.result()\n        except asyncio.InvalidStateError:  # pragma: no cover\n            event.Cancel = True\n\n    def set_result(self, result):\n        super().set_result(result)\n        self.native.Close()\n\n    def winforms_Click_quit(self, sender, event):\n        self.set_result(False)\n\n    def winforms_Click_retry(self, sender, event):\n        self.set_result(True)\n\n    def winforms_Click_accept(self, sender, event):\n        self.set_result(None)\n\n\nclass FileDialog(BaseDialog):\n    def __init__(\n        self,\n        native,\n        interface,\n        title,\n        initial_directory,\n        *,\n        filename=None,\n        file_types=None,\n    ):\n        super().__init__(interface)\n        self.native = native\n\n        self._set_title(title)\n        if filename is not None:\n            native.FileName = filename\n\n        if initial_directory is not None:\n            self._set_initial_directory(str(initial_directory))\n\n        if file_types is not None:\n            filters = [f\"{ext} files (*.{ext})|*.{ext}\" for ext in file_types] + [\n                \"All files (*.*)|*.*\"\n            ]\n\n            if len(file_types) > 1:\n                pattern = \";\".join([f\"*.{ext}\" for ext in file_types])\n                filters.insert(0, f\"All matching files ({pattern})|{pattern}\")\n\n            native.Filter = \"|\".join(filters)\n\n        def show():\n            response = native.ShowDialog()\n            if response == DialogResult.OK:\n                self.set_result(self._get_filenames())\n            else:\n                self.set_result(None)\n\n        self.start_inner_loop(show)\n\n    def _set_title(self, title):\n        self.native.Title = title\n\n    def _set_initial_directory(self, initial_directory):\n        self.native.InitialDirectory = initial_directory\n\n\nclass SaveFileDialog(FileDialog):\n    def __init__(self, interface, title, filename, initial_directory, file_types):\n        super().__init__(\n            WinForms.SaveFileDialog(),\n            interface,\n            title,\n            initial_directory,\n            filename=filename,\n            file_types=file_types,\n        )\n\n    def _get_filenames(self):\n        return Path(self.native.FileName)\n\n\nclass OpenFileDialog(FileDialog):\n    def __init__(\n        self,\n        interface,\n        title,\n        initial_directory,\n        file_types,\n        multiple_select,\n    ):\n        super().__init__(\n            WinForms.OpenFileDialog(),\n            interface,\n            title,\n            initial_directory,\n            file_types=file_types,\n        )\n        if multiple_select:\n            self.native.Multiselect = True\n\n    def _get_filenames(self):\n        if self.native.Multiselect:\n            return [Path(filename) for filename in self.native.FileNames]\n        else:\n            return Path(self.native.FileName)\n\n\nclass SelectFolderDialog(FileDialog):\n    def __init__(self, interface, title, initial_directory, multiple_select):\n        super().__init__(\n            WinForms.FolderBrowserDialog(),\n            interface,\n            title,\n            initial_directory,\n        )\n\n        # The native dialog doesn't support multiple selection, so the only effect\n        # this has is to change whether we return a list.\n        self.multiple_select = multiple_select\n\n    def _get_filenames(self):\n        filename = Path(self.native.SelectedPath)\n        return [filename] if self.multiple_select else filename\n\n    def _set_title(self, title):\n        self.native.Description = title\n\n    def _set_initial_directory(self, initial_directory):\n        self.native.SelectedPath = initial_directory\n", "path": "winforms/src/toga_winforms/dialogs.py"}]}