winforms - How can I prevent the keydown event of a form in C# from firing more than once? -


According to

, the keydown event occurs only once on a Windows Forms control, but it is easy to show that the event is a constant fire The key is placed below as follows:

  Private zero text box1_KeyDown (Object Sender, KeyEventArgs E) {label1.Text = string.Format ("{0}", globalCounter ++); }  

How can you use this phenomenon so that it can only fire once?

I'm usually a VB boy, but it seems to work as a demo code for me It creates itself as an input source:

  namespace WindowsFormsApplication1 {public partial class} form 1: form {private bool _keyHeld; Public Form 1 () {Initialization (); This.KeyUp + = New KeyEventHandler (Form1_KeyUp); this. Kadoun + = new key event handler (form 1_kadown); This._keyHeld = false; } Zero form 1_ (Object Sender, Key Event E) {this._keyHeld = false; } Zero form1_keydown (object sender, key event er) {if (! This._keyHeld) {this._keyHeld = true; If (this.BackColor == Control.DefaultBackColor) {this.BackColor = Color.Red; } And {this.backcolor = control. Default bikerolar; }} And {e. Handle = true; I think that if you are holding multiple keys at a time, then the logic is summarized, but it appears that the event was pressed with the last key which was pressed. Anyway, I do not think this has become an issue. 

I tested it in the text box in VB, and it worked fine. It was not certain that I should follow heritage conventions in C #, so I left it as a straightforward form for this answer.

Sorry for any gross code formatting errors, again, this is not my usual language.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -