C#WPF组合框平均 [英] c# wpf combobox average
问题描述
我要为我的项目一个简单的代码,当我点击在文本框按钮平均值应出现选择的号码的平均值在所有组合框
I want a simple code for my project , when i click in button average in textbox should appear the average of selected numbers in all combobox
<ComboBox Name="cbnota6" Margin="0,9,0,0">
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="6"/>
<ComboBoxItem Content="7"/>
<ComboBoxItem Content="8"/>
<ComboBoxItem Content="9"/>
<ComboBoxItem Content="10"/>
</ComboBox>
<ComboBox Name="cbnota7" Margin="0,9,0,0">
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="6"/>
<ComboBoxItem Content="7"/>
<ComboBoxItem Content="8"/>
<ComboBoxItem Content="9"/>
<ComboBoxItem Content="10"/>
</ComboBox>
<ComboBox Name="cbnota8" Margin="0,9,0,0">
<ComboBoxItem Content="5"/>
<ComboBoxItem Content="6"/>
<ComboBoxItem Content="7"/>
<ComboBoxItem Content="8"/>
<ComboBoxItem Content="9"/>
<ComboBoxItem Content="10"/>
</ComboBox>
<Button Name="btnmesatarja" Content="Mesatarja" Width="80" Margin="0,10,0,0" Click="btnmesatarja_Click" />
<TextBox Name="txtmesatarja" Width="100" Margin="0,4,0,0"/>
所以可能你有人告诉怎么做,我问
So could someone of you tell how to do that what i asked
推荐答案
您可以使用的foreach
遍历每个组合框来遍历获得所有项目的总和内这些组合框,把它保存在这些组合框的所有项目的计数一起的变量。
You can use a foreach
loop to traverse through each combobox to get the sum of all the items inside those comboboxes, keep it saved in a variable along with the count of all items in those comboboxes.
类似
int intCBAverage;
int intCBSum = 0;
int intCBCount = 0;
foreach (var item in cbnota6.Items)
{
intCBSum += int.Parse(item.ToString());
intCBCount++;
}
foreach (var item in cbnota7.Items)
{
intCBSum += int.Parse(item.ToString());
intCBCount++;
}
foreach (var item in cbnota8.Items)
{
intCBSum += int.Parse(item.ToString());
intCBCount++;
}
intCBAverage = intCBSum / intCBCount;
上面这段代码只是为理念的例证。希望你有这个想法。
The above piece of code is just for illustration of the idea. Hope you got the idea.
一旦你的平均值,则可以显示在教科书。
Once you have the average, you can show that in the textbook.
此外,而不是通过每个循环的组合框,您可以通过在表单每个控制回路,检查控制是否是组合框与否,如果是,再通过组合框的项目循环。类似
Also, instead of looping through each combobox you can loop through each control in the form, check whether the control is a combobox or not and if yes, then loop through the combobox items. Something like
foreach (Control x in this.Controls)
{
if (x is ComboBox)
{
foreach (var item in x.Items)
{
intCBSum += int.Parse(item.ToString());
intCBCount++;
}
}
}
这篇关于C#WPF组合框平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!