【问题标题】:How to hide checkbox of the certain TreeNode in TreeView control using C#4.0 Win Form Application?如何使用 C#4.0 Win Form Application 在 TreeView 控件中隐藏某个 TreeNode 的复选框?
【发布时间】:2011-11-04 11:20:03
【问题描述】:

在我的 C# Windows 窗体应用程序中,我有带有复选框的 Treeview 控件。

我想对用户隐藏 TreeView 控件中某个树节点的复选框。我该怎么做?

请指导我摆脱这个问题...

【问题讨论】:

标签: c# .net winforms c#-4.0


【解决方案1】:

This article 解释了如何在树形视图中隐藏某个节点的复选框。

更新

文章中的解释和代码:

目前,没有内置支持来完成这项工作。但是我们可以发送 向树视图控件发送 TVM_SETITEM 消息,设置 TVITEM 结构的状态 字段为 0,TVITEM 的 hItem 字段为树节点的句柄。那么这个 treenode 将去掉复选框。

下面的示例代码列表:

public const int TVIF_STATE = 0x8;
public const int TVIS_STATEIMAGEMASK = 0xF000;
public const int TV_FIRST= 0x1100;
public const int  TVM_SETITEM = TV_FIRST + 63;

public struct TVITEM
{
    public int mask;
    public IntPtr hItem;
    public int state;
    public int stateMask;
    [MarshalAs(UnmanagedType.LPTStr)]
    public String lpszText;
    public int cchTextMax;
    public int iImage;
    public int iSelectedImage;
    public int cChildren;
    public IntPtr lParam;
}

[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

private void button1_Click(object sender, System.EventArgs e)
{
    TVITEM tvi=new TVITEM();
    tvi.hItem=this.treeView1.SelectedNode.Handle;
    tvi.mask=TVIF_STATE;
    tvi.stateMask = TVIS_STATEIMAGEMASK;
    tvi.state=0;
    IntPtr lparam=Marshal.AllocHGlobal(Marshal.SizeOf(tvi));
    Marshal.StructureToPtr(tvi, lparam, false);
    SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
}

此代码隐藏了选定树节点的复选框,它在我的 边。您可以复制粘贴到您的项目中进行测试。

【讨论】:

  • 好一个..你能解释一下如何取消隐藏复选框吗?提前谢谢你
猜你喜欢
  • 2012-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 2013-12-23
  • 2010-10-17
相关资源
最近更新 更多