System.Windows.Forms.Keys.HasFlag behaving weirdly

后端 未结 3 546
悲哀的现实
悲哀的现实 2020-12-22 00:52

I have the following code, meant to prevent user from writing new-lines in a memo text editor:

private void m_commentMemoEdit_KeyDown(object sender, KeyEvent         


        
3条回答
  •  眼角桃花
    2020-12-22 01:24

    I suspect it has something to do with the underlying values of the System.Windows.Forms.Keys enum being mapped directly to the keyboard codes, which are not always mutually exclusive. The documentation says that you should not use any kind of bitwise operation on them, and gives an example of why not (Beware the FlagsAttribute on the enum!).

    MSDN also says that the HasFlag function returns the result of this: thisInstance And flag = flag, so you could potentially set a breakpoint and look at the actual binary codes coming in and see if that operation would give you a true for the set of keys you listed.

    In the end, your updated code is the right way to do what you want to do.

提交回复
热议问题