How to sync ListView checkbox with selection?(WIN32)
up vote
2
down vote
favorite
I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?
c windows listview winapi win32gui
add a comment |
up vote
2
down vote
favorite
I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?
c windows listview winapi win32gui
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?
c windows listview winapi win32gui
I'm writing a simple Win32 program that has a ListView with check box and multi-row selection enabled. :
It seems like checkbox checks and row selections are two distinct behaviors. Is there a way to sync these two behaviors, that whenever a check box is checked that row will be selected and whenever a row selected the corresponding checkbox will be check?
c windows listview winapi win32gui
c windows listview winapi win32gui
edited May 21 '16 at 4:52
Barmak Shemirani
20.3k42044
20.3k42044
asked May 20 '16 at 21:21
Kelvin Zhang
380210
380210
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
"whenever a row is selected, the corresponding checkbox will be checked"
Check WM_NOTIFY
and LVIS_SELECTED
flag to detect when user selects a row. And use ListView_SetCheckState
to check the check-box:
BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hListView;
switch (msg)
{
case WM_INITDIALOG:
hListView = GetDlgItem(hWnd, IDC_LIST1);
break;
case WM_NOTIFY:
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
if (nmlist->uNewState & LVIS_SELECTED)
ListView_SetCheckState(hListView, nmlist->iItem, 1);
break;
}
...
}
"whenever a check box is checked that row will be selected"
Check WM_NOTIFY
and LVIS_STATEIMAGEMASK
flag to detect when checkbox is checked, then use ListView_SetItemState
to select the row.
Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy
variable to stop recursive calls.
case WM_NOTIFY:
if (lParam)
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
//use `busy` as a flag to prevent recursive calls:
static BOOL busy = FALSE;
if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
{
busy = TRUE;
if (nmlist->uNewState & LVIS_SELECTED)
{
//row has been selected => check the checkbox
ListView_SetCheckState(hListView, nmlist->iItem, 1);
}
else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
{
//checkbox has been changed => select/unselect the row
BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
ListView_SetItemState(hListView, nmlist->iItem,
checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
}
busy = FALSE;
}
}
break;
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
"whenever a row is selected, the corresponding checkbox will be checked"
Check WM_NOTIFY
and LVIS_SELECTED
flag to detect when user selects a row. And use ListView_SetCheckState
to check the check-box:
BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hListView;
switch (msg)
{
case WM_INITDIALOG:
hListView = GetDlgItem(hWnd, IDC_LIST1);
break;
case WM_NOTIFY:
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
if (nmlist->uNewState & LVIS_SELECTED)
ListView_SetCheckState(hListView, nmlist->iItem, 1);
break;
}
...
}
"whenever a check box is checked that row will be selected"
Check WM_NOTIFY
and LVIS_STATEIMAGEMASK
flag to detect when checkbox is checked, then use ListView_SetItemState
to select the row.
Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy
variable to stop recursive calls.
case WM_NOTIFY:
if (lParam)
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
//use `busy` as a flag to prevent recursive calls:
static BOOL busy = FALSE;
if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
{
busy = TRUE;
if (nmlist->uNewState & LVIS_SELECTED)
{
//row has been selected => check the checkbox
ListView_SetCheckState(hListView, nmlist->iItem, 1);
}
else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
{
//checkbox has been changed => select/unselect the row
BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
ListView_SetItemState(hListView, nmlist->iItem,
checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
}
busy = FALSE;
}
}
break;
add a comment |
up vote
2
down vote
accepted
"whenever a row is selected, the corresponding checkbox will be checked"
Check WM_NOTIFY
and LVIS_SELECTED
flag to detect when user selects a row. And use ListView_SetCheckState
to check the check-box:
BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hListView;
switch (msg)
{
case WM_INITDIALOG:
hListView = GetDlgItem(hWnd, IDC_LIST1);
break;
case WM_NOTIFY:
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
if (nmlist->uNewState & LVIS_SELECTED)
ListView_SetCheckState(hListView, nmlist->iItem, 1);
break;
}
...
}
"whenever a check box is checked that row will be selected"
Check WM_NOTIFY
and LVIS_STATEIMAGEMASK
flag to detect when checkbox is checked, then use ListView_SetItemState
to select the row.
Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy
variable to stop recursive calls.
case WM_NOTIFY:
if (lParam)
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
//use `busy` as a flag to prevent recursive calls:
static BOOL busy = FALSE;
if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
{
busy = TRUE;
if (nmlist->uNewState & LVIS_SELECTED)
{
//row has been selected => check the checkbox
ListView_SetCheckState(hListView, nmlist->iItem, 1);
}
else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
{
//checkbox has been changed => select/unselect the row
BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
ListView_SetItemState(hListView, nmlist->iItem,
checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
}
busy = FALSE;
}
}
break;
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
"whenever a row is selected, the corresponding checkbox will be checked"
Check WM_NOTIFY
and LVIS_SELECTED
flag to detect when user selects a row. And use ListView_SetCheckState
to check the check-box:
BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hListView;
switch (msg)
{
case WM_INITDIALOG:
hListView = GetDlgItem(hWnd, IDC_LIST1);
break;
case WM_NOTIFY:
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
if (nmlist->uNewState & LVIS_SELECTED)
ListView_SetCheckState(hListView, nmlist->iItem, 1);
break;
}
...
}
"whenever a check box is checked that row will be selected"
Check WM_NOTIFY
and LVIS_STATEIMAGEMASK
flag to detect when checkbox is checked, then use ListView_SetItemState
to select the row.
Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy
variable to stop recursive calls.
case WM_NOTIFY:
if (lParam)
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
//use `busy` as a flag to prevent recursive calls:
static BOOL busy = FALSE;
if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
{
busy = TRUE;
if (nmlist->uNewState & LVIS_SELECTED)
{
//row has been selected => check the checkbox
ListView_SetCheckState(hListView, nmlist->iItem, 1);
}
else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
{
//checkbox has been changed => select/unselect the row
BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
ListView_SetItemState(hListView, nmlist->iItem,
checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
}
busy = FALSE;
}
}
break;
"whenever a row is selected, the corresponding checkbox will be checked"
Check WM_NOTIFY
and LVIS_SELECTED
flag to detect when user selects a row. And use ListView_SetCheckState
to check the check-box:
BOOL CALLBACK DialogProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HWND hListView;
switch (msg)
{
case WM_INITDIALOG:
hListView = GetDlgItem(hWnd, IDC_LIST1);
break;
case WM_NOTIFY:
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
if (header && header->idFrom == IDC_LIST1 && header->code == LVN_ITEMCHANGED)
if (nmlist->uNewState & LVIS_SELECTED)
ListView_SetCheckState(hListView, nmlist->iItem, 1);
break;
}
...
}
"whenever a check box is checked that row will be selected"
Check WM_NOTIFY
and LVIS_STATEIMAGEMASK
flag to detect when checkbox is checked, then use ListView_SetItemState
to select the row.
Also this can lead to recursive calls, because we change the row in response to checkbox, and we change the checkbox in response to row selection. Use the busy
variable to stop recursive calls.
case WM_NOTIFY:
if (lParam)
{
NMHDR* header = (NMHDR*)lParam;
NMLISTVIEW* nmlist = (NMLISTVIEW*)lParam;
//use `busy` as a flag to prevent recursive calls:
static BOOL busy = FALSE;
if (!busy && header->hwndFrom == hListView && header->code == LVN_ITEMCHANGED)
{
busy = TRUE;
if (nmlist->uNewState & LVIS_SELECTED)
{
//row has been selected => check the checkbox
ListView_SetCheckState(hListView, nmlist->iItem, 1);
}
else if (nmlist->uNewState & LVIS_STATEIMAGEMASK)
{
//checkbox has been changed => select/unselect the row
BOOL checked = ListView_GetCheckState(hListView, nmlist->iItem);
ListView_SetItemState(hListView, nmlist->iItem,
checked ? LVIS_SELECTED : 0, LVIS_SELECTED);
}
busy = FALSE;
}
}
break;
edited May 23 '16 at 4:59
answered May 21 '16 at 4:52
Barmak Shemirani
20.3k42044
20.3k42044
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37356042%2fhow-to-sync-listview-checkbox-with-selectionwin32%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown