CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: ListCtrl in MFC

  1. #1
    Join Date
    May 2015
    Posts
    500

    ListCtrl in MFC

    Hello All,

    As per your suggestion, i have been trying to learn MFC and try to replicate the problem i am solving in the legacy code.

    I have comeup with the following code:, But the issue here is the list is growing in the opposite direction from what i was expecting !!!

    Code:
    map<int, vector<int>> m = { { 1, {1,3,4 }} ,{ 2, {4,5,6 } } };
    map<int, string> n = { { 1, "Site1", } , { 2, "Site2"} ,{ 3, "Site3" },{ 4, "Site4" },{ 5, "Site5" },{ 6, "Site6" } };
    
    
    BOOL CMFCApplication2Dlg::OnInitDialog()
    {
    
    
    	// TODO: Add extra initialization here
    
    	priya_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
    
    	int nItem = priya_list.InsertColumn(0, _T("Cell"), LVCFMT_LEFT, -1, 0);
    	priya_list.InsertColumn(1, _T("Site"), LVCFMT_LEFT, -1, 0);
    
    	int centralsite;
    	vector<int> sites;
    	bool bIsfirstCentralsite(false);
    	for (const auto pair : m)
    	{
    		bIsfirstCentralsite = true;
    		string centreId;
    		int index = pair.first;
    		if (index == 1)
    		{
    			centreId = "first";
    		}
    		else
    		{
    			centreId = "second";
    		}
    
    		CString ccetre(centreId.c_str());
    		for (const auto site : pair.second)
    		{
    			int nItem(0);
    			if(bIsfirstCentralsite)
    				nItem = priya_list.InsertItem(0, ccetre);
    			else
    				nItem = priya_list.InsertItem(0, _T(""));
    
    			auto itr = n.find(site);
    			string sSiteId = itr->second;
    			CString cSiteId(sSiteId.c_str());
    			priya_list.SetItemText(nItem, 1, cSiteId);
    
    			bIsfirstCentralsite = false;
    
    		}
    	}
    
    }
    Sorry my code maynot be well written, but i am able to replicate the issue i have.


    Name:  Capture.PNG
Views: 684
Size:  7.6 KB

    What i wanted is:

    Cell Site

    first site1
    site3
    site4

    second site4
    site5
    site6


    Basically, first has sites 1,3,4 and second has sites 4,5 ,6

    Hope i am able to convey the problem i am facing


    thanks a lot

    Again, thanks a lot for having patience with my naive questions..

    Merry Christmas and Happy new year

    Best wishes
    Pdk

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: ListCtrl in MFC

    I don't know MFC, and I last used win32 API when XP was the current OS!

    However, for testing I used a vector of pair to simulate the list and displayed to the console. Given:

    Code:
    #include <map>
    #include <vector>
    #include <string>
    #include <iostream>
    #include <utility>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	const map<int, vector<int>> m = {{ 1, {1,3,4 }} ,{ 2, {4,5,6 } }};
    	const map<int, string> n = {{ 1, "Site1", } , { 2, "Site2"} ,{ 3, "Site3" },{ 4, "Site4" },{ 5, "Site5" },{ 6, "Site6" }};
    
    	vector<pair<string, string>> output;
    	bool bIsfirstCentralsite {false};
    
    	for (const auto pair : m) {
    		string centreId;
    
    		bIsfirstCentralsite = true;
    
    		if (pair.first == 1)
    			centreId = "first";
    		else
    			centreId = "second";
    
    		for (const auto site : pair.second) {
    			if (bIsfirstCentralsite)
    				output.emplace_back(centreId, "");
    			else
    				output.emplace_back("", "");
    
    			const auto itr {n.find(site)};
    
    			if (itr != n.end())
    				output.back().second = itr->second;
    
    			bIsfirstCentralsite = false;
    		}
    	}
    
    	for (const auto& out : output)
    		cout << left << setw(8) << out.first << setw(8) << out.second << '\n';
    }
    This displays:

    Code:
    first   Site1
            Site3
            Site4
    second  Site4
            Site5
            Site6
    so this algorithm seems OK. The issue would appear to be in use of the list control.

    PS It looks like the list is being sorted by site. Are there any sorting criteria set anywhere?
    Last edited by 2kaud; December 22nd, 2020 at 06:53 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: ListCtrl in MFC

    Thankyou so much kaud. Very helpful for me.

    Im so grateful for this forum, esp to you being so responsive, being patient and help with every doubt.

    I was working in startup where it was 3gpp (i was good in 3gpp) and c++ linux (not good in c++,linux) work, then company was not doing good. So had to find my current job, where it is windows based, not much 3gpp. So there was no strong point for me , in this project. Almost most collegues here are over 20 years exprience, was very difficult for me to ramp up. Without this forum, i wouldnot have survived in this job. Massive thankyou for , allowing me to ask naive questions, and suggestions and help.

    Thankyou

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: ListCtrl in MFC

    nItem = priya_list.InsertItem(0, ccetre);
    You're inserting at the beginning of the control. That's that the 0 is doing. You need to insert at the end.
    nItem = priya_list.InsertItem(priya_list.GetItemCount(), ccetre);

  5. #5
    Join Date
    May 2015
    Posts
    500

    Re: ListCtrl in MFC

    @GCDEF : Thanks a lot,

    Just before you posted, i also found this answer, googling

    Code:
    	SetIcon(m_hIcon, TRUE);			// Set big icon
    	SetIcon(m_hIcon, FALSE);		// Set small icon
    
    	// TODO: Add extra initialization here
    
    	pdk_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_ONECLICKACTIVATE);
    
    	int nItem = pdk_list.InsertColumn(0, _T("Cell"), LVCFMT_LEFT, -1, 0);
    	pdk_list.InsertColumn(1, _T("Site"), LVCFMT_LEFT, -1, 0);
    
    	int centralsite;
    	vector<int> sites;
    	bool bIsfirstCentralsite(false);
    	for (const auto pair : m)
    	{
    		bIsfirstCentralsite = true;
    		string centreId;
    		int index = pair.first;
    		if (index == 1)
    		{
    			centreId = "first";
    		}
    		else
    		{
    			centreId = "second";
    		}
    
    		CString ccetre(centreId.c_str());
    		for (const auto site : pair.second)
    		{
    			int nItem(0);
    			if(bIsfirstCentralsite)
    				nItem = pdk_list.InsertItem(pdk_list.GetItemCount(), ccetre);
    			else
    				nItem = pdk_list.InsertItem(pdk_list.GetItemCount(), _T(""));
    
    			auto itr = n.find(site);
    			string sSiteId = itr->second;
    			CString cSiteId(sSiteId.c_str());
    			pdk_list.SetItemText(nItem, 1, cSiteId);
    
    			bIsfirstCentralsite = false;
    
    		}
    	}

  6. #6
    Join Date
    May 2015
    Posts
    500

    Re: ListCtrl in MFC

    Now the basic GUI looks ok. Trying to add some more functionality:

    Actually, on clicking the row, i want to grab the column text. Note, that some rows maynot have the column info.

    I have added the code to get the info of the present row. Not sure how to get the corresponding column text.

    Code:
    void CMFCApplication2Dlg::OnBnClickedAddToSiteDB()
    {
    
    	CListCtrl * pdk_ctrl = (CListCtrl *) GetDlgItem(IDC_LIST1);
    
    	int nColumns = pdk_ctrl ->GetHeaderCtrl()->GetItemCount();
    	POSITION pos = pdk_ctrl ->GetFirstSelectedItemPosition();
    	while (pos)
    	{
    		int nItem = pdk_ctrl ->GetNextSelectedItem(pos);
    
    		for (int i = 0; i<nColumns; i++)
    		{
    			CString sItem = pdk_ctrl ->GetItemText(nItem, i);
    			// TO DO: do stuff with item text here
    		}
    	}
    }
    in the above figue, if click on row which has "site6", i need to get the corresponding column, ie "second", eventhough the column info on that row is not having any info.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: ListCtrl in MFC

    Quote Originally Posted by pdk5 View Post
    in the above figue, if click on row which has "site6", i need to get the corresponding column, ie "second", eventhough the column info on that row is not having any info.
    I'm not sure what that means

  8. #8
    Join Date
    May 2015
    Posts
    500

    Re: ListCtrl in MFC

    Quote Originally Posted by GCDEF View Post
    I'm not sure what that means
    first Site1
    Site3
    Site4
    second Site4
    Site5
    Site6

    Here, Site1, 3, 4, are supposed to have column 1 as "first". So if i select, site1, we know the corresponding column is "site1". Lets say user selects "site3". Then user has to deduce that column 1 is "first", eventhough in the list it appears as no text.

    Basically, if user selects, 1st, 2nd or 3rd row, the function needs to derive "first"...And do some processing based on this.

    Otherway, is to explicitly store the column info for each rows... But it maynot look nice

    The function is something like this:

    OnSiteDB{

    if(selection is 1st, 2nd or 3rd row)
    coulmn_text = "first";

    if(selection is 4th, 5th or 6th row)
    column_text = "second"
    }

    Sorry if im trying to complicate things, or asking vague thing.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: ListCtrl in MFC

    Possibly not of interest now as you're fixed the MFC issue, but this is WIN32 code to display the data.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #define NOMINMAX
    
    #include <windows.h>
    #include <CommCtrl.h>
    
    #include <map>
    #include <vector>
    #include <string>
    #include <utility>
    
    using namespace std;
    
    #define WA_STARTUP	(WM_USER + 1)
    
    const int g_col1sz {200};
    const int g_col2sz {200};
    const char* const cnam {"myclass"};
    const char* const cwin {"mywin"};
    
    HINSTANCE	g_inst {};			// Current instance
    HWND		g_hwndMain {};		// Main window handle
    HWND		g_hwndLV {};		// List view window
    
    ATOM				MyRegisterClass(HINSTANCE hInstance);
    HWND				InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    bool				InitCommon();
    void				DoStartup(LPARAM lParam);
    void				DoSize(LPARAM lParam);
    
    const map<int, vector<int>> m {{ 1, {1,3,4 }} ,{ 2, {4,5,6 } }};
    const map<int, string> n {{ 1, "Site1", } , { 2, "Site2"} ,{ 3, "Site3" },{ 4, "Site4" },{ 5, "Site5" },{ 6, "Site6" }};
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
    {
    	if (!InitCommon() || !MyRegisterClass(hInstance) || !(g_hwndMain = InitInstance(hInstance, nCmdShow)))
    		return FALSE;
    
    	SendMessage(g_hwndMain, WA_STARTUP, 0, 0);
    
    	MSG msg {};
    
    	while (GetMessage(&msg, nullptr, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return (int)msg.wParam;
    }
    
    bool InitCommon()
    {
    	INITCOMMONCONTROLSEX icex {};
    
    	InitCommonControls();
    	icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    	icex.dwICC = ICC_LISTVIEW_CLASSES;
    	return InitCommonControlsEx(&icex);
    }
    
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX cex {0};
    
    	cex.cbSize = sizeof(WNDCLASSEX);
    	cex.style = CS_HREDRAW | CS_VREDRAW;
    	cex.lpfnWndProc = WndProc;
    	cex.hInstance = hInstance;
    	cex.hCursor = LoadCursor(NULL, IDC_ARROW);
    	cex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    	cex.lpszClassName = cnam;
    
    	return RegisterClassEx(&cex);
    }
    
    HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
    	g_inst = hInstance;
    
    	if (const auto hWnd = CreateWindowEx(0, cnam, cwin, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); hWnd) {
    		ShowWindow(hWnd, nCmdShow);
    		UpdateWindow(hWnd);
    		return hWnd;
    	}
    
    	return FALSE;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static LPARAM oldlParam {};
    
    	switch (message) {
    		case WA_STARTUP:
    			DoStartup(oldlParam);
    			return TRUE;
    
    		case WM_SIZE:
    			DoSize(oldlParam = lParam);
    			break;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    
    		case WM_PAINT:
    		{
    			PAINTSTRUCT ps;
    
    			BeginPaint(hWnd, &ps);
    			EndPaint(hWnd, &ps);
    		}
    		break;
    
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    void DoStartup(LPARAM lParam)
    {
    	if ((g_hwndLV = CreateWindowEx(0, WC_LISTVIEW, "", LVS_REPORT  | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_HSCROLL | WS_VSCROLL, 0, 0, 0, 0, g_hwndMain, NULL, g_inst, NULL)) == NULL) {
    		MessageBox(0, "Issue creating ListView control", "Error", MB_OK | MB_ICONERROR);
    		return;
    	}
    
    	DoSize(lParam);
    	ShowWindow(g_hwndLV, SW_SHOW);
    
    	LVCOLUMN lvc {};
    
    	lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
    	lvc.pszText = "cell";
    	lvc.cx = g_col1sz;
    	lvc.fmt = LVCFMT_LEFT;
    
    	if (ListView_InsertColumn(g_hwndLV, 0, &lvc) < 0) {
    		MessageBox(0, "ListView InsertColumn failed!", "Error", MB_OK | MB_ICONERROR);
    		return;
    	}
    
    	lvc.pszText = "site";
    	lvc.cx = g_col2sz;
    
    	if (ListView_InsertColumn(g_hwndLV, 1, &lvc) < 0) {
    		MessageBox(0, "ListView InsertColumn failed!", "Error", MB_OK | MB_ICONERROR);
    		return;
    	}
    
    	bool bIsfirstCentralsite {};
    	LVITEM lvi {};
    
    	lvi.mask = LVIF_TEXT;
    
    	for (const auto& pair : m) {
    		const string centreId {pair.first == 1 ? "first" : "second"};
    
    		bIsfirstCentralsite = true;
    
    		for (const auto& site : pair.second) {
    			lvi.pszText = bIsfirstCentralsite ? centreId.c_str() : "";
    
    			const auto ins {ListView_InsertItem(g_hwndLV, &lvi)};
    
    			if (const auto itr {n.find(site)}; itr != n.end())
    				ListView_SetItemText(g_hwndLV, ins, 1, (char*)itr->second.c_str());
    
    			bIsfirstCentralsite = false;
    			++lvi.iItem;
    		}
    	}
    }
    
    
    void DoSize(LPARAM lParam)
    {
    	MoveWindow(g_hwndLV, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
    }
    I understand your selection issue. I'll look into it. I think the answer is to use the LPARAM attribute of LVITEM. I'll try a knock-up later and post here.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: ListCtrl in MFC

    Quote Originally Posted by pdk5 View Post
    first Site1
    Site3
    Site4
    second Site4
    Site5
    Site6

    Here, Site1, 3, 4, are supposed to have column 1 as "first". So if i select, site1, we know the corresponding column is "site1". Lets say user selects "site3". Then user has to deduce that column 1 is "first", eventhough in the list it appears as no text.

    Basically, if user selects, 1st, 2nd or 3rd row, the function needs to derive "first"...And do some processing based on this.

    Otherway, is to explicitly store the column info for each rows... But it maynot look nice

    The function is something like this:

    OnSiteDB{

    if(selection is 1st, 2nd or 3rd row)
    coulmn_text = "first";

    if(selection is 4th, 5th or 6th row)
    column_text = "second"
    }

    Sorry if im trying to complicate things, or asking vague thing.
    You find an empty second column, then read the previous second column until you find one that's not empty any more.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: ListCtrl in MFC

    This is one way to do it using WIN32. This will set the window title to the appropriate Cell name when an item/subitem is left-clicked. The LPARAM member of the listview item is set to the numeric value used for the first column - then its just a simple look up. This avoids searching the list backwards until you find what you want.

    Sorry, I can't do it in MFC.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #define NOMINMAX
    
    #include <windows.h>
    #include <CommCtrl.h>
    
    #include <map>
    #include <vector>
    #include <string>
    #include <utility>
    
    using namespace std;
    
    #define WA_STARTUP	(WM_USER + 1)
    #define WA_CLICK	(WM_USER + 2)
    
    const char* const cnam {"myclass"};
    const char* const cwin {"mywin"};
    
    HINSTANCE	g_inst {};			// Current instance
    HWND		g_hwndMain {};		// Main window handle
    HWND		g_hwndLV {};		// List view window
    
    ATOM				MyRegisterClass(HINSTANCE hInstance);
    HWND				InitInstance(HINSTANCE, int);
    LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
    bool				InitCommon();
    void				DoStartup(LPARAM lParam);
    void				DoSize(LPARAM lParam);
    void				LVclick(WPARAM wParam);
    
    const map<int, vector<int>> m {{ 1, {1,3,4 }} ,{ 2, {4,5,6 } }};
    const map<int, string> n {{ 1, "Site1", } , { 2, "Site2"} ,{ 3, "Site3" },{ 4, "Site4" },{ 5, "Site5" },{ 6, "Site6" }};
    const map<int, string> o {{1, "first"}, {2, "second"}};
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
    {
    	if (!InitCommon() || !MyRegisterClass(hInstance) || !(g_hwndMain = InitInstance(hInstance, nCmdShow)))
    		return FALSE;
    
    	SendMessage(g_hwndMain, WA_STARTUP, 0, 0);
    
    	MSG msg {};
    
    	while (GetMessage(&msg, nullptr, 0, 0)) {
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    
    	return (int)msg.wParam;
    }
    
    bool InitCommon()
    {
    	INITCOMMONCONTROLSEX icex {};
    
    	InitCommonControls();
    	icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    	icex.dwICC = ICC_LISTVIEW_CLASSES;
    	return InitCommonControlsEx(&icex);
    }
    
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    	WNDCLASSEX cex {0};
    
    	cex.cbSize = sizeof(WNDCLASSEX);
    	cex.style = CS_HREDRAW | CS_VREDRAW;
    	cex.lpfnWndProc = WndProc;
    	cex.hInstance = hInstance;
    	cex.hCursor = LoadCursor(NULL, IDC_ARROW);
    	cex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    	cex.lpszClassName = cnam;
    
    	return RegisterClassEx(&cex);
    }
    
    HWND InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
    	g_inst = hInstance;
    
    	if (const auto hWnd = CreateWindowEx(0, cnam, cwin, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr); hWnd) {
    		ShowWindow(hWnd, nCmdShow);
    		UpdateWindow(hWnd);
    		return hWnd;
    	}
    
    	return FALSE;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	static LPARAM oldlParam {};
    
    	switch (message) {
    		case WA_STARTUP:
    			DoStartup(oldlParam);
    			return TRUE;
    
    		case WM_SIZE:
    			DoSize(oldlParam = lParam);
    			break;
    
    		case WM_DESTROY:
    			PostQuitMessage(0);
    			break;
    
    		case WM_PAINT:
    		{
    			PAINTSTRUCT ps {};
    
    			BeginPaint(hWnd, &ps);
    			EndPaint(hWnd, &ps);
    		}
    		break;
    
    		case WA_CLICK:
    			LVclick(wParam);
    			return TRUE;
    
    		case WM_NOTIFY:
    			if (const auto tc = (LPNMHDR)lParam; (tc->hwndFrom == g_hwndLV) && (tc->code == NM_CLICK)) {
    				LVHITTESTINFO lvhit {};
    
    				GetCursorPos(&lvhit.pt);
    				ScreenToClient(g_hwndLV, &lvhit.pt);
    				if (ListView_SubItemHitTest(g_hwndLV, &lvhit) >= 0 && (lvhit.flags & LVHT_ONITEM))
    					PostMessage(g_hwndMain, WA_CLICK, lvhit.iItem, 0);
    				else
    					SetWindowText(g_hwndMain, "Unknown");
    			}
    			break;
    
    
    		default:
    			return DefWindowProc(hWnd, message, wParam, lParam);
    	}
    	return 0;
    }
    
    void DoStartup(LPARAM lParam)
    {
    	if ((g_hwndLV = CreateWindowEx(0, WC_LISTVIEW, "", LVS_REPORT  | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_HSCROLL | WS_VSCROLL, 0, 0, 0, 0, g_hwndMain, NULL, g_inst, NULL)) == NULL) {
    		MessageBox(0, "Issue creating ListView control", "Error", MB_OK | MB_ICONERROR);
    		return;
    	}
    
    	LVCOLUMN lvc {};
    
    	lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
    	lvc.fmt = LVCFMT_LEFT;
    	lvc.pszText = "cell";
    
    	if (ListView_InsertColumn(g_hwndLV, 0, &lvc) < 0) {
    		MessageBox(0, "ListView InsertColumn failed!", "Error", MB_OK | MB_ICONERROR);
    		return;
    	}
    
    	lvc.pszText = "site";
    
    	if (ListView_InsertColumn(g_hwndLV, 1, &lvc) < 0) {
    		MessageBox(0, "ListView InsertColumn failed!", "Error", MB_OK | MB_ICONERROR);
    		return;
    	}
    
    	DoSize(lParam);
    	ShowWindow(g_hwndLV, SW_SHOW);
    
    	bool bIsfirstCentralsite {};
    	LVITEM lvi {};
    
    	lvi.mask = LVIF_TEXT | LVIF_PARAM;
    
    	for (const auto& pair : m) {
    		const auto oit {o.find(pair.first)};
    		const string centreId {oit != o.end() ? oit->second : "Unknown"};
    
    		bIsfirstCentralsite = true;
    		lvi.lParam = pair.first;
    
    		for (const auto& site : pair.second) {
    			lvi.pszText = bIsfirstCentralsite ? centreId.c_str() : "";
    
    			const auto ins {ListView_InsertItem(g_hwndLV, &lvi)};
    
    			if (const auto itr {n.find(site)}; itr != n.end())
    				ListView_SetItemText(g_hwndLV, ins, 1, (char*)itr->second.c_str());
    
    			bIsfirstCentralsite = false;
    			++lvi.iItem;
    		}
    	}
    }
    
    void DoSize(LPARAM lParam)
    {
    	ListView_SetColumnWidth(g_hwndLV, 0, LOWORD(lParam) / 2);
    	ListView_SetColumnWidth(g_hwndLV, 1, LOWORD(lParam) / 2);
    	MoveWindow(g_hwndLV, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
    }
    
    void LVclick(WPARAM wParam)
    {
    	LVITEM	lv {};
    
    	lv.mask = LVIF_PARAM;
    	lv.iItem = wParam;
    
    	if (ListView_GetItem(g_hwndLV, &lv))
    		if (const auto oit = o.find(lv.lParam); oit != o.end()) {
    			SetWindowText(g_hwndMain, o.find(lv.lParam)->second.c_str());
    			return;
    		}
    
    	SetWindowText(g_hwndMain, "bad!");
    }
    Name:  listview.jpg
Views: 512
Size:  7.7 KB
    Last edited by 2kaud; December 22nd, 2020 at 02:48 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    May 2015
    Posts
    500

    Re: ListCtrl in MFC

    Thankyou very much kaud, very helpful

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: ListCtrl in MFC

    That would be way less code in MFC. I didn't read what you're doing, but if you're using SetItemData to set the item number of the appropriate second column value, that would be a more elegant solution than the one I suggested.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: ListCtrl in MFC

    That would be way less code in MFC.
    Almost certainly! But I don't know/have never used MFC
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    May 2015
    Posts
    500

    Re: ListCtrl in MFC

    GCDEF, Could you bit elaborate, on how to use SetItemData in this case ?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured