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

    你可能感兴趣的文章
    nginx-vts + prometheus 监控nginx
    查看>>
    Nginx/Apache反向代理
    查看>>
    Nginx: 413 – Request Entity Too Large Error and Solution
    查看>>
    nginx: [emerg] getpwnam(“www”) failed 错误处理方法
    查看>>
    nginx: [emerg] the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:
    查看>>
    nginx: [error] open() “/usr/local/nginx/logs/nginx.pid“ failed (2: No such file or directory)
    查看>>
    nginx:Error ./configure: error: the HTTP rewrite module requires the PCRE library
    查看>>
    Nginx:objs/Makefile:432: recipe for target ‘objs/src/core/ngx_murmurhash.o‘解决方法
    查看>>
    nginxWebUI runCmd RCE漏洞复现
    查看>>
    nginx_rtmp
    查看>>
    Vue中向js中传递参数并在js中定义对象并转换参数
    查看>>
    Nginx、HAProxy、LVS
    查看>>
    nginx一些重要配置说明
    查看>>
    Nginx下配置codeigniter框架方法
    查看>>
    Nginx与Tengine安装和使用以及配置健康节点检测
    查看>>
    Nginx中使用expires指令实现配置浏览器缓存
    查看>>
    Nginx中使用keepalive实现保持上游长连接实现提高吞吐量示例与测试
    查看>>
    Nginx中如何配置WebSocket代理?
    查看>>
    Nginx中实现流量控制(限制给定时间内HTTP请求的数量)示例
    查看>>
    nginx中配置root和alias的区别
    查看>>