在C#中怎样禁止ListView控件Details视图中双击某个项时复选框会自动设置或取消的这个功能呢?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在C#中怎样禁止ListView控件Details视图中双击某个项时复选框会自动设置或取消的这个功能呢?相关的知识,希望对你有一定的参考价值。
我在使用ListView控件时需要在项旁边显示复选框,当我双击某个项时复选框会自动设置或取消,我应怎样关掉这个功能呢?我需要在双击项时复选框的状态不能改变,只有鼠标在复选框上单击时才允许复选框的值改变。请知道的朋友指教一下,非常的感谢!
我继承ListView控件后重写双击事件也不能解决问题。
我只是希望在我双击某个项时复选框的状态不会自动改变。就像苹果的iTunes音乐播放器的播放列表一样。
给你提供一个解决方案:
1 双击时将选中的值的check状态改成false
2 在选中的值添加右键菜单,此时可以将选中值的check状态改成true;
代码实现:
(该代码可以直接运行.)
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
public partial class Form1 : Form
public Form1()
InitializeComponent();
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.components = new System.ComponentModel.Container();
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("BBB");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem(new string[]
"AAA",
"A1",
"A2",
"A3", -1);
this.listView1 = new System.Windows.Forms.ListView();
this.contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip2.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.Alignment = System.Windows.Forms.ListViewAlignment.Default;
this.listView1.CheckBoxes = true;
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
listViewItem1.StateImageIndex = 0;
listViewItem1.ToolTipText = "BBB";
listViewItem2.StateImageIndex = 0;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[]
listViewItem1,
listViewItem2);
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.ShowItemToolTips = true;
this.listView1.Size = new System.Drawing.Size(286, 165);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.DoubleClick += new System.EventHandler(this.listView1_DoubleClick);
this.listView1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listView1_MouseDown);
//
// contextMenuStrip2
//
this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
this.toolStripMenuItem1);
this.contextMenuStrip2.Name = "contextMenuStrip1";
this.contextMenuStrip2.Size = new System.Drawing.Size(95, 26);
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(94, 22);
this.toolStripMenuItem1.Text = "选中";
this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(286, 165);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
this.contextMenuStrip2.ResumeLayout(false);
this.ResumeLayout(false);
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip2;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private void listView1_DoubleClick(object sender, EventArgs e)
if (this.listView1.SelectedItems.Count == 1)
this.listView1.SelectedItems[0].Checked = false;
private void listView1_MouseDown(object sender, MouseEventArgs e)
if (e.Button == MouseButtons.Right)
if (this.listView1.SelectedItems.Count == 1)
this.contextMenuStrip2.Show(this.listView1, e.Location);
private void toolStripMenuItem1_Click(object sender, EventArgs e)
if (this.listView1.SelectedItems.Count == 1)
this.listView1.SelectedItems[0].Checked = true;
参考技术A 在asp.net中,我们采用javascript的document.getElementById方法,可以轻松实现你要的结果。 参考技术B 你可以在复选框点击事件中控制要操作的东西。
如何在 C#中的listView 控件中显示图片?
图标加数字
首先你需要在WinForm 中添加一个imagelist 控件。
在imagelist控件的图片集合中添加要显示的图片。
我的listView 中 View 属性 设置为 details(其它的没试)。然后将ListView的SmallImageList 属性赋值为添加的imagelist控件名。之后:
listView1.SmallImageList=ImageListName; // SmallImageList 属性
ListViewItem lv =newListViewItem();
lv.ImageIndex=0;//显示imageList中的第一张图片
lv.SubItems.Add("000");
lv.SubItems.Add("111");
lv.SubItems.Add("222");
listView1.Items.Add(lv);
或者
int i = skinListView.Items.Count;
listView1.Items.Add("000");
listView1.Items[i].SubItems.Add("111");
listView1.Items[i].SubItems.Add("222");
listView1.Items[i].ImageIndex = 0;//显示imageList中的第一张图片
这样就在行的第一列加了个图片。
大图标形式
以上是在View 为 details 时,在第一列显示图片,下载在LargeIcon 下让ListView中的每个项目显示图片
- 添加ImageList 控件 这种相关属性:
主要设置了要显示的图片,及其要显示的大小:
添加如下代码:
lvwIcon.View = View.LargeIcon;
lvwIcon.LargeImageList = imageList1;
for (int i = 0; i < 4; i++)
{
lvwIcon.Items.Add("图片" + (i+1));
lvwIcon.Items[i].ImageIndex = i;
}
以上是关于在C#中怎样禁止ListView控件Details视图中双击某个项时复选框会自动设置或取消的这个功能呢?的主要内容,如果未能解决你的问题,请参考以下文章
asp.net(C#)怎样将listview中的radiobutton设置成互斥?
C# 怎样将ListView中的图片复制到richTextBox中去?