1

I want to create a subset enums list from System.Windows.Forms.Keys, that only includes A-Z, 0-9, and F keys. Currently, I set the full list to a combo box on a winform:

comboBox1.DataSource = Enum.GetValues(typeof (System.Windows.Forms.Keys));

Is there a fast way to create this subset? My current workaround is to hardcode a list of acceptable keys:

private List<Keys> acceptableKeys = new List<Keys>
            {
                Keys.A,
                Keys.B, Keys.C, Keys.D, Keys.E, Keys.F, Keys.G, Keys.H, Keys.I, Keys.J, Keys.K, Keys.L, Keys.M,
                Keys.N, Keys.O, Keys.P, Keys.Q, Keys.R, Keys.S, etc....
            };

and use that as the dataSource.

Is there a better method to do this?

1 Answer 1

3

If you don't want to hardcode it you could try checking the int value of the enum and adding them to the list.

var enums = Enum.GetValues(typeof(Keys));

List<Keys> acceptableKeys = new List<Keys>();
foreach (var e in enums)
{
   int val = (int)e;
   //The boundaries represent A-Z, 0-9, F1 - F24 respectively
   if ((val >= 65 && val <= 90) || (val >= 48 && val <= 59) || (val >= 112 && val <= 135))
       acceptableKeys.Add((Keys)e);
}

This won't include numbers on the numpad but you can add that check if you want to include those as well.

Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly! Good thinking. Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.