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
}
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.Quote:
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;
}
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()
What List Control notification do you handle?Quote:
Since this is an MFC application then one can use CListCtrl::GetItemText method.
I got the solution using following code means I got the text of selected value.Quote:
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.
1. Again: please, use code tags and avoid multiple empty lines! :mad:
2.Why do you call GetDlgItem? :confused:Quote:
You don't need it at all! Just because you already have the control member variable for this list control: m_klist.
3.Then eithe rset it to Report in the resource editor or change the style to LVS_REPORT. See this thread.Quote:
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
This assumes that the variable strText is either global or a member of the class - and that m_klist has the correct value.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;
}