c# - Using Linq to filter possible values of System.Windows.Forms.Keys -


I am creating an alternative dialog using WPF which lists the potential key so that the user can allocate the hotkey to the program I am trying to filter all the possible values ​​of the system. Windows.Forms.Keys enumeration just under AZ and F1-F12, and then tied that list into a combo box.

Here is my code away:

  reggaex filter = new regenges ("(^ [aZ] $) | (^ F [0- 9] {1,2 } $) "); IEnumerable & LT; Key & gt; KeyList = x (IEnumerable & lt; k & gt;) Enum.GetValues ​​(typeof (keys)) where filter. Match (x.ToString ()). Selection of success; Keys.DataContext = keyList;  

After executing it, the key list is twice the letter "A" and the letter "P" disappears through "U". Why am I as a loss?

I am interested in alternative ways, if there is a better way.

You had two types - you're System.Windows.Input.Key Instead of using System.Windows.Forms.Keys were using twice. What an unfortunate typo! I suggest that as long as you are not actually using both WinForms and WPF instructions in the same file required , you want to avoid both of them there.

Here's a small but complete example that works (depending on your code, but by default):

  using the system; Using System.Collections.Generic; Using System.Linq; Using System.Text.RegularExpressions; Using System.Windows.Forms; Class Test {Static Zero Main () {Reggae Filter = New Reggae ("(^ [A-Zed] $) | (^ F [0- 9] {1,2} $)"); IEnumerable & LT; Key & gt; KeyList = x (IEnumerable & lt; keys & gt;) Enum.GetValues ​​(typeof (keys)) where filter.Match (x.ToString ()). Select success x; Foreach (var key in keyList) {Console.WriteLine (key); }}}  

Note that you can type query expression more easily by using a clearly typed range variable:

  IEnumerable & Lt; Keys & gt; KeyList = Enum.GetValues ​​(key in typeof (key)) where filter.Match (x.ToString ()). Selection of success;  

Or use dot notation to get started:

  var keyList = Enum.GetValues ​​(typeof (key)) .stast & lt ; Keys & gt; (). (X => Filters. Match (x.ToString ()). Success);  

Alternatively, you can use it and get a strongly typed list:

  var keyList = Enums.GetValues & Lt; Keys & gt; (). X = & gt; Filter. Match (x.ToString ()). Success);  

Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -