CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Jan 2016
    Posts
    61

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

    I have a list control on my dialog-box which contain list of names. when one particular name is selcted then display it in editbox control. How to do this in mfc? better if provide one simple code example.

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

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

    You have to handle the LBN_SELCHANGE notification of this listbox.
    Then you call CListBox::GetCurSel method to obtain the index of new selected item and if it not LB_ERR then call CListBox::GetText to obtain the text. now you'll be able to set this text to an edit control.
    See also:
    https://msdn.microsoft.com/en-us/library/y04ez4c9.aspx
    http://www.codeproject.com/Articles/...istBox-control
    Victor Nijegorodov

  3. #3
    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
    You have to handle the LBN_SELCHANGE notification of this listbox.
    Then you call CListBox::GetCurSel method to obtain the index of new selected item and if it not LB_ERR then call CListBox::GetText to obtain the text. now you'll be able to set this text to an edit control.
    See also:
    https://msdn.microsoft.com/en-us/library/y04ez4c9.aspx
    http://www.codeproject.com/Articles/...istBox-control
    but I am asking about list control not about listbox control

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

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

    Quote Originally Posted by kiwagh105@gmail.com View Post
    but I am asking about list control not about listbox control
    Really?
    Then, please, read the title of your thread!
    select item from listbox and diplay into editbox in mfc vc++


    Anyway you have to first read MSDN about CListCtrl class, its methods and notifications...
    Victor Nijegorodov

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

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

    Quote Originally Posted by kiwagh105@gmail.com View Post
    but I am asking about list control not about listbox control
    Your post is titled 'select item from listbox and display into editbox..'

    If you want some info about using list control with MFC have a look at
    http://www.codeproject.com/Articles/...e-List-Control
    https://msdn.microsoft.com/en-us/library/bycfwcsh.aspx
    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. #6
    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
    Really?
    Then, please, read the title of your thread!


    Anyway you have to first read MSDN about CListCtrl class, its methods and notifications...
    Extremely sorry for that.

  7. #7
    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
    Your post is titled 'select item from listbox and display into editbox..'

    If you want some info about using list control with MFC have a look at
    http://www.codeproject.com/Articles/...e-List-Control
    https://msdn.microsoft.com/en-us/library/bycfwcsh.aspx
    Extremely sorry for this

  8. #8
    Join Date
    Jan 2016
    Posts
    61

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

    Code:
    void Cselect_product::OnClickListCategory(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    	// TODO: Add your control notification handler code here
    
    
    
    
    			POSITION p = m_klist.GetFirstSelectedItemPosition();
    		while (p)
    		{
    			int nSelected = m_klist.GetNextSelectedItem(p);
    			// Do something with item nSelected
    		}
    
    
    				 TCHAR szBuffer[1024];
    		DWORD cchBuf(1024);
    		LVITEM lvi;
    		lvi.iItem = nItemIndex;
    		lvi.iSubItem = 0;
    		lvi.mask = LVIF_TEXT;
    		lvi.pszText = szBuffer;
    		lvi.cchTextMax = cchBuf;
    		m_klist.GetItem(&lvi);
    
    	
    	*pResult = 0;
    }
    I am trying this but it gives nothing in m_klist.GetItem(&lvi); it contain NULL at last.

  9. #9
    Join Date
    Jan 2016
    Posts
    61

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

    please post code if possible.

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

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

    Quote Originally Posted by kiwagh105@gmail.com View Post
    Code:
    void Cselect_product::OnClickListCategory(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    		...
    		 TCHAR szBuffer[1024];
    		DWORD cchBuf(1024);
    		LVITEM lvi;
    		lvi.iItem = nItemIndex;
    		lvi.iSubItem = 0;
    		lvi.mask = LVIF_TEXT;
    		lvi.pszText = szBuffer;
    		lvi.cchTextMax = cchBuf;
    		m_klist.GetItem(&lvi);
    
    	
    	*pResult = 0;
    }
    I am trying this but it gives nothing in m_klist.GetItem(&lvi); it contain NULL at last.
    What is nItemIndex? Where and how is it defined/initialized?
    Victor Nijegorodov

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

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

    Quote Originally Posted by kiwagh105@gmail.com View Post
    please post code if possible.
    Have you already read MSDN about CListCtrl and its using?
    THERE is a lot of examples out there:
    https://msdn.microsoft.com/en-us/lib...(v=vs.71).aspx
    http://www.codeproject.com/Articles/...e-List-Control
    https://www.google.ch/webhp?sourceid...stctrl+example
    Last edited by VictorN; January 15th, 2016 at 06:02 AM.
    Victor Nijegorodov

  12. #12
    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
    What is nItemIndex? Where and how is it defined/initialized?

    Code:
    void Cselect_product::OnClickListCategory(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    	// TODO: Add your control notification handler code here
    	int nItemIndex;
    
    			POSITION pos  = m_klist.GetFirstSelectedItemPosition (); 
    			if (pos == NULL) 
    			TRACE0 ("No items were selected! / N"); 
    			else 
    			{ 
    			while (pos) 
    			{ 
    			 nItemIndex= m_klist.GetNextSelectedItem (pos); 
    			TRACE1 ("Item% d was selected! / N", nItemIndex); 
    			// You could do your own processing on nItem here 
    			} 
    			}
    
    
    		/*	TCHAR szBuf [1024]; 
    			LVITEM lvi; 
    			lvi.iItem = 0; 
    			lvi.iSubItem = 0; 
    			lvi.mask = LVIF_TEXT; 
    			lvi.pszText = szBuf; 
    			lvi.cchTextMax = 1024; 
    			&m_klist.GetItem(LVI);
    
    
    
    			CString  GetItemText(nItemIndex,0);*/
    
    
    
    
    			POSITION p = m_klist.GetFirstSelectedItemPosition();
    		while (p)
    		{
    			int nSelected = m_klist.GetSelectedItem(23);
    			// Do something with item nSelected
    		}
    
    
    				 TCHAR szBuffer[1024];
    		DWORD cchBuf(1024);
    		LVITEM lvi;
    		lvi.iItem = nItemIndex;
    		lvi.iSubItem = 0;
    		lvi.mask = LVIF_TEXT;
    		lvi.pszText = szBuffer;
    		lvi.cchTextMax = cchBuf;
    		m_klist.GetItem(&lvi);
    
    		CString GetItemText(nItem,0);
    
    
    	//int GetItemText(nItem,0,szBuffer,2);
    	
    	*pResult = 0;
    }

  13. #13
    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
    Yes I did

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

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

    Code:
    	POSITION p = m_klist.GetFirstSelectedItemPosition();
    		while (p)
    		{
    			int nSelected = m_klist.GetNextSelectedItem(p);
    			// Do something with item nSelected
    		}
    This doesn't seem right. You iterate through the selected items, do nothing with them and then try to obtain item details?? Doesn't your ivi. code need to be within the while block?
    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
    Jan 2016
    Posts
    61

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

    Quote Originally Posted by 2kaud View Post
    Code:
    	POSITION p = m_klist.GetFirstSelectedItemPosition();
    		while (p)
    		{
    			int nSelected = m_klist.GetNextSelectedItem(p);
    			// Do something with item nSelected
    		}
    This doesn't seem right. You iterate through the selected items, do nothing with them and then try to obtain item details?? Doesn't your ivi. code need to be within the while block?
    Then please rectify that code.

Page 1 of 2 12 LastLast

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