繁体   English   中英

计算ListBoxes中两个值的总和?

[英]Calculate total of two values in ListBoxes?

我在WPF应用程序中有两个带有学校课程和费用的ListBoxes 但是,运行时,我应该显示两个值的总和的标签仅显示最近选择的值。 我如何才能使总数真正计算出两个选定的值?

XAML:

<Grid>
    <ListBox x:Name="courseListBox" SelectionChanged="courseListBox_SelectionChanged" HorizontalAlignment="Left" Height="126" VerticalAlignment="Top" Width="120" Margin="42,35,0,0">
        <ListBoxItem>Intro to Comp Sci</ListBoxItem>
        <ListBoxItem>Honors Comp Sci</ListBoxItem>
        <ListBoxItem>AP Comp Sci A</ListBoxItem>
        <ListBoxItem>AP Comp Sci P</ListBoxItem>
        <ListBoxItem>Independent Study</ListBoxItem>
        <ListBoxItem>Webpage Design</ListBoxItem>
    </ListBox>

    <ListBox x:Name="classListBox" SelectionChanged="classListBox_SelectionChanged" HorizontalAlignment="Left" Height="85" Margin="42,197,0,0" VerticalAlignment="Top" Width="120">
        <ListBoxItem>Freshman</ListBoxItem>
        <ListBoxItem>Sophomore</ListBoxItem>
        <ListBoxItem>Junior</ListBoxItem>
        <ListBoxItem>Senior</ListBoxItem>
    </ListBox>
    <Label x:Name="totalLabel" Content="25" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="168,34,0,0"/>
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" Width="120" Margin="42,9,0,0"/>
    <Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" Width="120" Margin="42,169,0,0"/>

</Grid>

C#:

public partial class MainWindow : Window
{
    public int courseFee = 0;
    public int classFee = 0;
    public int totalCost = 0;
    public MainWindow()
    {
        InitializeComponent();
    }
    private void courseListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selected = courseListBox.SelectedIndex;
        if (selected == 0)
            classFee = 545;
        if (selected == 1)
            classFee = 615;
        if (selected == 2)
            classFee = 1500;
        if (selected == 3)
            classFee = 1000;
        if (selected == 4)
            classFee = 2500;
        if (selected == 5)
            classFee = 1720;
        totalCost = courseFee + classFee;
        totalLabel.Content = totalCost;
    }

    private void classListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selected = classListBox.SelectedIndex;
        if (selected == 0)
            classFee = 350;
        if (selected == 1)
            classFee = 275;
        if (selected == 2)
            classFee = 200;
        if (selected == 3)
            classFee = 150;
        totalCost = courseFee + classFee;
        totalLabel.Content = totalCost;
    }
}

服务器是无状态的,因此您无法记住回发之间的费用值。 只需结合两个过程。

protected void getTotal() 
    {
        int selected = courseListBox.SelectedIndex;
        if (selected == 0)
            classFee = 545;
        if (selected == 1)
            classFee = 615;
        if (selected == 2)
            classFee = 1500;
        if (selected == 3)
            classFee = 1000;
        if (selected == 4)
            classFee = 2500;
        if (selected == 5)
            classFee = 1720;
        int total = classFee;
        selected = classListBox.SelectedIndex;
        if (selected == 0)
            classFee = 350;
        if (selected == 1)
            classFee = 275;
        if (selected == 2)
            classFee = 200;
        if (selected == 3)
            classFee = 150;
        total += classFee;
        totalLabel.Content = total;
    }

    private void classListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
       getTotal();
    }

(使两个列表框使用相同的事件处理程序)

好吧,开始时可能是将courseListBox_SelectionChanged方法中的classFee更改为courseFree的一个好主意

代码可能像这样...

int value = 0;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                value += int.Parse(listView1.Items[i].SubItems[e.Column].Text);
            }

            textBox1.Text = value.ToString();

我认为您的意思是课程费用,而不是像courseListBox_SelectionChanged这样的classFee

private void courseListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selected = courseListBox.SelectedIndex;
        if (selected == 0)
            courseFee = 545;
        if (selected == 1)
            courseFee = 615;
        if (selected == 2)
            courseFee = 1500;
        if (selected == 3)
            courseFee = 1000;
        if (selected == 4)
            courseFee = 2500;
        if (selected == 5)
            courseFee = 1720;
        totalCost = courseFee + classFee;
        totalLabel.Content = totalCost;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM