开发者

Verify string does NOT contain a value other than known values [VB.NET]

开发者 https://www.devze.com 2023-01-27 11:55 出处:网络
I am trying to verify that a string contains nothing but known values. In this case, I need to make sure it contains only \"Shift\", \"Control\", or \"Alt\", but not necessarily all of those. For exam

I am trying to verify that a string contains nothing but known values. In this case, I need to make sure it contains only "Shift", "Control", or "Alt", but not necessarily all of those. For example, these should be true: "Shift + P", "Shift + Control + H", "Alt + U; but these should not: "Other + P", "Shift + Fake + Y", "Unknown + Shift + E" etc.

This is the code I tried to use:

If Not shortcut.Contains("Shift") Or Not shortcut.Contains("Control") Or Not shortcut.Contains("Alt") The开发者_StackOverflown
    MessageBox.Show("Invalid")
End If

I'm having difficulty wrapping my head around the needed logic to do this. I'm assuming there's a logic operator that can do this?


I believe you should not use strings for this purpose. You need a data type that can represent a hotkey combination, which is comprised of a "normal" key and a set of modifiers (Alt, Control, Shift), each of which can either be on or off. The on/off modifiers can be represented by an enum with flags, and the "normal" key can be represented by a separate enum. Both of the enums can be contained within a class.

The System.Windows.Forms.Keys enumeration can be used as both enums. You can store two numeric values (one for the modifiers, one for the "normal" key) - the underlying enum values - and they will represent the combination. No need to store strings.

If you do use strings for this purpose, you need to define your constraints better. Your rules do not specify how "Shift + Other" is invalid, but "Shift + F" is. A way to go about this, anyway, is to separate the string by " + " (assuming this is always the separator) and then compare each part to the list of valid values, which apparently contains "Shift", "Alt", "Control" and all single letters.


I think it would be easier to split the string into segments and then iterate through the list of words and then comparing to what words should exist. This is in C# but I guess you could figure it out.

C#:

        string text = "Shift+Control+Alt+Other";
        string[] textSegments = text.Split('+');
        string[] allowedWords = { "Alt", "Shift", "Control" };
        foreach (string t in textSegments)
        {
            bool temp = false;

            foreach (string t2 in allowedWords)
            {
                if (t == t2)
                {
                    temp = true;
                    continue;
                }
            }

            if (!temp)
            {
                MessageBox.Show("Wrong!");
            }
            else
            {
                MessageBox.Show("Right!");
            }


        }

The output will be 4 MessageBoxes displaying: "Right!" "Right!" "Right!" "Wrong!"

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号