【发布时间】:2016-01-25 06:57:48
【问题描述】:
希望有人可以帮助我解决这个问题!
我有一个对话框,其中包含一些填充了数据的组合框,用户应该填写这些组合框,然后单击保存。当您单击保存时,程序会创建一个包含所选数据的输出文件。
我的问题是我需要在保存文件之前修剪连字符处的所有内容!
组合框填充了如下所示的字符串:
- 4010-第一
- 4020 秒
我希望它在修剪后看起来像这样:
- 4010
- 4020
还有:
- PH-彼得·汉森
- JK-约翰·金
我希望它在修剪后看起来像这样:
- PH
- JK
我使用 Visual Studio 6.0 和 MFC。
这是OnOK代码:
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);
}
提前致谢!
------------------------------------更新-------- ------------------------------
好的,经过一番困惑,我终于找到了一个对我有用的解决方案..
在你们给我的建议之后,我正在尝试做以下事情:
来自组合框的数据:
“4010-组”
我的代码:
pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);
int i = group.Find("-");
if (i >= 0)
{
group = group.Mid(0, i);
}
MessageBox(group); // results = 4010-group
没有用。
我认为可能存在一些与 UNICODE 相关的问题,所以我将 ComboBox 中的数据从 "4010-group" 更改为 "4010 group" 并尝试了以下操作:
pWnd = GetDlgItem(IDC_GROUP);
pWnd->GetWindowText(group);
int i = group.Find(" ");
if (i >= 0)
{
group = group.Mid(0, i);
}
MessageBox(group); // results = 4010
有效!但我不明白为什么连字符不起作用,有人知道吗?
【问题讨论】:
-
您要查找的术语不是修剪,它是 substring。
-
为什么不使用 CString::TrimRight 方法??
-
哦,好吧!那我来看看子字符串!我试过: group.TrimRight('-');但它对我不起作用
-
我不确定。 TrimRight 一直为我工作。你能告诉我你是怎么在这里使用它的吗?
-
请不要使用
CDialog::EndDialog()。而是使用默认的CDialog::OnOK()实现来关闭对话框。还请使用 MFC 标准的数据交换机制来替换像GetDlgItem()这样的丑陋调用