I want to add values to an array. These values come from textBoxes. When I click the button "Calcular", it should show every number but that doesn't happen.
Can anyone explain me what's happening?
Code:
//Declaração das Variáveis/Arrays
float[] Valores = new float[5];
int Limite = 0;
float Valor0, Valor1, Valor2, Valor3, Valor4;
//Introduzir Valores
private void TextBoxIntroduzirValores_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (Limite >= 5)
{
MessageBox.Show("Só pode introduzir 5 números!");
TextBoxIntroduzirValores.Text = "";
}
else
{
for (int i = 0; i < 4; i++)
{
float ValorTemp = Convert.ToSingle(
TextBoxIntroduzirValores.Text);
Valores[i] = ValorTemp;
}
ListaValores.Items.Add(TextBoxIntroduzirValores.Text);
TextBoxIntroduzirValores.Text = "";
Limite = Limite + 1;
}
}
}
//Introduzir Valores
private void TextBoxIntroduzirValores_TextChanged(object sender, EventArgs e)
{
if (System.Text.RegularExpressions.Regex.IsMatch(
TextBoxIntroduzirValores.Text, "[^0-9]"))
{
MessageBox.Show("Introduza apenas números por favor!");
TextBoxIntroduzirValores.Text = TextBoxIntroduzirValores.Text.Remove(
TextBoxIntroduzirValores.Text.Length - 1);
}
}
//Botão Calcular
private void Calcular_Click(object sender, EventArgs e)
{
Valor0 = Valores[0];
Valor1 = Valores[1];
Valor2 = Valores[2];
Valor3 = Valores[3];
Valor4 = Valores[4];
string Valor00 = Convert.ToString(Valor0);
string Valor11 = Convert.ToString(Valor1);
string Valor22 = Convert.ToString(Valor2);
string Valor33 = Convert.ToString(Valor3);
string Valor44 = Convert.ToString(Valor4);
TextBoxMaximo.Text = Valor00 + Valor11 + Valor22 + Valor33 + Valor44;
}
WinForm in Design View:
And at runtime:
As you can see, it's not showing the array correctly


forloop you add the same item (Convert.ToSingle(TextBoxIntroduzirValores.Text)) to the first 4 indexes of the array, which doesn't seem right. Should that code instead be assigning the value to the latest index in the array (noforloop required)?Valores[Limite] = Convert.ToSingle(TextBoxIntroduzirValores.Text);