在C#中怎样禁止ListView控件Details视图中双击某个项时复选框会自动设置或取消的这个功能呢? 15
我在使用ListView控件时需要在项旁边显示复选框,当我双击某个项时复选框会自动设置或取消,我应怎样关掉这个功能呢?我需要在双击项时复选框的状态不能改变,只有鼠标在复选...
我在使用ListView控件时需要在项旁边显示复选框,当我双击某个项时复选框会自动设置或取消,我应怎样关掉这个功能呢?我需要在双击项时复选框的状态不能改变,只有鼠标在复选框上单击时才允许复选框的值改变。请知道的朋友指教一下,非常的感谢!
我继承ListView控件后重写双击事件也不能解决问题。
我只是希望在我双击某个项时复选框的状态不会自动改变。就像苹果的iTunes音乐播放器的播放列表一样。 展开
我继承ListView控件后重写双击事件也不能解决问题。
我只是希望在我双击某个项时复选框的状态不会自动改变。就像苹果的iTunes音乐播放器的播放列表一样。 展开
展开全部
没有在复选框上单击的事件,它与ListViewItem是捆绑在一起的.
给你提供一个解决方案:
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;
}
}
}
}
给你提供一个解决方案:
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;
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询