Difficulty transcribing a console application program for windows form (C #)

2

I was learning to use split and in the end I had this code:

namespace String_Split
{
    class Program
    {
        static void Main(string[] args)
        {
            string mensagem_completa;
            string palavra;
            string[] Apenas_palavras;
            Console.WriteLine("Apresente o texto a ser lido . . .");
            mensagem_completa = Console.ReadLine();

            Apenas_palavras = mensagem_completa.Split(' ', '.', ',');
            int tamanho = Apenas_palavras.Length;


            Console.ReadKey();

            for (int i = 0; i < tamanho; i++)
            {
                palavra = Apenas_palavras[i];
                Console.Clear();
                Console.WriteLine(palavra);
                Thread.Sleep(1000);

            }
            Console.WriteLine("Total de caracteres: " + mensagem_completa.Length);
            Console.WriteLine("Total de palavras: {0}", tamanho);
            Console.ReadKey();

        }

    }
}

The program ran smoothly, all right.

So I thought, why not turn it into a program with windows form? The result was this one:

namespace Split_em_Form
{

    public partial class Form1 : Form
    {

        public string[] texto_em_array;
        public string texto;
        public int tamanho_do_texto;
        public string palavra;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            texto = textBox1.Text;
            texto_em_array = texto.Split(' ', ',', '\n', '.');
            tamanho_do_texto = texto_em_array.Length;


            for (int i = 0; i < tamanho_do_texto; i++)
            {
                palavra = texto_em_array[i];
                label1.Text = "";
                label1.Text = palavra;
                Thread.Sleep(1000);
            }
        }
    }
}

When clicking on the button1 something unexpected happened, only the last item in the array appeared in label1 . Oh my doubt stays, why did this happen? What is the difference between the code I wrote the first time for the second code and how do I solve it?

    
asked by anonymous 07.10.2015 / 18:53

2 answers

3

You're throwing the values in label and not concatenating them, so that every loop label.Text has a different value.

Whenever you do Console.WriteLine() a new line is written. When you do label.Text = "texto" , you are doing what current text of label is replaced by this new text.

To concatenate all the values of array in label would look like this:

for (int i = 0; i < tamanho_do_texto; i++)
{
    palavra = texto_em_array[i];
    label1.Text += " - " + palavra;
    Thread.Sleep(1000);
}

Instead, instead of using loop to concatenate your values, you can use string.Join() .

label.Text = string.Join(", ", texto_em_array); 

The first parameter of the method receives the separator for the values of the array, the second is the array of string and the return will be all values of the array separated by , / p>     

07.10.2015 / 19:00
1

A Label can only use one text at a time. Whenever a value is assigned to it, through label1.Text = palavra; , the previous text is replaced by the new one.

While using Thread.Sleep(1000); , Label is only visually updated (on-screen) after for has finished.

To get the effect you want to have to call the Refresh after every label1.Text = palavra;

for (int i = 0; i < tamanho_do_texto; i++)
{
    palavra = texto_em_array[i];
    label1.Text = palavra;
    Refresh();
    Thread.Sleep(1000);
}

Perhaps a better way to present the words would be to use a ListBox instead of a Label .

Put a ListBox in your Window and change the code from for to:

listBox1.Items.Clear();
for (int i = 0; i < tamanho_do_texto; i++)
{
    palavra = texto_em_array[i];
    listBox1.Items.Add(palavra);
}
    
07.10.2015 / 19:24