-
January 15th, 2016, 06:17 AM
#16
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by kiwagh105@gmail.com
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
-
January 15th, 2016, 06:41 AM
#17
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)
-
January 15th, 2016, 07:00 AM
#18
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by VictorN
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 .
-
January 15th, 2016, 07:01 AM
#19
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by 2kaud
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 ?
-
January 15th, 2016, 07:17 AM
#20
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)
-
January 15th, 2016, 07:30 AM
#21
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by kiwagh105@gmail.com
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
-
January 15th, 2016, 07:32 AM
#22
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by 2kaud
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
-
January 17th, 2016, 01:03 AM
#23
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by 2kaud
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?
-
January 17th, 2016, 01:52 AM
#24
Re: select item from listbox and diplay into editbox in mfc vc++
Originally Posted by kiwagh105@gmail.com
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.
-
January 17th, 2016, 04:27 AM
#25
Re: select item from listbox and diplay into editbox in mfc vc++
1. Again: please, use code tags and avoid multiple empty lines!
2.
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.
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
-
January 17th, 2016, 05:51 AM
#26
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)
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
|