简体   繁体   English

如何使用C#4.0 Win Form Application隐藏TreeView控件中某个TreeNode的复选框?

[英]How to hide checkbox of the certain TreeNode in TreeView control using C#4.0 Win Form Application?

In my C# Windows Form Application, I have Treeview control with checkboxes. 在我的C#Windows窗体应用程序中,我有Treeview控件和复选框。

I want to hide check box of the certain tree node in TreeView control from user.How i do it?. 我想从用户隐藏TreeView控件中某个树节点的复选框。我怎么做?

Please Guide me to get out of this issue... 请指导我摆脱这个问题......

This article explains on how you can hide the checbox of a certain node in a treeview. 本文介绍如何在树视图中隐藏某个节点的checbox。

Update 更新

Explanation and code from the article: 文章中的解释和代码:

Currently, there is not build-in support to get this done. 目前,没有内置支持来完成这项工作。 But we can send a TVM_SETITEM message to the treeview control, set TVITEM structure's state field to 0, and TVITEM's hItem field to the treenode's handle. 但是我们可以向treeview控件发送TVM_SETITEM消息,将TVITEM结构的状态字段设置为0,将TVITEM的hItem字段设置为treenode的句柄。 Then this treenode will be got rid of the checkbox. 然后这个treenode将摆脱复选框。

Sample code lists below: 示例代码列表如下:

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);
}

This code hides the selected treenode's checkbox and it works well on my side. 此代码隐藏了所选的treenode的复选框,它在我身边很有效。 You may copy and paste in your project to have a test. 您可以复制并粘贴项目以进行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM