博客
关于我
ListView控件演示08:获取列表中的所有选定项
阅读量:533 次
发布时间:2019-03-08

本文共 2371 字,大约阅读时间需要 7 分钟。

ListView 控件的 SelectedItems 属性是操作 ListView 组件时非常实用的属性,用于获取用户选定的列表项集合。本文将通过 C# 代码示例,展示如何在 Windows 弹性应用程序中使用该属性并结合事件处理程序实现计算功能。

代码结构如下:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace Demo08{    public partial class MainForm : Form    {        public MainForm()        {            InitializeComponent();        }        ///         /// 初始化列表中的数据。        ///         ///         ///         private void MainForm_Load(object sender, EventArgs e)        {            string[] FoodList = new string[] { "Juice", "Coffee", "Cereal & Milk", "Fruit Plate", "Toast & Jelly", "Bagel & Cream Cheese" };            string[] FoodPrice = new string[] { "1.09", "1.09", "2.19", "2.49", "1.49", "1.49" };            this.BreakfastListView.BeginUpdate();            for (int Index = 0; Index < FoodList.Length; Index++)            {                ListViewItem Item = new ListViewItem();                Item.Text = FoodList[Index];                Item.SubItems.Add(FoodPrice[Index]);                this.BreakfastListView.Items.Add(Item);            }            this.BreakfastListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);            this.BreakfastListView.EndUpdate();        }        ///         /// 计算用户选中的早餐所需的花费。        ///         ///         ///         private void CalculateButton_Click(object sender, EventArgs e)        {            double Price = 0;            ListView.SelectedListViewItemCollection SelectedItems = this.BreakfastListView.SelectedItems;            foreach (ListViewItem Item in SelectedItems)            {                double ItemPrice = double.Parse(Item.SubItems[1].Text);                Price += ItemPrice;            }            MessageBox.Show(string.Format("所选中的早餐需要花费{0}人民币!", Price));        }    }}

代码解释:

  • Data Source Initialization

    • 存储早餐项目数据。
    • 存储每个项目的具体价格。
  • Loading Data to ListView

    • 使用 BeginUpdate 方法避免刷新 UI。
    • 遍历数据数组,添加新的 ListViewItem
    • 每个项目包含两部分内容:名称和价格。
  • Resizing Columns

    • 调整列宽度至最优状态,确保用户可以清楚地查看所有信息。
  • Calculating Total Cost

    • 使用 LVQ SelectedItems 获取用户选定的项目集合。
    • 遍历集合项,累加选定的项目价格。
    • 带动消息框显示总费用。
  • 整个代码框架清晰易懂,适合后台管理或用户交互相关的应用场景。通过实际操作可以快速实现类似功能。

    项目中使用的关键技术包括:

    • ListView 控件:适合网格形式的数据展示。
    • SelectedItemCollection:获取用户选定的项目集合。
    • 事件处理程序:响应用户操作并执行计算功能。

    总体来说,这段代码展示了如何利用 ListView 组件与事件处理机制,实现用户交互和数据计算结合的应用场景。

    转载地址:http://ldtiz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现boruvka博鲁夫卡算法(附完整源码)
    查看>>
    Objective-C实现Boyer-Moore字符串搜索算法(附完整源码)
    查看>>
    Objective-C实现BP误差逆传播算法(附完整源码)
    查看>>
    Objective-C实现breadth First Search广度优先搜索算法(附完整源码))
    查看>>
    Objective-C实现BreadthFirstSearch广度优先搜索算法(附完整源码)
    查看>>
    Objective-C实现BreadthFirstShortestPath广度优先最短路径算法(附完整源码)
    查看>>
    Objective-C实现bubble sort冒泡排序算法(附完整源码)
    查看>>
    Objective-C实现bucket sort桶排序算法(附完整源码)
    查看>>
    Objective-C实现Burke 抖动算法(附完整源码)
    查看>>
    Objective-C实现Burrows-Wheeler 算法(附完整源码)
    查看>>
    Objective-C实现CaesarsCiphe凯撒密码算法(附完整源码)
    查看>>
    Objective-C实现calloc函数功能(附完整源码)
    查看>>
    Objective-C实现canny边缘检测算法(附完整源码)
    查看>>
    Objective-C实现cartesianProduct笛卡尔乘积算法(附完整源码)
    查看>>
    Objective-C实现check strong password检查密码强度算法(附完整源码)
    查看>>
    Objective-C实现chudnovsky algorithm楚德诺夫斯基算法(附完整源码)
    查看>>
    Objective-C实现CIC滤波器(附完整源码)
    查看>>
    Objective-C实现circle sort圆形排序算法(附完整源码)
    查看>>
    Objective-C实现CircularQueue循环队列算法(附完整源码)
    查看>>
    Objective-C实现clearBit清除位算法(附完整源码)
    查看>>