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

  • Home
  • SEARCH
  • 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 6868759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:28:25+00:00 2026-05-27T03:28:25+00:00

In my application I have: private bool _clear = true; This boolean is used

  • 0

In my application I have:

private bool _clear = true;

This boolean is used to see if a textbox should be cleared or not when user enters a new text into it (by pressing on a TreeNode in a TreeView).

Then I have these two events for my form:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control)
    {
        _clear = false;
    }

}

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control)
    {
        _clear = true;
    }
}

I want it somehow when user is holding the CTRL key, clear be FALSE and when CTRL is released, clear goes back to TRUE.

Obviously the code I wrote here, does not work! what can be wrong and/or is there a better way?

  • 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-05-27T03:28:26+00:00Added an answer on May 27, 2026 at 3:28 am

    It’s a simple fix, as when you release the key, the KeyUp event does not receive any info of the key released itself, so just set the property to true:

    private void Form1_KeyUp(object sender, KeyEventArgs e)
    {
       _clear = true;       
    }
    
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
       if (e.Modifiers == Keys.Control)
       {
          _clear = false;
       }
    }
    

    If you want to see it work in real time, add a label to your form and add this under each setting of the ‘_clear’ variable:

    label1.Text = _clear.ToString();
    

    Per your comment, change the second code block to:

    if (e.KeyData.ToString() == "ControlKey, Control")
    {
       _clear = false;                  
    }
    else if(other shortcut conditionals go here or on other else if's)
    {
       _clear = true;
    }
    

    The only time this conditional will hold true is when control is held by itself. The else case is there for the purpose of setting _clear to true when you press ctrl followed by another key, due to the fact that as soon as you press control, it will fire the KeyDown event.

    Based on this change, as long as you take care of the key presses following that if statement, (such as else if()’s), you will not need to set anything in the KeyUp event.

    See my answer here to the intricacies of keys and their properties if you want some more in-depth info.

    Edit #3 :

    As long as you set the _clear to true on the first line in each conditional, you should be able to avoid the problem you are facing in your comment:

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
    
       if (e.KeyData.ToString() == "ControlKey, Control")
       {
          _clear = false;
    
       }
       else if(e.KeyData.ToString() == "O, Control")
       {
           _clear = true;
           //Do other stuff here, such as opening a file dialog
    
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I see an application have used Log.info = some info where are these logs
Actually, I'm using this way. Do you have a better way? private bool AcceptJson(HttpRequest
I have the following code: private bool IsMousetrapFile(string path) { logger.Log(validateFileMessage + path); Excel.Application
I have the following console application: private static bool run = false; static void
I have an application that opens another class using intent : private void createRepository(){
I have an array list of objects in my application. private static ArrayList<Player> userList=new
I have a WM 6.1 Prof. application that checks updates when user wishes to
I have one user reporting this exception. My research here and elsewhere suggests this
I have an application that logs messages to the screen using a TextBox. The
My application has a restricted access. I have a user/password box in a small

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.