Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7856423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:42:25+00:00 2026-06-02T20:42:25+00:00

I have a form which, among the other controls, has a TextBox field that

  • 0

I have a form which, among the other controls, has a TextBox field that can be optionally filled by user .
There is also a btnSubmit Button that performs required actions and closes the form .
In its code, I check if a comment is present and, if not so, ask the user if he wants to fill it before exiting .
I use a bool AskToFillCommentIfNeeded() function that displays a MessageBox asking user to optionally fill the comment before exiting if this has not been done already .
It returns true if user answered Yes, false otherwise .
If user clicks Yes, I must exit the submit function without closing the form, so user can enter the comment and then press submit button again .

The [edited] code is something like this :

private void btnSubmit_Click(object sender, EventArgs e)
{
     // ask user if he wants to fill the comment : if so, exit this function
     if (AskToFillCommentIfNeeded()) { return; };

     // ... save data and exit form ...
}

I use this code in a non modal form and it works as expected .
But when I try to use it in a modal form with btnSubmit DialogResult property set to OK,
it does not work as expected :
instead of just exiting the event sink, it closes the form
without saving data .

I made up a (clumsy) workaround, using a boolean flag like this :

private bool isBusy = false;
private void btnSubmit_Click(object sender, EventArgs e)
{
     // clumsy attempt to avoid form exit :
     isBusy = true;

     // ask user if he wants to fill the comment : if so, exit this function
     if (AskToFillCommentIfNeeded()) { return; };

     isBusy = false;

      // ... save data and exit form ...
}

Thus, if the return statement is executed, I intercept the _FormClosing event
and cancel it if the flag is set to true :

private void FDialog_FormClosing(object sender, FormClosingEventArgs e)
{
     // abort closing if flag is set
     e.Cancel = isBusy;
}

This works, but it is less than satisfactory .
Is there a better way to achieve the same behaviour ?
I am using C# Express 2010 .
Thank you in advance .

jack griffin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T20:42:27+00:00Added an answer on June 2, 2026 at 8:42 pm

    Just set the form’s DialogResult property back to None to prevent the dialog from closing:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
         if (AskToFillCommentIfNeeded()) {
             this.DialogResult = DialogResult.None; 
             return;
         }
    
         // ... save data and exit form ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form which has a FileField among other fields. Imagine the user
I have a form which users can add controls to and when they right
I have a DataGrid bound to a DataView which, among other columns has, an
I have a Windows Forms user control which consists of several controls. Among others
I have an Excel2010 VBA userform that has one comboBox, from which the user
In my application I have a products model which has among other things four
We have a servlet project, which contains (among many other classes) an interface that
I have a form which among others contains text inputs that contain arithmetic data
I have a form which takes both the user details and an image uploaded
I have a form which has only tab control in it. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.