博客
关于我
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 常用命令
    查看>>
    Mysql 常见ALTER TABLE操作
    查看>>
    mysql 往字段后面加字符串
    查看>>
    mysql 快速自增假数据, 新增假数据,mysql自增假数据
    查看>>
    Mysql 报错 Field 'id' doesn't have a default value
    查看>>
    MySQL 报错:Duplicate entry 'xxx' for key 'UNIQ_XXXX'
    查看>>
    mysql 排序id_mysql如何按特定id排序
    查看>>
    Mysql 提示:Communication link failure
    查看>>
    mysql 插入是否成功_PDO mysql:如何知道插入是否成功
    查看>>
    Mysql 数据库InnoDB存储引擎中主要组件的刷新清理条件:脏页、RedoLog重做日志、Insert Buffer或ChangeBuffer、Undo Log
    查看>>
    mysql 数据库备份及ibdata1的瘦身
    查看>>
    MySQL 数据库备份种类以及常用备份工具汇总
    查看>>
    mysql 数据库存储引擎怎么选择?快来看看性能测试吧
    查看>>
    MySQL 数据库操作指南:学习如何使用 Python 进行增删改查操作
    查看>>
    MySQL 数据库的高可用性分析
    查看>>
    Mysql 数据库重置ID排序
    查看>>
    Mysql 数据类型一日期
    查看>>
    MySQL 数据类型和属性
    查看>>
    mysql 敲错命令 想取消怎么办?
    查看>>
    Mysql 整形列的字节与存储范围
    查看>>