CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26
  1. #16
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: select item from listbox and diplay into editbox in mfc vc++

    Quote Originally Posted by kiwagh105@gmail.com View Post
    Then please rectify that code.
    First, please, explain (in words) what you are trying to obtain in this OnClickListCategory method and how and when this method is about to be called.
    Victor Nijegorodov

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

    Re: select item from listbox and diplay into editbox in mfc vc++

    Something like (not tried)
    Code:
    void Cselect_product::OnClickListCategory(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	POSITION p = m_klist.GetFirstSelectedItemPosition();
    	while (p)
    	{
    		int nItemIndex = m_klist.GetNextSelectedItem(p);
    		
    		TCHAR szBuffer[1024] = {0};
    		DWORD cchBuf(1023);
    		LVITEM lvi;
    		lvi.iItem = nItemIndex;
    		lvi.iSubItem = 0;
    		lvi.mask = LVIF_TEXT;
    		lvi.pszText = szBuffer;
    		lvi.cchTextMax = cchBuf;
    
    		if (m_klist.GetItem(&lvi)) {
                         //use szBuffer here
                    } else
                         OutputDebugString("Error with GetItem\n");
    	}
    
    	*pResult = 0;
    }
    Last edited by 2kaud; January 15th, 2016 at 06:53 AM. Reason: Added error check
    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. #18
    Join Date
    Jan 2016
    Posts
    61

    Re: select item from listbox and diplay into editbox in mfc vc++

    Quote Originally Posted by VictorN View Post
    First, please, explain (in words) what you are trying to obtain in this OnClickListCategory method and how and when this method is about to be called.

    OnClickListCategory() method I am trying to get the text of selected item in list control. This method called when we select a value .

  4. #19
    Join Date
    Jan 2016
    Posts
    61

    Re: select item from listbox and diplay into editbox in mfc vc++

    Quote Originally Posted by 2kaud View Post
    Something like (not tried)
    Code:
    void Cselect_product::OnClickListCategory(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	POSITION p = m_klist.GetFirstSelectedItemPosition();
    	while (p)
    	{
    		int nItemIndex = m_klist.GetNextSelectedItem(p);
    		
    		TCHAR szBuffer[1024] = {0};
    		DWORD cchBuf(1023);
    		LVITEM lvi;
    		lvi.iItem = nItemIndex;
    		lvi.iSubItem = 0;
    		lvi.mask = LVIF_TEXT;
    		lvi.pszText = szBuffer;
    		lvi.cchTextMax = cchBuf;
    
    		if (m_klist.GetItem(&lvi)) {
                         //use szBuffer here
                    } else
                         OutputDebugString("Error with GetItem\n");
    	}
    
    	*pResult = 0;
    }
    Where(in which veriable) is text stored of select item in list control ?

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

    Re: select item from listbox and diplay into editbox in mfc vc++

    Read the documentation! https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

    pszText

    Type: LPTSTR

    If the structure specifies item attributes, pszText is a pointer to a null-terminated string containing the item text.

    So szBuffer will contain the text after a successful call to .getItem()
    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)

  6. #21
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: select item from listbox and diplay into editbox in mfc vc++

    Quote Originally Posted by kiwagh105@gmail.com View Post
    OnClickListCategory() method I am trying to get the text of selected item in list control. This method called when we select a value .
    What List Control notification do you handle?
    Victor Nijegorodov

  7. #22
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: select item from listbox and diplay into editbox in mfc vc++

    Quote Originally Posted by 2kaud View Post
    Read the documentation! https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

    pszText

    Type: LPTSTR

    If the structure specifies item attributes, pszText is a pointer to a null-terminated string containing the item text.

    So szBuffer will contain the text after a successful call to .getItem()
    Since this is an MFC application then one can use CListCtrl::GetItemText method.
    Victor Nijegorodov

  8. #23
    Join Date
    Jan 2016
    Posts
    61

    Re: select item from listbox and diplay into editbox in mfc vc++

    Quote Originally Posted by 2kaud View Post
    Read the documentation! https://msdn.microsoft.com/en-us/lib...=vs.85%29.aspx

    pszText

    Type: LPTSTR

    If the structure specifies item attributes, pszText is a pointer to a null-terminated string containing the item text.

    So szBuffer will contain the text after a successful call to .getItem()

    Yes it is store into szBuffer but it contain some hex value also. like (0x012200,"text" ) i want just text to store into my variable how to do this?

  9. #24
    Join Date
    Jan 2016
    Posts
    61

    Re: select item from listbox and diplay into editbox in mfc vc++

    Quote Originally Posted by kiwagh105@gmail.com View Post
    Yes it is store into szBuffer but it contain some hex value also. like (0x012200,"text" ) i want just text to store into my variable how to do this?
    I got the solution using following code means I got the text of selected value.

    void Cselect_product::OnItemchangedListCategory(NMHDR* pNMHDR, LRESULT* pResult)
    {
    NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
    // TODO: Add your control notification handler code here





    CListCtrl* pListCtrl = (CListCtrl *) GetDlgItem(IDC_ListCategory);


    POSITION pos = m_klist.GetFirstSelectedItemPosition ();
    if (pos == NULL)
    TRACE0 ("No items were selected! \n");
    else
    {
    while (pos)
    {
    int nItem = m_klist.GetNextSelectedItem (pos);
    TRACE1 ("Item %d was selected! \n", nItem);
    //you could do your own processing on nItem here

    strText = m_klist.GetItemText(nItem, 0);
    }
    }


    *pResult = 0;
    }


    but list control's view property is set to the List at that time I want all this operation when view property of list control is Report.
    Last edited by kiwagh105@gmail.com; January 17th, 2016 at 01:57 AM.

  10. #25
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: select item from listbox and diplay into editbox in mfc vc++

    1. Again: please, use code tags and avoid multiple empty lines!
    2.
    Quote Originally Posted by kiwagh105@gmail.com
    Code:
    CListCtrl* pListCtrl = (CListCtrl *) GetDlgItem(IDC_ListCategory);
    Why do you call GetDlgItem?
    You don't need it at all! Just because you already have the control member variable for this list control: m_klist.
    3.
    Quote Originally Posted by kiwagh105@gmail.com
    list control's view property is set to the List at that time I want all this operation when view property of list control is Report
    Then eithe rset it to Report in the resource editor or change the style to LVS_REPORT. See this thread.
    Victor Nijegorodov

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

    Re: select item from listbox and diplay into editbox in mfc vc++

    As Victor stated in post #25, pNMListView and pListCtrl are not required here as you are using m_klist and are thus this is superfluous code. Your effective code is
    Code:
    void Cselect_product::OnItemchangedListCategory(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	POSITION pos = m_klist.GetFirstSelectedItemPosition();
    	if (pos == NULL)
    		TRACE0("No items were selected! \n");
    	else
    		while (pos)
    		{
    			int nItem = m_klist.GetNextSelectedItem(pos);
    			TRACE1("Item %d was selected! \n", nItem);
    			//you could do your own processing on nItem here
    
    			strText = m_klist.GetItemText(nItem, 0);
    		}
    
    	*pResult = 0;
    }
    This assumes that the variable strText is either global or a member of the class - and that m_klist has the correct value.
    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)

Page 2 of 2 FirstFirst 12

Tags for this Thread

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