博客
关于我
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有感)
    查看>>
    MySQL数据库面试题(2021最新版)
    查看>>
    MySQL数据库高并发优化配置
    查看>>
    mysql数据恢复
    查看>>
    MySQL数据的主从复制、半同步复制和主主复制详解
    查看>>
    mysql数据碎片整理
    查看>>
    MySQL数据类型
    查看>>
    MySQL数据类型字节长度
    查看>>
    mysql数据被误删的恢复方案
    查看>>
    MySQL数据读写分离(MaxScale)上干货!!!
    查看>>
    mysql整库导入、导出
    查看>>
    mysql文本函数和数字函数
    查看>>
    Mysql新建用户和数据库并授权
    查看>>
    mysql日志
    查看>>
    mysql日志 事务问题_mysql因为事务日志问题无法启动
    查看>>
    mysql日志文件
    查看>>
    mysql日志管理学习笔记
    查看>>
    mysql日志问题定位实用命令
    查看>>
    MySQL日期时间函数大全
    查看>>
    mysql时间相减的问题
    查看>>