Change from Cstring to double in listview vc++
-
Hello everyone! I've used a list control to display data and an edit box to edit them. So I used this function:
void CDataDialog::SetCell(HWND hWnd1,
CString value, int nRow, int nCol)I want to make this change:
void CDataDialog::SetCell(HWND hWnd1,
double value, int nRow, int nCol)but I have problem with other functions such as GetDlgItemText which I don't know how to change. Here is the code:
void CDataDialog::SetCell(HWND hWnd1,
double value, int nRow, int nCol)
{
TCHAR szString[256];
swprintf(szString, value, 0);//Fill the LVITEM structure with the //values given as parameters. LVITEM lvItem; lvItem.mask = LVIF\_TEXT; lvItem.iItem = nRow; lvItem.pszText = szString; lvItem.iSubItem = nCol; if (nCol >0) //set the value of listItem ::SendMessage(hWnd1, LVM\_SETITEM, (WPARAM)0, (WPARAM)&lvItem); else //Insert the value into List ListView\_InsertItem(hWnd1, &lvItem);
}
CString CDataDialog::GetItemText(
HWND hWnd, int nItem, int nSubItem) const
{
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.iSubItem = nSubItem;
CString str;
int nLen = 128;
int nRes;
do
{
nLen *= 2;
lvi.cchTextMax = nLen;
lvi.pszText = str.GetBufferSetLength(nLen);
nRes = (int)::SendMessage(hWnd,
LVM_GETITEMTEXT, (WPARAM)nItem,
(LPARAM)&lvi);
} while (nRes == nLen - 1);
str.ReleaseBuffer();
return str;
}void CDataDialog::OnClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
//LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: Add your control notification handler code here
//*pResult = 0;
Invalidate();
HWND hWnd1 = ::GetDlgItem(m_hWnd, IDC_LIST1);
LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE)pNMHDR;
RECT rect;
//get the row number
nItem = temp->iItem;
//get the column number
nSubItem = temp->iSubItem;
if (nSubItem == 0 || nSubItem == -1 || nItem == -1)
return;
//Retrieve the text of the selected subItem
//from the list
CString str = GetItemText(hWnd1, nItem,
nSubItem);RECT rect1, rect2; // this macro is used to retrieve the Rectanle // of the selected SubItem ListView\_GetSubItemRect(hWnd1, temp->iItem, temp->iSubItem, LVIR\_BOUNDS, &rect); //Get the Rectange of the listControl ::GetWindowRect(temp->hdr.hwndFrom, &rect1); //Get the Rectange of the Dialog ::GetWindowRect(m\_hWnd, &rect2); int x = rect1.left - rect2.left; int y = rect1.top - rect2.top; if (nItem != -1) ::SetWindowPos(::GetD
-
Hello everyone! I've used a list control to display data and an edit box to edit them. So I used this function:
void CDataDialog::SetCell(HWND hWnd1,
CString value, int nRow, int nCol)I want to make this change:
void CDataDialog::SetCell(HWND hWnd1,
double value, int nRow, int nCol)but I have problem with other functions such as GetDlgItemText which I don't know how to change. Here is the code:
void CDataDialog::SetCell(HWND hWnd1,
double value, int nRow, int nCol)
{
TCHAR szString[256];
swprintf(szString, value, 0);//Fill the LVITEM structure with the //values given as parameters. LVITEM lvItem; lvItem.mask = LVIF\_TEXT; lvItem.iItem = nRow; lvItem.pszText = szString; lvItem.iSubItem = nCol; if (nCol >0) //set the value of listItem ::SendMessage(hWnd1, LVM\_SETITEM, (WPARAM)0, (WPARAM)&lvItem); else //Insert the value into List ListView\_InsertItem(hWnd1, &lvItem);
}
CString CDataDialog::GetItemText(
HWND hWnd, int nItem, int nSubItem) const
{
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.iSubItem = nSubItem;
CString str;
int nLen = 128;
int nRes;
do
{
nLen *= 2;
lvi.cchTextMax = nLen;
lvi.pszText = str.GetBufferSetLength(nLen);
nRes = (int)::SendMessage(hWnd,
LVM_GETITEMTEXT, (WPARAM)nItem,
(LPARAM)&lvi);
} while (nRes == nLen - 1);
str.ReleaseBuffer();
return str;
}void CDataDialog::OnClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
//LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: Add your control notification handler code here
//*pResult = 0;
Invalidate();
HWND hWnd1 = ::GetDlgItem(m_hWnd, IDC_LIST1);
LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE)pNMHDR;
RECT rect;
//get the row number
nItem = temp->iItem;
//get the column number
nSubItem = temp->iSubItem;
if (nSubItem == 0 || nSubItem == -1 || nItem == -1)
return;
//Retrieve the text of the selected subItem
//from the list
CString str = GetItemText(hWnd1, nItem,
nSubItem);RECT rect1, rect2; // this macro is used to retrieve the Rectanle // of the selected SubItem ListView\_GetSubItemRect(hWnd1, temp->iItem, temp->iSubItem, LVIR\_BOUNDS, &rect); //Get the Rectange of the listControl ::GetWindowRect(temp->hdr.hwndFrom, &rect1); //Get the Rectange of the Dialog ::GetWindowRect(m\_hWnd, &rect2); int x = rect1.left - rect2.left; int y = rect1.top - rect2.top; if (nItem != -1) ::SetWindowPos(::GetD
A list control can not store double values without additional handling. To set the text of an item to a floating point value it must be converted. Read the documentation of the used sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l[^] function. So you have to use something like
WCHAR szString[256];
swprintf(szString, sizeof(szString) / sizeof(WCHAR), L"%G", value);But it would be better to use _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l[^] here:
TCHAR szString[256];
_sntprintf_t(szString, sizeof(szString) / sizeof(TCHAR), _T("%G"), value);To get a double value back when reading the cell text use
_tstof
(atof, _atof_l, _wtof, _wtof_l[^]):// double CDataDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const
double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
{
CString str = GetItemText(hWnd, nItem, nSubItem);
return _tstof(str);
} -
A list control can not store double values without additional handling. To set the text of an item to a floating point value it must be converted. Read the documentation of the used sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l[^] function. So you have to use something like
WCHAR szString[256];
swprintf(szString, sizeof(szString) / sizeof(WCHAR), L"%G", value);But it would be better to use _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l[^] here:
TCHAR szString[256];
_sntprintf_t(szString, sizeof(szString) / sizeof(TCHAR), _T("%G"), value);To get a double value back when reading the cell text use
_tstof
(atof, _atof_l, _wtof, _wtof_l[^]):// double CDataDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const
double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
{
CString str = GetItemText(hWnd, nItem, nSubItem);
return _tstof(str);
}I did it but I got this: "no suitable constructor exists from double to ATL::CStringT wchar_t StrTraitMFC__DLL>>" on
CString str = GetItemText(hWnd, nItem, nSubItem);
If I don't turn data from string to double could I "pass" them in a text file after edit them on the list view? :)
-
I did it but I got this: "no suitable constructor exists from double to ATL::CStringT wchar_t StrTraitMFC__DLL>>" on
CString str = GetItemText(hWnd, nItem, nSubItem);
If I don't turn data from string to double could I "pass" them in a text file after edit them on the list view? :)
Sorry, forget to rename it during copy & paste. Should be named
GetItemDouble
:.double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
{
CString str = GetItemText(hWnd, nItem, nSubItem);
return _tstof(str);
} -
Sorry, forget to rename it during copy & paste. Should be named
GetItemDouble
:.double CDataDialog::GetItemDouble(HWND hWnd, int nItem, int nSubItem) const
{
CString str = GetItemText(hWnd, nItem, nSubItem);
return _tstof(str);
}Thank you very much Sir!It was compiled fine!! I got a message about "Unhandled exception" when tried to write on second dialog but I must have made a mistake on code.Could I save editable data from list view on text file without converting them from string type?
-
Thank you very much Sir!It was compiled fine!! I got a message about "Unhandled exception" when tried to write on second dialog but I must have made a mistake on code.Could I save editable data from list view on text file without converting them from string type?
Use the debugger to find out when the exception occurs and check the code lines that has been executed before. You can write strings directly to text files when using appropriate file functions. But those will be then Unicode text files (when having a Unicode build) and the file class does no perform implicit conversion to ANSI text.
-
Hello everyone! I've used a list control to display data and an edit box to edit them. So I used this function:
void CDataDialog::SetCell(HWND hWnd1,
CString value, int nRow, int nCol)I want to make this change:
void CDataDialog::SetCell(HWND hWnd1,
double value, int nRow, int nCol)but I have problem with other functions such as GetDlgItemText which I don't know how to change. Here is the code:
void CDataDialog::SetCell(HWND hWnd1,
double value, int nRow, int nCol)
{
TCHAR szString[256];
swprintf(szString, value, 0);//Fill the LVITEM structure with the //values given as parameters. LVITEM lvItem; lvItem.mask = LVIF\_TEXT; lvItem.iItem = nRow; lvItem.pszText = szString; lvItem.iSubItem = nCol; if (nCol >0) //set the value of listItem ::SendMessage(hWnd1, LVM\_SETITEM, (WPARAM)0, (WPARAM)&lvItem); else //Insert the value into List ListView\_InsertItem(hWnd1, &lvItem);
}
CString CDataDialog::GetItemText(
HWND hWnd, int nItem, int nSubItem) const
{
LVITEM lvi;
memset(&lvi, 0, sizeof(LVITEM));
lvi.iSubItem = nSubItem;
CString str;
int nLen = 128;
int nRes;
do
{
nLen *= 2;
lvi.cchTextMax = nLen;
lvi.pszText = str.GetBufferSetLength(nLen);
nRes = (int)::SendMessage(hWnd,
LVM_GETITEMTEXT, (WPARAM)nItem,
(LPARAM)&lvi);
} while (nRes == nLen - 1);
str.ReleaseBuffer();
return str;
}void CDataDialog::OnClickList1(NMHDR *pNMHDR, LRESULT *pResult)
{
//LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast(pNMHDR);
// TODO: Add your control notification handler code here
//*pResult = 0;
Invalidate();
HWND hWnd1 = ::GetDlgItem(m_hWnd, IDC_LIST1);
LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE)pNMHDR;
RECT rect;
//get the row number
nItem = temp->iItem;
//get the column number
nSubItem = temp->iSubItem;
if (nSubItem == 0 || nSubItem == -1 || nItem == -1)
return;
//Retrieve the text of the selected subItem
//from the list
CString str = GetItemText(hWnd1, nItem,
nSubItem);RECT rect1, rect2; // this macro is used to retrieve the Rectanle // of the selected SubItem ListView\_GetSubItemRect(hWnd1, temp->iItem, temp->iSubItem, LVIR\_BOUNDS, &rect); //Get the Rectange of the listControl ::GetWindowRect(temp->hdr.hwndFrom, &rect1); //Get the Rectange of the Dialog ::GetWindowRect(m\_hWnd, &rect2); int x = rect1.left - rect2.left; int y = rect1.top - rect2.top; if (nItem != -1) ::SetWindowPos(::GetD
Is there some very important reason for you to use in a MFC application plain Win32 macros/messages to handle List Control? What is wrong to use [CListCtrl Class](https://msdn.microsoft.com/en-us/library/hfshke78.aspx) methods?
-
Is there some very important reason for you to use in a MFC application plain Win32 macros/messages to handle List Control? What is wrong to use [CListCtrl Class](https://msdn.microsoft.com/en-us/library/hfshke78.aspx) methods?
-
Use the debugger to find out when the exception occurs and check the code lines that has been executed before. You can write strings directly to text files when using appropriate file functions. But those will be then Unicode text files (when having a Unicode build) and the file class does no perform implicit conversion to ANSI text.
-
Yes!As I mentioned above I am looking for a way to save all editable data in list view in a text file.
lolici wrote:
...I am looking for a way to save all editable data in list view in a text file.
What does that have to do with not using MFC?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles