none
Unexpected Character Being Printed to TextBox RRS feed

  • Question

  •  

    I have KeyPress event as shown below and it works. However, instead of printing "Success" in the textbox it includes the question mark and prints "?Success". I have tried to clear the textbox before I print "Success" and that does not seem to make any difference, neither does anything else I have tried. What I actually want to do if someone types a "?" in the textbox is run another method and leave the textbox blank. I was only printing to the text box to make sure I was capturing the "?" correctly. Seems simple enough but I can't make it work. Obviously I am overlooking something here. Any suggestions/help would be appreciated.

     private void TxtExpectedAPR_KeyPress(object sender, KeyPressEventArgs e)
          {
             if (e.KeyChar == '?')
             {
                txtExpectedAPR.Text = "Success";
             }
                
          }



    JR

    • Moved by CoolDadTx Tuesday, June 18, 2019 1:56 PM Winforms related
    Tuesday, June 18, 2019 9:27 AM

Answers

  •   

    I think the trick is also to check for shift. On my keyboard ? comes when typing shift and +;

    bool shift=false;

      private void TextBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if(e.Shift) shift=true;
            }

      private void TextBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if(e.KeyCode.Equals(Keys.Oemplus)&&shift)
                { textBox1.Text="Success"; }
                shift=false;
            }

        
    • Marked as answer by J_Reid Tuesday, June 18, 2019 12:21 PM
    Tuesday, June 18, 2019 12:00 PM

All replies

  •  

    You need to use KeyUp or KeyReleased or equivalent, not KeyPressed.

    Typing Success needs to happen AFTER the keypress that creates the '?'






    • Edited by ThisNewbie Tuesday, June 18, 2019 11:09 AM
    Tuesday, June 18, 2019 9:55 AM
  • Thank you for your reply. It turns out that all I needed to do was add the line "e.Handled = True". That solved my problem. However, every time I try and type "e.Key" I get an error and it will not accept ".Key" and it is not available in intelisense. Any Thoughts on why that might be? I am using C# in VS2019 

    JR

    Tuesday, June 18, 2019 11:19 AM
  • This works when hitting + 

    private void TxtExpectedAPR_KeyUp(object sender, KeyEventArgs e)
            {
                if(e.KeyCode.Equals(Keys.Oemplus))
                {
     txtExpectedAPR.Text="Success"; }
            }



    ?
    • Edited by ThisNewbie Tuesday, June 18, 2019 11:48 AM
    Tuesday, June 18, 2019 11:26 AM
  • I am working on a Windows Form App that was created by someone else  either last year or maybe the year before. It all works fine I am just trying to add some additional functionality. 

    JR

    Tuesday, June 18, 2019 11:46 AM
  •   

    I think the trick is also to check for shift. On my keyboard ? comes when typing shift and +;

    bool shift=false;

      private void TextBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if(e.Shift) shift=true;
            }

      private void TextBox1_KeyUp(object sender, KeyEventArgs e)
            {
                if(e.KeyCode.Equals(Keys.Oemplus)&&shift)
                { textBox1.Text="Success"; }
                shift=false;
            }

        
    • Marked as answer by J_Reid Tuesday, June 18, 2019 12:21 PM
    Tuesday, June 18, 2019 12:00 PM
  • I thank you again for the additional information. This works except I have to change the Oemplus to Oemquestion. Also, after I added the "e.handled = true" to my keypressed event it also works very well with much less code. I still have not figured out why I cannot add the ".Key" as in "e.Key".

    JR

    Tuesday, June 18, 2019 12:28 PM