Ok Handler always closes the dialog

C

CaptainCaveman

Hi,

My form has an OK button, which I want to verify the data that has been
input.
Under certain conditions I dont want the form to close, i.e. when a control
has no data entered in to it.

Is there a property that I need to set to prevent the form from closing as
my OK handler function closes regardless?

OK Handler code is as follows
private void OnClkOK(object sender, EventArgs e)
{
//populate the member variables
if (m_tbText.Text.Length > 0)
{
tbText = m_tbText.Text;
AlternativeText = m_tbAlternativeText.Text;
m_tbText.Text = string.Empty;
m_tbAlternativeText.Text = string.Empty;
this.Close();
}
else
{
MessageBox.Show("A valid ID is required");
}
}



TIA

CC
 
J

Jeff Johnson

My form has an OK button, which I want to verify the data that has been
input.
Under certain conditions I dont want the form to close, i.e. when a
control has no data entered in to it.

Is there a property that I need to set to prevent the form from closing as
my OK handler function closes regardless?

At the end of your function (when you determine you DON'T want to close):

this.DialogResult = DialogResult.None;
 
A

Alberto Poblacion

CaptainCaveman said:
Is there a property that I need to set to prevent the form from closing as
my OK handler function closes regardless?

You need to REMOVE the value from the DialogResult property of the
button. Instead, set the DialogResult of the Form in your code (if you need
it) before closing the form.
 
C

CaptainCaveman

Thanks Mark,

If I set a break point and tehn step through the code operates correctly.
So, if the text control has no text then a message is displayed saying that
the string is empty, otherwise the If statement executes as though there was
data.
This form is being displayed under a ShowDialog() call - does that make a
difference? As I said earlier, I dont want this Form to close unless there
is text in teh textbox to verify.

TIA

CC
 
D

dunawayc

Hi,

My form has an OK button, which I want to verify the data that has been
input.
Under certain conditions I dont want the form to close, i.e. when a control
has no data entered in to it.

Is there a property that I need to set to prevent the form from closing as
my OK handler function closes regardless?

OK Handler code is as follows
private void OnClkOK(object sender, EventArgs e)
{
//populate the member variables
if (m_tbText.Text.Length > 0)
{
tbText = m_tbText.Text;
AlternativeText = m_tbAlternativeText.Text;
m_tbText.Text = string.Empty;
m_tbAlternativeText.Text = string.Empty;
this.Close();
}
else
{
MessageBox.Show("A valid ID is required");
}

}

TIA

CC

Check to see if the button has it's DialogResult property set. If it
is set to anything other than None, and you showed the form using
ShowDialog, then the form will automatically close when the button is
clicked.

You can also override the OnFormClosing method and use the e.Cancel
property to prevent the form from closeing.

Chris
 
C

CaptainCaveman

Thanks Jeff,
That works a treat.


Jeff Johnson said:
At the end of your function (when you determine you DON'T want to close):

this.DialogResult = DialogResult.None;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top