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?