Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I had created a treecontrol using win32.Now i would like to add checkboxes to the nodes randomly .I added the checkboxes to all nodes by using TVS_CHECKBOXES.But the thing is i don't want this in this passion .It had to be up to my wish to add or remove the checkboxes.Can any one help me out to make this done.

Thanks in Advance,
Siva Vuyyuru.
Posted
Comments
[no name] 6-May-14 12:01pm    
Do you want now to add/remove them randomly or up to your wishes?...or are your wishes maybe randomly?
RajaPratap 6-May-14 12:15pm    
Please let me know how to add and remove so that i will make the changes in my project.What i mean is if come to know to how add and remove so that i can add the checkboxes at the desired nodes.


Thanks in Advance.
Siva Vuyyuru
[no name] 6-May-14 13:57pm    
You mentioned/tagged the question only with "Win32"...realy "native" WinApi?
RajaPratap 7-May-14 1:35am    
Yes Only with Win32

You have the handle to the tree window I will call it hWnd in the code

To add the check boxes
C#
DWORD dwStyle = GetWindowLong(hWnd , GWL_STYLE);
dwStyle |= TVS_CHECKBOXES;   // OR THE FLAGS ON
SetWindowLongPtr(hWnd , GWL_STYLE, dwStyle );

To remove them
C#
DWORD dwStyle = GetWindowLong(hWnd , GWL_STYLE);
dwStyle &= (~TVS_CHECKBOXES);   // AND NOT THE FLAGS OFF
SetWindowLongPtr(hWnd , GWL_STYLE, dwStyle );


DONT FORGET => Invalidate or Redraw the window (hWnd) after it to update the visual on screen
 
Share this answer
 
v5
Comments
RajaPratap 7-May-14 6:50am    
Hi,

This is how i had done.As you mentioned inorder to remove the checkbox to add that but there we are passing the handle of the tree right ,so what happening is removable of all checkboxes or presence of all checkboxes appearing.i don't want like that ,i want them in a random manner.i will attach the screenshot of how i want the tree.please let me know what correction i have to make for my code .

BOOL CALLBACK DialogProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{

switch(message)
{
case WM_INITDIALOG:
{

CHAR szResult[100] = {0};
char *p;
//wchar_t *szResult;
CHAR szName[100] = {0};
CHAR szModelName[100] = {0};
CHAR szPort[100] = {0};
CHAR szShared[100] = {0};
CHAR szSection[50] = {0};
CHAR szReturnString[50] = {0};
CHAR szModelAl[50] = {0};
CHAR szAlPort[50] = {0};
CHAR szAlShared[50] = {0};
InitCommonControls();


hTree = GetDlgItem(hWnd,IDC_TREE);
tvinsert.hParent = NULL;
tvinsert.hInsertAfter = TVI_ROOT;
tvinsert.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;

::GetPrivateProfileSectionNames(szResult,BUFSIZE,"C:\\Users\\vsiva\\Desktop\\Temp.INI");


::GetPrivateProfileString(szResult,"Model","",szName,256,"C:\\Users\\vsiva\\Desktop\\Temp.INI");


tvinsert.item.pszText = szName;
tvinsert.item.iImage = 0;
tvinsert.item.state = 0;
tvinsert.item.iSelectedImage = 1;


Parent = (HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE,TVM_INSERTITEM,0,(LPARAM)&tvinsert);
BOOL fCheck = TRUE;


Root = Parent;
Before = Parent;
tvinsert.hParent = Before;
tvinsert.hInsertAfter = TVI_LAST;
::GetPrivateProfileString(szResult,"ModelName","",szModelName,256,"C:\\Users\\vsiva\\Desktop\\Temp.INI");

tvinsert.item.pszText = szModelName ;



Parent = (HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE,TVM_INSERTITEM,0,(LPARAM)&tvinsert);

tvinsert.hParent = Before;
::GetPrivateProfileString(szResult,"Port","",szPort,256,"C:\\Users\\vsiva\\Desktop\\Temp.INI");

tvinsert.item.pszText = szPort;
Parent = (HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE,TVM_INSERTITEM,0,(LPARAM)&tvinsert);


tvinsert.hParent = Before;
tvinsert.hInsertAfter = TVI_LAST;
::GetPrivateProfileString(szResult,"Shared","",szShared,256,"C:\\Users\\vsiva\\Desktop\\Temp.INI");

tvinsert.item.pszText = szShared;
tvinsert.item.mask = TVIF_TEXT;
Parent = (HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE,TVM_INSERTITEM,0,(LPARAM)&tvinsert);


tvinsert.hParent = Before;
tvinsert.hInsertAfter = TVI_LAST;
tvinsert.item.mask=TVIF_TEXT|TVIF_IMAGE|TVIF_SELECTEDIMAGE;

DWORD dwStyle = GetWindowLong(hTree,GWL_STYLE);
dwStyle |= TVS_CHECKBOXES;//checkbox enabled for all nodes
SetWindowLong(hTree,GWL_STYLE,dwStyle);

tvinsert.hParent = NULL;
tvinsert.hInsertAfter = TVI_LAST;

p = szResult;
while(*p != '\0')
{
p++;
}
if(NULL != p)
{
p++;
}



::GetPrivateProfileString(p,"Model","",szReturnString,256,"C:\\Users\\vsiva\\Desktop\\Temp.INI");

tvinsert.item.pszText = szReturnString;
Parent = (HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE,TVM_INSERTITEM,0,(LPARAM)&tvinsert);



//DWORD dwStyle = GetWindowLong(hTree,GWL_STYLE);
//dwStyle |= TVS_CHECKBOXES;//checkbox enabled for all nodes
//SetWindowLong(hTree,GWL_STYLE,dwStyle);

Before = Parent;
tvinsert.hParent = Before;

::GetPrivateProfileString(p,"ModelName","",szModelAl,256,"C:\\Users\\vsiva\\Desktop\\Temp.INI");
tvinsert.item.pszText = szModelAl;


SetWindowLong((HWND)(tvinsert.item.hItem),GWL_STYLE,dwStyle);
Parent = (HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE,TVM_INSERTITEM,0,(LPARAM)&tvinsert);


tvinsert.hParent = Before;
::GetPrivateProfileString(p,"Port","",szAlPort,256,"C:\\Users\\vsiva\\Desktop\\Temp.INI");
tvinsert.item.pszText = szAlPort;
Parent = (HTREEITEM)SendDlgItemMessage(hWnd,IDC_TREE,TVM_INSERTITEM,0,(LPARAM)&tvinsert)
RajaPratap 7-May-14 6:54am    
[]Parent(checkbox not required for root)
.
.
.child1(checkbox required)
.
.
.child2(checkbox not required)
.
.
.
.child3(checkbox required)
RajaPratap 7-May-14 6:56am    
This is what i want.Can anyone please let me know how to acheive this.i would be greatful if you could help me out to make this done.

Thanks in Advance,
Siva V.
Okay what you are asking for is a ListBox with option of a per item checkbox

Short answer is that ability doesn't exist in native WIN32 API you have to build it yourself.

In the MFC framework it is called a CCheckBoxList and it is the framework doing all the work.

If you want tackle it you have to make an OWNERDRAW list box or subclass the listbox.

If you want to have a crack you may want to look here where I helped another user who was doing a subclassed listbox

Incorrect result with TransparentBlt[^]
 
Share this answer
 
Comments
RajaPratap 7-May-14 8:09am    
No no you misunderstood me i want that scenario in treeview only not in the listview.
RajaPratap 7-May-14 9:25am    
[]Parent(checkbox not required for the parent node)
.
.
.Child1(Checkbox Required)
.
.
.
.
.Child2(checkbox not required)
.
.
.
.
.
.child3(checkbox required)
RajaPratap 7-May-14 9:30am    
I will provide you a link where the UI pic is present .I want the tree control as it is in that diagram by looking that i hope you better come to know what i want.So,please have a look at this http://dotnetfollower.com/wordpress/2011/05/winforms-treeview-hide-checkbox-of-treenode/ and please let me know the right approach.
RajaPratap 7-May-14 9:30am    
http://dotnetfollower.com/wordpress/2011/05/winforms-treeview-hide-checkbox-of-treenode/
RajaPratap 7-May-14 9:34am    
Very sorry please have a look at this URL for the image

http://dotnetfollower.com/wordpress/wordpress-content/uploads/2011/05/MixedCheckboxTreeView.png

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900