Archive for the ‘Treeview’ Category

How to search Node in Treeview Control
  1.  Private Function search(ByVal Nod As TreeNode, ByVal StrFind As String) As TreeNode
  2.         If Nod Is Nothing Then Nod = Me.Nodes(0)
  3.         Dim tmpNod As TreeNode
  4.  
  5.         If String.Compare(Nod.Text, StrFind, True) = 0 Then
  6.             Return Nod
  7.         Else
  8.             For Each mNod As TreeNode In Nod.Nodes
  9.                 tmpNod = search(mNod, StrFind)
  10.                 If Not tmpNod Is Nothing Then
  11.                     Return tmpNod
  12.                 End If
  13.             Next
  14.         End If
  15.         Return Nothing
  16.     End Function

Usage:

  1. dim nod as TreeNode
  2.  
  3. nod = search(me.MyTreeViewControl.Nodes(0), "My search")

, ,

No Comments


How to remove parent node checkbox of the Treeview

Add this script to your common module
VB.NET

Imports System.Runtime.InteropServices
  1.  
  2. Public Const TVIF_STATE As Integer = &H8
  3. Public Const TVIS_STATEIMAGEMASK As Integer = &HF000
  4. Public Const TV_FIRST As Integer = &H1100
  5. Public Const TVM_SETITEM As Integer = TV_FIRST + 63
  6.  
  7.  Public Structure TVITEM
  8.     Public mask As Integer
  9.     Public hItem As IntPtr
  10.     Public state As Integer
  11.     Public stateMask As Integer
  12.      Public lpszText As String
  13.     Public cchTextMax As Integer
  14.     Public iImage As Integer
  15.     Public iSelectedImage As Integer
  16.     Public cChildren As Integer
  17.     Public lParam As IntPtr
  18. End Structure
  19.  
  20.     Public Sub RemoveCheckbox(ByVal nod As SiteTreeNode)
  21.         Dim tvi As TVITEM
  22.         tvi.hItem = nod.Handle
  23.         tvi.mask = TVIF_STATE
  24.         tvi.stateMask = TVIS_STATEIMAGEMASK
  25.         tvi.state = nod.Index << 12
  26.         SendMessage(nod.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, tvi)
  27.     End Sub

Then call the subroutine
Public Sub TreeNode_SetStateImageIndex(ByVal node As TreeNode, ByVal index
As Integer)
RemoveCheckbox(node)
End Sub

C#

  1.  
  2. using System.Runtime.InteropServices;
  3.  
  4. namespace Treeview___CheckBoxes
  5. {
  6.  public partial class Form1 : Form
  7.  {
  8.   public Form1()
  9.   {
  10.  InitializeComponent();
  11.   }
  12.  private void Form1_Load(object sender, EventArgs e)
  13.  {
  14.  // Iterate over the root nodes, removing their checkboxes
  15.  for (int n = 0; n<treeView1.Nodes.Count; n++)
  16.  {
  17.   TreeNode node = treeView1.Nodes[n];
  18.   TVITEM tvItem = new TVITEM();
  19.   tvItem.hItem = node.Handle;
  20.   tvItem.mask = TVIF_STATE;
  21.   tvItem.stateMask = TVIS_STATEIMAGEMASK;
  22.   tvItem.state = 0;
  23.   IntPtr lparam = Marshal.AllocHGlobal(Marshal.SizeOf(tvItem));
  24.   Marshal.StructureToPtr(tvItem, lparam, false);
  25.   SendMessage(this.treeView1.Handle, TVM_SETITEM, IntPtr.Zero, lparam);
  26.  }
  27.  }
  28.  public const int TVIF_STATE = 0×8;
  29.  public const int TVIS_STATEIMAGEMASK = 0xF000;
  30.  public const int TV_FIRST = 0×1100;
  31.  public const int TVM_SETITEM = TV_FIRST + 63;
  32.  public struct TVITEM
  33.  {
  34.   public int mask;
  35.   public IntPtr hItem;
  36.   public int state;
  37.   public int stateMask;
  38.   [MarshalAs(UnmanagedType.LPTStr)]
  39.   public String lpszText;
  40.   public int cchTextMax;
  41.   public int iImage;
  42.   public int iSelectedImage;
  43.   public int cChildren;
  44.   public IntPtr lParam;
  45.  }
  46.   [DllImport(“user32.dll)]  static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  47.  
  48.  }
  49. }

Sources: c#  | vb.net

, , ,

No Comments



SetPageWidth