计算列表框中两个值的总和

本文关键字:两个 列表 计算 | 更新日期: 2023-09-27 18:15:11

我有两个ListBoxes在WPF应用程序与学校课程和费用。但是,在运行时,应该显示两个值的总和的标签只显示最近选择的值。如何使total实际计算两个选定的值?

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;
    }