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

发布于 2024-12-02 02:21:10 字数 121 浏览 0 评论 0原文

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

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

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

In my C# Windows Form Application, I have Treeview control with checkboxes.

I want to hide check box of the certain tree node in TreeView control from user.How i do it?.

Please Guide me to get out of this issue...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2024-12-09 02:21:10

本文介绍了如何您可以隐藏树视图中某个节点的复选框。

更新

文章中的说明和代码:

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

下面列出了示例代码:

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 article explains on how you can hide the checbox of a certain node in a treeview.

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. Then this
treenode will be got rid of the checkbox.

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. You may copy and paste in your project to have a test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文