C# 如何从文本框向数组添加值?WinC窗体#
我想向数组中添加值。这些值来自文本框。 当我点击“Calcular”按钮时,它应该会显示每个数字,但事实并非如此 有人能解释一下发生了什么事吗 代码:C# 如何从文本框向数组添加值?WinC窗体#,c#,arrays,winforms,C#,Arrays,Winforms,我想向数组中添加值。这些值来自文本框。 当我点击“Calcular”按钮时,它应该会显示每个数字,但事实并非如此 有人能解释一下发生了什么事吗 代码: //Declaraço das Variáveis/Arrays 浮动[]价值=新浮动[5]; int Limite=0; 浮动Valor0、Valor1、Valor2、Valor3、Valor4; //瓦洛雷斯导言 私有void textboxIntroductionIrvalores_KeyDown(对象发送方,KeyEventArgs e)
//Declaraço das Variáveis/Arrays
浮动[]价值=新浮动[5];
int Limite=0;
浮动Valor0、Valor1、Valor2、Valor3、Valor4;
//瓦洛雷斯导言
私有void textboxIntroductionIrvalores_KeyDown(对象发送方,KeyEventArgs e)
{
如果(e.KeyCode==Keys.Enter)
{
如果(限制>=5)
{
MessageBox.Show(“Sópode Introzir 5 números!”);
textboxintrozirvalores.Text=“”;
}
其他的
{
对于(int i=0;i<4;i++)
{
float ValorTemp=Convert.ToSingle(
textboxIntroductionIrvalores.Text);
Valores[i]=ValorTemp;
}
ListaValores.Items.Add(textboxIntroductionIrvalores.Text);
textboxintrozirvalores.Text=“”;
Limite=Limite+1;
}
}
}
//瓦洛雷斯导言
私有void textboxIntrozirvalores\u TextChanged(对象发送方,事件参数e)
{
如果(System.Text.RegularExpressions.Regex.IsMatch(
TextBoxIntroductionIrvalores.Text,“[^0-9]”)
{
MessageBox.Show(“introzaapenasnúmeros por four!”);
textBoxIntroductionIrvalores.Text=textBoxIntroductionIrvalores.Text.Remove(
textboxintrozirvalores.Text.Length-1);
}
}
//Botão结石
私有无效计算单击(对象发送方,事件参数e)
{
Valor0=Valores[0];
Valor1=Valores[1];
Valor2=Valores[2];
Valor3=Valores[3];
Valor4=Valores[4];
字符串Valor00=Convert.ToString(Valor0);
字符串Valor11=Convert.ToString(Valor1);
字符串Valor22=Convert.ToString(Valor2);
字符串Valor33=Convert.ToString(Valor3);
字符串Valor44=Convert.ToString(Valor4);
TextBoxMaximo.Text=Valor00+Valor11+Valor22+Valor33+Valor44;
}
设计视图中的WinForm:
在运行时:
正如您所看到的,它没有正确显示阵列我不知道我是否真的理解您想要的,无论如何 变量:
float[] values = new float[5];
int num = 0;
假设您输入了5个值从文本框tbValues
private void tbValues_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter && num < 5 && !(string.IsNullorEmpty(tbValues.text)))
{
values[num] = int.Parse(tbValues.Text);
listValues.Items.Add(values[num].ToString());
num ++;
}
else
...
tbValues.Clear();
}
我在代码中看到了一些您可能想要更改的内容 将项目添加到
列表框时
:
Length
属性作为条件。如果您以后决定更改最大值数量,这将减少您必须更新代码的位置数量Limite
作为添加项目的索引private void TextBoxIntroduzirValores_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (Limite >= Valores.Length)
{
MessageBox.Show($"You can only enter {Valores.Length} numbers!");
TextBoxIntroduzirValores.Text = "";
}
else
{
// Add item to array
Valores[Limite] = Convert.ToSingle(TextBoxIntroduzirValores.Text);
// Increment index
Limite = Limite + 1;
// Add item to listbox
ListaValores.Items.Add(TextBoxIntroduzirValores.Text);
// Clear textbox
TextBoxIntroduzirValores.Text = "";
}
}
}
此外,在计算结果时,可以对存储值的数组使用System.Linq
扩展方法:
private void Calcular_Click(object sender, EventArgs e)
{
// You can calculate values using Sytem.Linq extension methods
txtMin.Text = Valores.Min().ToString();
txtMax.Text = Valores.Max().ToString();
txtAvg.Text = Valores.Average().ToString();
txtTotal.Text = Valores.Sum().ToString();
}
或者,您可以长期计算这些值。为此,请使用一些默认值创建变量。对于默认的Min
值,我们使用可能的最大值。然后,当我们循环遍历数组中的每个项时,我们查看该项是否小于Min
,如果小于,则使用此新值更新Min
。同样,我们使用尽可能最小的数字作为Max
的默认值。这样可以确保这些项目在循环结束时是准确的
对于Total
,我们从0
开始计算值,然后在循环过程中将数组中的每个项计算到Total
。对于平均值
,我们只需将总数
除以数组中的项数(即长度
属性):
private void Calcular\u单击(对象发送方,事件参数e)
{
//或者您也可以长期使用。首先从默认值开始:
float min=Single.MaxValue;
float max=Single.MinValue;
浮动总数=0;
//然后检查数组中的每个项目
//并在必要时更新上述值
foreach(价值浮动项目)
{
如果(项目<最小值)最小值=项目;
如果(项目>最大值)最大值=项目;
总计=总计+项目;
}
//最后计算平均值,因为我们首先需要总数
浮动平均值=总/价值。长度;
//使用以下值更新文本框:
txtMin.Text=min.ToString();
txtMax.Text=max.ToString();
txtAvg.Text=avg.ToString();
txtTotal.Text=total.ToString();
}
你说它应该显示每个数字是什么意思?它应该在文本框中填充统计信息?在此处发布相关代码,而不是作为指向外部站点的链接。在for
循环中,您将相同的项(Convert.ToSingle(textboxIntroductionIrvalores.text)
)添加到数组的前4个索引中,这似乎不正确。该代码是否应该将该值分配给数组中的最新索引(不需要for
循环)Valores[Limite]=Convert.ToSingle(textboxintrozirvalores.Text)代码>
private void TextBoxIntroduzirValores_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (Limite >= Valores.Length)
{
MessageBox.Show($"You can only enter {Valores.Length} numbers!");
TextBoxIntroduzirValores.Text = "";
}
else
{
// Add item to array
Valores[Limite] = Convert.ToSingle(TextBoxIntroduzirValores.Text);
// Increment index
Limite = Limite + 1;
// Add item to listbox
ListaValores.Items.Add(TextBoxIntroduzirValores.Text);
// Clear textbox
TextBoxIntroduzirValores.Text = "";
}
}
}
private void Calcular_Click(object sender, EventArgs e)
{
// You can calculate values using Sytem.Linq extension methods
txtMin.Text = Valores.Min().ToString();
txtMax.Text = Valores.Max().ToString();
txtAvg.Text = Valores.Average().ToString();
txtTotal.Text = Valores.Sum().ToString();
}
private void Calcular_Click(object sender, EventArgs e)
{
// Or you can do it the long way. First start with default values:
float min = Single.MaxValue;
float max = Single.MinValue;
float total = 0;
// Then go through each item in the array
// and update the values above if necessary
foreach (float item in Valores)
{
if (item < min) min = item;
if (item > max) max = item;
total = total + item;
}
// Calculate average last since we need the total first
float avg = total / Valores.Length;
// Update the textboxes with these values:
txtMin.Text = min.ToString();
txtMax.Text = max.ToString();
txtAvg.Text = avg.ToString();
txtTotal.Text = total.ToString();
}