Home > Blockchain >  Winform dialog show modal form wait for Dialogresult
Winform dialog show modal form wait for Dialogresult

Time:09-20

I need to use "Show()" method for dialog winform using Ironpython
and i need to get result of button pressing and use it in another part of program
how can i wait for pressing OK in modal "Show()" dialog?

mform = Form3()
mform.Show()
# How to wait for button OK

Thx

CodePudding user response:

Use ShowDialog() instead of Show(). Then check "if (mform.DialogResult == DialogResult.OK)"

CodePudding user response:

Suppose you have a main form called Form1 and a secondary form called AddForm which presents some result when the [OK] button is pressed.

The main form then can subscribe to the FormClosed event of the secondary form and pull the result if the user clicked on the [OK] button.

This can all happen in the subroutine where the secondary form is shown

Private Sub Button1.Click(sender as Object, e as EventArgs) Handles Button1.Click
  Dim dlg as New AddForm
  AddHandler dlg.FormClosed, _
    Sub(e,ev) TextBox1.Text = If(dlg.DialogResult = DialogResult.OK, dlg.Result, String.Empty)
  dlg.Show() 
End Sub

And in the secondary form make sure there is a property called Result which contains the results

The [OK] button has a handler like so

Private Sub Button1_Click(sender as Object, e as EventArgs) Handles Button1.Click
  Result = ...
  DialogResult = DialogResult.OK
  Me.Close()
End Sub

and the [Cancel] button has a handler as so

Private Sub Button2_Click(sender as Object, e as EventArgs) Handles Button2.Click
  DialogResult = DialogResult.Cancel
  Me.Close()
End Sub
  • Related