博客
关于我
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/

    你可能感兴趣的文章
    MySQL基础系列—SQL分类之一
    查看>>
    MySQL处理千万级数据分页查询的优化方案
    查看>>
    mysql备份
    查看>>
    mysql备份与恢复
    查看>>
    mysql备份工具xtrabackup
    查看>>
    mysql备份恢复出错_尝试备份/恢复mysql数据库时出错
    查看>>
    mysql复制内容到一张新表
    查看>>
    mysql复制表结构和数据
    查看>>
    mysql复杂查询,优质题目
    查看>>
    MySQL外键约束
    查看>>
    MySQL多表关联on和where速度对比实测谁更快
    查看>>
    MySQL多表左右连接查询
    查看>>
    mysql大批量删除(修改)The total number of locks exceeds the lock table size 错误的解决办法
    查看>>
    mysql如何做到存在就更新不存就插入_MySQL 索引及优化实战(二)
    查看>>
    mysql如何删除数据表,被关联的数据表如何删除呢
    查看>>
    MySQL如何实现ACID ?
    查看>>
    mysql如何记录数据库响应时间
    查看>>
    MySQL子查询
    查看>>
    Mysql字段、索引操作
    查看>>
    mysql字段的细节(查询自定义的字段[意义-行列转置];UNION ALL;case-when)
    查看>>