Search code examples
c#picturebox

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


I am making an Enigma Simulator Application in C#. In addition to that I am trying to make its lightboard, which is basically a keyboard that lights up the letter which is returned by the reflector. Now, my idea was to add 26 pictureboxes with yellow images of letters and on top of each one to add 26 other pictureboxes with grey images of letters.

The grey ones are the ones that are visible if 0 letters are typed by the user. When the user types a letter, the enigma decodes it and returns another one based on its settings and that letter should turn on in the keyboard(yellow image of letter), then it turns off (grey image) as the next letter arrives.

The code below is the part that shows how I tried to do this, but I can't figure out how to make them go on one after another not all at once. Any help or advice how to achieve this effect would be welcomed.

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

Solution

  • This is not an entire solution, i'm just putting on the right track sort of speak.

    you can use keyPress Event of the textbox to capture the key that was pressed while the user is typing.

    We gonna need A Dictionary to store the pictureboxe, the grey img, and the yellow img, with a keyvalue of type char being the char the user gonna type in the textbox

    So we gonna declare it like this :

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

    Then when the form loads you read the imges from their directory and populate the lists, and at the same time read all of the pictureBoxes we have in the form and add them to the Dictionary.

    i used a groupBox, to groups the PictureBox Controls together to make is to loop through.

    i'm gonna assume the pictures are named based on the 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]));
        }
    }
    

    Now after that we, we add a textbox to the form, and RightClick on it properties then click on the lighting bulb icon scroll down till you see KeyPress double click on it. now the event handler is created for the keypress.

    so you put this code :

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

    Hope this works.