How to attend click button even on to in order to don't close form called by ShowDialog?

  • Thread starter Thread starter £ukasz Margielewski
  • Start date Start date
£

£ukasz Margielewski

I have my own windows form with some contrlos to set a few parameters.
I show this form by frm.ShowDialog(). How I have to proram the button which
sometimes should close form but in another time, the buuton should give a
messagebox (with warning message: "Some parameters are incorect") and not to
close form, but continue form's focus to correct thiese parameters?

I tried to set the button.dialogresult = dialogresult.None, ut it doesn't
work. The form has been still closing...
Sorry for my english, but I'm beginner ;-)
 
you need to set the DialogResult of the form (not button) to None.
In your button event handler just write,
this.DialogResult = DialogResult.None

--Saurabh
 
£ukasz Margielewski said:
I have my own windows form with some contrlos to set a few parameters.
I show this form by frm.ShowDialog(). How I have to proram the button which
sometimes should close form but in another time, the buuton should give a
messagebox (with warning message: "Some parameters are incorect") and not to
close form, but continue form's focus to correct thiese parameters?

I tried to set the button.dialogresult = dialogresult.None, ut it doesn't
work. The form has been still closing...
Sorry for my english, but I'm beginner ;-)

There are several ways to accomplish this.

The model I like to work with is to set one button's dialog result to
OK, another to CANCEL. Then on the form declare the ACCEPT BUTTON as a
property, and the CANCEL BUTTON as a form property. THEN in the CLOSING
event for the form I check the DIALOG RESULT property on the form. If it
is OK then I validate data. If it is valid, I post the collected data to
where ever it needs to go, and exit the CLOSING handler. If it is NOT
valid I then throw up a dialog box with a problem report. User can
either resume editing, in which case I set the CANCEL property on the
event arguments, which effectively stops the closing of the form. I also
allow the user to exit WITHOUT posting the invalid data, in which case I
simply allow the form to close. The nice thing about this approach is
that it takes a logical action EVEN if the user closes the form by some
method OTHER than your OK button.

HTH - Lee Gillie, Spokane WA
 
Hmmm... the problem is, that i have arleady tried this solution and it's
doesn't work, The form is still closing. Below I place a code of function
responsible for attend buttonClick event:
private void buttonDodaj_Click(object sender, EventArgs e)

{

if (p.znajdzTurnusPoKodzie(p.kodTurnusu) == null)

{

MessageBox.Show("Turnus o podanym kodzie istnieje, zmieñ dane!");

this.DialogResult = DialogResult.None;

}else

{

p.kodTurnusu = tKodTurnusu.Text;

p.nazwaTurnusu = tNazwaTurnusu.Text;

p.rok = tRok.Text;

this.DialogResult = DialogResult.OK;

}

}

What's wrong with this code? I also have to mention, that simirarly solution
doesn't work in the others "showdialog" forms ...

i wll be very thankful for help ....
 
Are you sure that the condition
(p.znajdzTurnusPoKodzie(p.kodTurnusu) == null)
is evaluating to true ?

does p.znajdzTurnusPoKodzie(p.kodTurnusu) return a string by any chance ?

--Saurabh
 
I think that mentioned condition is ok because belowed line of code:
MessageBox.Show("Turnus o podanym kodzie istnieje, zmieñ dane!");
is executing (the dialogbox appears) before closing the form ...
 
Lukasz said:
How should I do that? Can You give some code sample?

Part of this work is in the designer, so I am showing form and button
properties as well.

- Lee

Public Class DialogForm
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnOK As System.Windows.Forms.Button
Friend WithEvents btnCancel As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.btnOK = New System.Windows.Forms.Button
Me.btnCancel = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'btnOK
'
Me.btnOK.DialogResult = System.Windows.Forms.DialogResult.OK
Me.btnOK.Location = New System.Drawing.Point(16, 216)
Me.btnOK.Name = "btnOK"
Me.btnOK.Size = New System.Drawing.Size(72, 32)
Me.btnOK.TabIndex = 0
Me.btnOK.Text = "OK"
'
'btnCancel
'
Me.btnCancel.DialogResult =
System.Windows.Forms.DialogResult.Cancel
Me.btnCancel.Location = New System.Drawing.Point(200, 224)
Me.btnCancel.Name = "btnCancel"
Me.btnCancel.Size = New System.Drawing.Size(72, 32)
Me.btnCancel.TabIndex = 1
Me.btnCancel.Text = "CANCEL"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(96, 128)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(88, 20)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(64, 88)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(144, 32)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Enter a number from 1 to 3"
'
'DialogForm
'
Me.AcceptButton = Me.btnOK
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.CancelButton = Me.btnCancel
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.btnCancel)
Me.Controls.Add(Me.btnOK)
Me.Name = "DialogForm"
Me.Text = "DialogForm"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub DialogForm_Closing( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
If Me.DialogResult = DialogResult.OK Then
If Not IsNumeric(Me.TextBox1.Text) Then
If MsgBox( _
"Not a number. Retry to resume editing.", _
MsgBoxStyle.RetryCancel) = MsgBoxResult.Retry Then
e.Cancel = True
Return
End If
End If
Dim v As Integer = CInt(Me.TextBox1.Text)
If (v < 1) Or (v > 3) Then
If MsgBox( _
"Must be 1-3. Retry to resume editing.", _
MsgBoxStyle.RetryCancel) = MsgBoxResult.Retry Then
e.Cancel = True
Return
End If
End If
' APPLY THE VALUE OF V HERE TO WHAT EVER
' BECAUSE YOU KNOW IT IS VALID NOW
' CANCEL OR CLOSING FORM WON'T GET TO HERE
' RETRY WILL KEEP THE FORM OPEN
End If
End Sub
End Class
 
I have just try your approach, what describes following code:

private void buttonAdd_Click(object sender, EventArgs e)
{
if (p.FindTurnusByID(p.kodTurnusu) == null)
{
MessageBox.Show("Wrong data - try change them!");
this.DialogResult = DialogResult.None;
e.Cancel = true;


}else
{
p.kodTurnusu = tKodTurnusu.Text;
p.nazwaTurnusu = tNazwaTurnusu.Text;
p.rok = tRok.Text;
this.DialogResult = DialogResult.OK;
}

}

And compiler ocurres this error: System.EventArgs does not contain
definition for 'Cancel'

I'm using C# languege - if it changes something :-(
 
Back
Top