C# 如何根据用户输入使图片框从可见变为不可见?

C# 如何根据用户输入使图片框从可见变为不可见?,c#,picturebox,C#,Picturebox,我正在用C#制作一个Enigma模拟器应用程序。除此之外,我还试图制作它的光板,它基本上是一个键盘,用来照亮反射器返回的字母。现在,我的想法是添加26个带有黄色字母图像的图片框,并在每个图片框的顶部添加26个带有灰色字母图像的其他图片框 如果用户键入0个字母,则灰色字母是可见的。当用户键入一个字母时,enigma将对其进行解码并根据其设置返回另一个字母,该字母应在键盘上打开(字母的黄色图像),然后在下一个字母到达时关闭(灰色图像) 下面的代码显示了我是如何做到这一点的,但我不知道如何让它们一个接

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

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

下面的代码显示了我是如何做到这一点的,但我不知道如何让它们一个接一个地进行,而不是一次完成。任何关于如何实现这一效果的帮助或建议都将受到欢迎

 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++;
        }
StringBuilder-ciphertext=新的StringBuilder(txtCiphertext.Text);
int i=0;
while(i
当用户使用
按键输入时,您可以验证用户的输入。当用户按下某个键时,将引发此事件,并由一个函数处理该事件,并执行此函数中的代码。您还可以使用
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; 

        ...
    }
}
然后需要将函数与
文本框
关联起来

如果您这样做,当用户按下
文本框中的键时,将执行此功能,它将执行您需要的操作

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


这并不是一个完整的解决方案,我只是在说正确的话

您可以使用文本框的按键事件捕获按下的按键
Dictionary<char, Tuple<PictureBox, string, string>> List = new 
         Dictionary<char, Tuple<PictureBox, string, string>>();
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]));
    }
}
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;
    } 
}