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

时间:2018-05-07 18:29:22

标签: c# picturebox

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

如果用户输入0个字母,则灰色的是可见的。当用户键入一个字母时,它将对其进行解码并根据其设置返回另一个字母,并且该字母应在键盘中打开(字母的黄色图像),然后在下一个字母到达时关闭(灰色图像)。 / p>

以下代码是显示我是如何尝试这样做的部分,但我无法弄清楚如何让它们一个接一个地继续下去。如何实现这一效果的任何帮助或建议都会受到欢迎。

 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++;
        }

2 个答案:

答案 0 :(得分:0)

您可以在输入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处的按键时,此功能将被执行,它将满足您的需要。

  

我在这里留下了一些您可以看到的页面,以获取更多信息   我的回答:

     

Control.KeyPress Event

     

KeyPressEventArgs.KeyChar Property

答案 1 :(得分:0)

这不是一个完整的解决方案,我只是说出正确的话语。

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

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

所以我们要这样声明:

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

然后当表单加载时,你从他们的目录中读取imges并填充列表,同时读取我们在表单中的所有pictureBox,并将它们添加到Dictionary中。

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

我会假设图片是根据密钥命名的。

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双击它。现在为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;
    } 
}

希望这有效。