如何根据用户输入使 PictureBoxes 从可见变为不可见?

How to make the PictureBoxes change from visible to unvisible depending on user input?

我正在用 C# 制作一个 Enigma 模拟器应用程序。除此之外,我正在尝试制作它的灯板,它基本上是一个键盘,可以照亮反射器返回的字母。现在,我的想法是添加 26 个带有黄色字母图像的图片框,并在每个图片框的顶部添加 26 个带有灰色字母图像的其他图片框。

如果用户键入 0 个字母,则灰色的是可见的。当用户键入一个字母时,enigma 会根据其设置解码它和 returns 另一个字母,并且该字母应在键盘上打开(字母的黄色图像),然后关闭(灰色图像)作为下一个信到了。

下面的代码显示了我如何尝试执行此操作的部分,但我不知道如何让它们一个接一个地进行,而不是同时进行。欢迎任何有关如何实现此效果的帮助或建议。

 StringBuilder ciphertext = new StringBuilder(txtCiphertext.Text);
        int i = 0;
        while (i < ciphertext.Length)
        {
            if (ciphertext[i] == (char)Keys.A)
            {
                Aoff.Visible = false;
                Aon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.B)
            {
                Boff.Visible = false;
                Bon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.C)
            {
                Coff.Visible = false;
                Con.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.D)
            {
                Doff.Visible = false;
                Don.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.E)
            {
                Eoff.Visible = false;
                Eon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.F)
            {
                Foff.Visible = false;
                Fon.Visible = true;


            }
            else if (ciphertext[i] == (char)Keys.G)
            {
                Goff.Visible = false;
                Gon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.H)
            {
                Hoff.Visible = false;
                Hon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.I)
            {
                Ioff.Visible = false;
                Ion.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.J)
            {
                Joff.Visible = false;
                Jon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.K)
            {
                Koff.Visible = false;
                Kon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.L)
            {
                Loff.Visible = false;
                Lon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.M)
            {
                Moff.Visible = false;
                Mon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.N)
            {
                Noff.Visible = false;
                Non.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.O)
            {
                Ooff.Visible = false;
                Oon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.P)
            {
                Poff.Visible = false;
                Pon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.Q)
            {
                Qoff.Visible = false;
                Qon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.R)
            {
                Roff.Visible = false;
                Ron.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.S)
            {
                Soff.Visible = false;
                Son.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.T)
            {
                Toff.Visible = false;
                Ton.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.U)
            {
                Uoff.Visible = false;
                Uon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.V)
            {
                Voff.Visible = false;
                Von.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.W)
            {
                Woff.Visible = false;
                Won.Visible = true;

            }

            else if (ciphertext[i] == (char)Keys.X)
            {
                Xoff.Visible = false;
                Xon.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.W)
            {
                Woff.Visible = false;
                Won.Visible = true;

            }
            else if (ciphertext[i] == (char)Keys.Z)
            {
                Zoff.Visible = false;
                Zon.Visible = true;

            }






            i++;
        }

您可以在用户输入 KeyPress 事件时验证用户输入。当用户按下一个键时,会引发此事件,并且函数会处理该事件并执行此函数中的代码。您也可以使用 switch 而不是多个 if 语句。

首先创建处理函数:

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    switch (e.KeyChar)
    {
        case 'A':
            Aoff.Visible = false;
            Aon.Visible = true;
            break;

        case 'B':
            Boff.Visible = false;
            Bon.Visible = true;
            break; 

        ...
    }
}

然后您需要将函数关联到 TextBox

如果这样做,当用户在 TextBox 处按下按键时,将执行此函数,它将执行您需要的操作。

I left here some pages that you can see to get more information about my answer:

Control.KeyPress Event

KeyPressEventArgs.KeyChar Property

这不是一个完整的解决方案,我只是在说正确的轨道。

您可以使用文本框的 keyPress 事件来捕获用户键入时按下的键。

我们需要一个字典来存储图片框、灰色 img 和黄色 img,其中 char 类型的键值是用户要在文本框中键入的字符

所以我们要这样声明:

Dictionary<char, Tuple<PictureBox, string, string>> List = new 
         Dictionary<char, Tuple<PictureBox, string, string>>();

然后当加载表单时,您从它们的目录中读取图像并填充列表,同时读取表单中的所有图片框并将它们添加到字典中。

我使用了一个 groupBox,将 PictureBox 控件组合在一起以进行循环。

我假设图片是根据 Key 命名的。

private void Form9_Load(object sender, EventArgs e)
{
    //Reading both  yellow and grey Imgs
    string[] grey = Directory.GetFiles(@"C:\greyImgs");
    string[] yellow = Directory.GetFiles(@"C:\yellowImgs");

    //looping thought the controls in the groupbox which are PictureBoxs
    for (int i = 0; i < groupBox1.Controls.Count; i++)
    {
        // Casting the controls as PictureBox
        PictureBox pic = groupBox1.Controls[i] as PictureBox;

        // Adding the grey imgs to PictureBoxx
        pic.ImageLocation = grey[i];

         // Populating the Dictionary
        List.Add(Path.GetFileNameWithoutExtension(grey[i])[0], new Tuple<PictureBox, string, string>(pic, grey[i], yellow[i]));
    }
}

现在,我们向表单添加一个文本框,然后右键单击它的属性,然后单击灯泡图标向下滚动,直到您看到 KeyPress 双击它。现在为按键创建了事件处理程序。

所以你把这个代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // e.keychar returns the key that the user pressed
    // So we Don't want the user to press a key we don't have so we perform a check 
    if (List.ContainsKey(e.KeyChar))
    {
        // Here we get the first item of the tuple which is the picturebox
        // and we assign the yellow img being the third item in the tuple.
        List[e.KeyChar].Item1.ImageLocation = List[e.KeyChar].Item3;
    } 
}

希望这能奏效。