-
January 15th, 2016, 01:20 AM
#1
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.
-
January 15th, 2016, 03:56 AM
#2
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
-
January 15th, 2016, 04:50 AM
#3
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by VictorN
but I am asking about list control not about listbox control
-
January 15th, 2016, 05:01 AM
#4
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by kiwagh105@gmail.com
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
-
January 15th, 2016, 05:02 AM
#5
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by kiwagh105@gmail.com
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)
-
January 15th, 2016, 05:07 AM
#6
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by VictorN
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.
-
January 15th, 2016, 05:07 AM
#7
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by 2kaud
Extremely sorry for this
-
January 15th, 2016, 05:49 AM
#8
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.
-
January 15th, 2016, 05:54 AM
#9
Re: select item from listbox and diplay into editbox in mfc vc++
please post code if possible.
-
January 15th, 2016, 05:58 AM
#10
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by kiwagh105@gmail.com
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
-
January 15th, 2016, 05:59 AM
#11
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by kiwagh105@gmail.com
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
-
January 15th, 2016, 06:05 AM
#12
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by VictorN
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;
}
-
January 15th, 2016, 06:06 AM
#13
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by VictorN
Yes I did
-
January 15th, 2016, 06:07 AM
#14
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)
-
January 15th, 2016, 06:11 AM
#15
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by 2kaud
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|