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 7781543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:15:38+00:00 2026-06-01T19:15:38+00:00

I am making a TextBox behave like if it could store a null value.

  • 0

I am making a TextBox behave like if it could store a null value. In order to do that, i have a variable NullMode that indicates wether the value is stored is Null, and in TextChanged i set that to false, and on specific user action i set it to true and Text to a value that indicates that there is null inside the textbox. Then, based on NullMode, textbox is drawn differently.

Now, i have a semaphore-like approach in order to prevent event handle from firing when i dont need it. Here is how it looks:

                private void input_TextChanged(object sender, EventArgs e)
        {
            if (_preventTextBoxEvents) 
                return;

            _preventTextBoxEvents = true;

            //if (NullMode)
            //  Text = "";
            NullMode = false;
            ValidateInput();

            _preventTextBoxEvents = false;
        }

Now, if i need to set a textbox text to something that should show while in nullmode, i just set _preventTextBoxEvents before i do to true and it works all right.

BUT! I need to also remove the text when user tries to input something into the textbox! So i need to set Text to “”. Problem is, if i uncomment that, form is closed after the event handler exits. I cannot prevent it (e.Cancel = true in FormClosing doesnt help!) and do not understand what can be causing it. There is no error message too (and i’m not doing try-catch).

My logic, when i do Text=””. OnTextChanged should fire, it should call my TextChanged and it will see _preventTextBoxEvents is true and exit, so there would be no stack overflow / infinite recursion.

What is going on?

  • 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-01T19:15:41+00:00Added an answer on June 1, 2026 at 7:15 pm

    It should be so (assuming your text box is called input)

    if (NullMode) 
        input.Text = ""; 
    

    Text alone refers to the current form caption, not to the input.Text property.
    However let me say that your approach is dangerous. If, for some reason, your ValidateInput throws an exception, you will exit from the event handler without restoring the global _preventTextBoxEvents to false;

    Add a try – catch – finally

        private void input_TextChanged(object sender, EventArgs e)   
        {   
            if (_preventTextBoxEvents)    
                return;   
    
            _preventTextBoxEvents = true;   
            try
            {
               if (NullMode) input.Text = "";   
               NullMode = false;   
               ValidateInput();   
            }
            catch(Exception ex)
            {
                // Inform user of the exception occurred inside your ValidateInput
                MessageBox.Show(ex.Message);
            }
            finally
            {
                // Be sure to restore this global to a functioning value.
                _preventTextBoxEvents = false;   
            }
        }   
    

    Moreover you could remove the logic for _preventTextBoxEvents simply disconnecting your TextChanged event when your program enters in the event code:

    try
    {
        input.TextChanged -= new EventArgs(input_TextChanged);
        // Now, the input textbox is no more connected to this code, you 
        // could happily change the textbox text without worry to reenter in this code
    }
    ...
    finally
    {
        // Work finished, reconnect this code to the event TextChanged
        input.TextChanged += new EventArgs(input_TextChanged);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found a similar question to mine in Making a WinForms TextBox behave
I have code like this, in pagefunction making up a wizard: <TextBox Name=txtDate Text={Binding
When I put value in textbox then its throught this error. I am making
I'm making my own commentbox control that inherits from a winforms textbox. One of
Making a specific word entered into a textbox trigger a password prompt that when
I am making a forms application is Visual C#. I have a textbox where
I have a jQuery autocomplete that works fine making the suggestions but when I
I have a problem that is making me crazy. On my page I have
I am making a project that uses barcodes to store information relating to the
Hi im making a custom Textbox i got everty thing working but how do

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.