How to associate items from a List of Strings to the code name of a label in C #?

0

My goal is to make a gallows game.

Basically my problem is this: I have a word divided into substrings for each letter of the word, all stored in a vector. For example:

string[] letras = new string[numletras];
for (int i = 0; i < numletras; i++)
{
   letras[i] = palavra.Substring(i, 1);
}

With the word divided into their respective letters, I wanted to associate each letter with each label.

Asanexample,Ihaveaword"shirt" and wanted to split the letters for each label . As each word has a different number of letters, I need to arrange a cycle that does this to me, that is, I can not do one by one.

Here is the whole code of my program. There are parts that I have to optimize I know, but my main problem was the one I mentioned above.

public partial class Form1 : Form
{
    string palavra;
    int tentativas;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBox1.Enabled = true;
        textBox2.Enabled = true;
        button2.Enabled = true;
        button3.Enabled = true;

        tentativas = 5;
        label14.Text = Convert.ToString(tentativas);

        var linhas = File.ReadAllLines(Application.StartupPath + "Roupas.txt").Length;
        Random rnd = new Random();
        int randomlinhanum = rnd.Next(linhas);
        int indicator = 0;


        using (var reader = File.OpenText(Application.StartupPath + "Roupas.txt"))
        {
            while (reader.ReadLine() != null)
            {
                if (indicator == randomlinhanum)
                {
                    palavra = File.ReadLines(Application.StartupPath + "Roupas.txt").Skip(indicator - 1).Take(1).First();
                    MessageBox.Show(palavra);
                    break;
                }
                indicator++;
            }
        }


        int numletras = palavra.Length;
        MessageBox.Show(Convert.ToString(numletras));

        //iNDICAÇÃO DE QUANTAS LABELS O TABULEIRO IRÁ TER

        if (numletras == 4)
        {
            letra1.Show();
            letra2.Show();
            letra3.Show();
            letra4.Show();
        }
        if (numletras == 5)
        {
            letra1.Show();
            letra2.Show();
            letra3.Show();
            letra4.Show();
            letra5.Show();
        }
        if (numletras == 6)
        {
            letra1.Show();
            letra2.Show();
            letra3.Show();
            letra4.Show();
            letra5.Show();
            letra6.Show();
        }
        if (numletras == 7)
        {
            letra1.Show();
            letra2.Show();
            letra3.Show();
            letra4.Show();
            letra5.Show();
            letra6.Show();
            letra7.Show();
        }
        if (numletras == 8)
        {
            letra1.Show();
            letra2.Show();
            letra3.Show();
            letra4.Show();
            letra5.Show();
            letra6.Show();
            letra7.Show();
            letra8.Show();
        }
        if (numletras == 9)
        {
            letra1.Show();
            letra2.Show();
            letra3.Show();
            letra4.Show();
            letra5.Show();
            letra6.Show();
            letra7.Show();
            letra8.Show();
            letra9.Show();
        }
        if (numletras == 10)
        {
            letra1.Show();
            letra2.Show();
            letra3.Show();
            letra4.Show();
            letra5.Show();
            letra6.Show();
            letra7.Show();
            letra8.Show();
            letra9.Show();
            letra10.Show();
        }

        string[] letras = new string[numletras];
        for (int i = 0; i < numletras; i++)
        {
            letras[i] = palavra.Substring(i, 1);
        }


      //Onde preciso de ajuda

    }

    private void Form1_Load(object sender, EventArgs e)
    {


        textBox1.Enabled = false;
        textBox2.Enabled = false;
        button2.Enabled = false;
        button3.Enabled = false;

        letra1.Hide();
        letra2.Hide();
        letra3.Hide();
        letra4.Hide();
        letra5.Hide();
        letra6.Hide();
        letra7.Hide();
        letra8.Hide();
        letra9.Hide();
        letra10.Hide();

    }

    private void button2_Click(object sender, EventArgs e)
    {
    }
    private void button3_Click(object sender, EventArgs e)
    {

    }
}
    
asked by anonymous 16.01.2018 / 16:45

2 answers

2

Just do this:

for (int i = 0; i < numletras; i++) letra[i].Text = palavra[i];

You will create the labels more or less like this:

for (int i = 0; i < numletras; i++) {
    var letra[i] = new Label(); //só a base
    //aqui teria outras coisas
}

Doing so makes the code absurdly smaller, flexible, and easier to maintain and understand.

I could help more if I had more parts of the code.

    
16.01.2018 / 18:20
0

In your

string[] letras = new string[numletras];
        for (int i = 0; i < numletras; i++)
        {
            letras[i] = palavra.Substring(i, 1);
        }

You need to pass the value to each label14.Text , that is.

   string[] letras = new string[numletras];
            for (int i = 0; i < numletras; i++)
            {
                label1.Text = palavra.Substring(1, 1);
                label2.Text = palavra.Substring(1, 2);  
            }

Of course you will have to do it in a dynamic way, or specify one by one.

    
16.01.2018 / 17:36