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

Asked

Viewed 286 times

2

I was learning to use split and at 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, everything straight.

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

By clicking on button1 something unexpected happened, only the last item of the array appeared in the label1. My doubt is, why did this happen? What is the difference of the code I wrote the first time to the second code and how can I solve it?

2 answers

3


You’re playing the values on label and not concatenating them, it makes each loop label.Text have a different value.

Whenever you do Console.WriteLine() a new line is written. Already when you do label.Text = "texto", you are doing with what current text of label be it replaced for this new text.

To concatenate all values of array in the 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);
}

Only as an alternative, instead of using a loop to concatenate its values, you can use string.Join().

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

The first parameter of the method receives the separator for the array values, the second is the array of string and the return will be all array values separated by , (comma).

1

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

Despite using the Thread.Sleep(1000); the Label is updated visually (on screen) only after cycle for have finished.

To get the effect you want you need to call the method Refresh after each 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 his Window and change the code of the for for:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.