Hopefully someone can help me with this issue!
I have a dialog with a few Combo Boxes that are populated with data, which the user is supposed to fill in and then click save. When you click save, the program creates an outfile with the chosen data in it.
My problem is that I need to trim everything at the hyphen before saving the file!
The Combo Boxes is populated with strings that looks something like this:
And I want it to look like this after the trim:
And:
And I want it to look like this after the trim:
I use Visual Studio 6.0 and MFC.
Here is the OnOK
Code:
void CExportChoices::OnOK()
{
CString sFileName, name, height, weight, age, haircolor, eyecolor, initials, group;
CWnd* pWnd = GetDlgItem(IDC_NAME);
pWnd->GetWindowText(name);
sFileName.Format(".\\Export\\%s_export%d.txt", name, GetTickCount());
ofstream outfile(sFileName,ios::out);
pWnd = GetDlgItem(IDC_HEIGHT);
pWnd->GetWindowText(height);
pWnd = GetDlgItem(IDC_WEIGHT);
pWnd->GetWindowText(weight);
pWnd = GetDlgItem(IDC_AGE);
pWnd->GetWindowText(age);
pWnd = GetDlgItem(IDC_HAIRCOLOR);
pWnd->GetWindowText(haircolor);
pWnd = GetDlgItem(IDC_EYECOLOR);
pWnd->GetWindowText(eyecolor);
pWnd = GetDlgItem(IDC_INITIALS);
pWnd->GetWindowText(initials);
pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);
outfile << "Height=" << height << "\n";
outfile << "\n";
outfile << "Weight=" << weight << "\n";
outfile << "\n";
outfile << "Age=" << age << "\n";
outfile << "\n";
outfile << "Hair color=" << haircolor << "\n";
outfile << "\n";
outfile << "Eye color=" << eyecolor << "\n";
outfile << "\n";
outfile << "Initials=" << initials << "\n";
outfile << "\n";
outfile << "Group=" << group << "\n";
outfile.close();
CDialog::EndDialog(22);
}
Thanks in advance!
------------------------------------UPDATE-------------------------------------
Okay, after some confusion I have finally found a solution that works somewhat for me..
Here is what I was trying to do after the advice you guys gave me:
Data from the ComboBox:
"4010-group"
My code:
pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);
int i = group.Find("-");
if (i >= 0)
{
group = group.Mid(0, i);
}
MessageBox(group); // results = 4010-group
It did not work.
I thought maybe there was some UNICODE related issue, so I changed the data in the ComboBox from "4010-group" to "4010 group" and tried this:
pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);
int i = group.Find(" ");
if (i >= 0)
{
group = group.Mid(0, i);
}
MessageBox(group); // results = 4010
It works! But I don't understand why the hyphen doesn't work, does anyone have a clue?
You can use CString::Find
and CString::Mid
, which is similar to wstring::find
and wstring::substr
See also CString functions
CString s = L"4010-First";
int i = s.Find('-');
if (i >= 0)
{
s = s.Mid(0, i);
TRACE(L"%s\n", s); //output: "4010"
}
or to get first and second part:
CString s1 = s.Mid(0, i);
CString s2 = s.Mid(i + 1);
TRACE(L"(%s)(%s)\n", s1, s2); //output: (4010)(First)