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.
Printable View
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.
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
Really?Quote:
Then, please, read the title of your thread!:rolleyes:Quote:
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...
Your post is titled 'select item from listbox and display into editbox..' :confused:Quote:
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
I am trying this but it gives nothing in m_klist.GetItem(&lvi); it contain NULL at last.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;
}
please post code if possible.
What is nItemIndex? Where and how is it defined/initialized?Quote:
Have you already read MSDN about CListCtrl and its using?Quote:
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
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;
}
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?Code:POSITION p = m_klist.GetFirstSelectedItemPosition();
while (p)
{
int nSelected = m_klist.GetNextSelectedItem(p);
// Do something with item nSelected
}