有没有字符串转变量的函数呢?

MEFULEU 2009-11-23 08:55:38
假设m_Out1为一个EDit的控件变量,并且这些控件N多,少量可以用if直接处理,但是这种方式和代码感觉很繁琐,有没有办法解决呢?

CString str,strValue;

//...处理过程

if (str=="m_Out1") m_Out1 =strValue;
if (str=="m_Out2") m_Out2 =strValue;
if (str=="m_Out3") m_Out3 =strValue;
if (str=="m_Out4") m_Out4 =strValue;
if (str=="m_Out5") m_Out5 =strValue;
if (str=="m_Out6") m_Out6 =strValue;


////////////////////////////////////////////////

如果有一个函数。。。 StrToVal(str)=strValue; 岂不是简洁多了。。。。各位大虾有没有这方面的经验呢?



...全文
260 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
MEFULEU 2009-11-23
  • 打赏
  • 举报
回复
就这样子吧。谢谢大家。
fandh 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mefuleu 的回复:]
郁闷,那个id编号不是顺序的。。。
#define IDC_EDIT_Out1      1038
#define IDC_EDIT_Out2      1090
#define IDC_EDIT_Out3      1040
#define IDC_EDIT_Out4      1091

。。。。
本来我的本意直接用  IDC_EDIT_Out + index ,这样子。。也就是一个变量。。。。不过看来目前这种方法并不好用。。。
[/Quote]
自己手动改一些就行了!
#define IDC_EDIT_Out1 1038
#define IDC_EDIT_Out2 1039
#define IDC_EDIT_Out3 1040
#define IDC_EDIT_Out4 1041
......
flyoxs 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mefuleu 的回复:]
郁闷,那个id编号不是顺序的。。。
#define IDC_EDIT_Out1      1038
#define IDC_EDIT_Out2      1090
#define IDC_EDIT_Out3      1040
#define IDC_EDIT_Out4      1091

。。。。
本来我的本意直接用  IDC_EDIT_Out + index ,这样子。。也就是一个变量。。。。不过看来目前这种方法并不好用。。。
[/Quote]
那个ID可以直接修改它,没事。
MoXiaoRab 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mefuleu 的回复:]
郁闷,那个id编号不是顺序的。。。
#define IDC_EDIT_Out1      1038
#define IDC_EDIT_Out2      1090
#define IDC_EDIT_Out3      1040
#define IDC_EDIT_Out4      1091

。。。。
本来我的本意直接用  IDC_EDIT_Out + index ,这样子。。也就是一个变量。。。。不过看来目前这种方法并不好用。。。
[/Quote]
你弄个ID数组放上面的那些控件ID就好了啊,要用的时候GetDlgItem(ID)不就行了
MEFULEU 2009-11-23
  • 打赏
  • 举报
回复
郁闷,那个id编号不是顺序的。。。
#define IDC_EDIT_Out1 1038
#define IDC_EDIT_Out2 1090
#define IDC_EDIT_Out3 1040
#define IDC_EDIT_Out4 1091

。。。。
本来我的本意直接用 IDC_EDIT_Out + index ,这样子。。也就是一个变量。。。。不过看来目前这种方法并不好用。。。
flyoxs 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 mefuleu 的回复:]
貌似哈稀表是个不错的注意,不过还要初始化。。。。那也是一大串的东东了。。。。
[/Quote]

总比写一大堆if/else好多了。具体怎么解决,要看你的实际项目安排
MEFULEU 2009-11-23
  • 打赏
  • 举报
回复
貌似哈稀表是个不错的注意,不过还要初始化。。。。那也是一大串的东东了。。。。
fandh 2009-11-23
  • 打赏
  • 举报
回复
if (str=="m_Out1") m_Out1 =strValue;
if (str=="m_Out2") m_Out2 =strValue;
if (str=="m_Out3") m_Out3 =strValue;
if (str=="m_Out4") m_Out4 =strValue;
if (str=="m_Out5") m_Out5 =strValue;
if (str=="m_Out6") m_Out6 =strValue;
对于这种情况,只要这样:
CString temp = str.Right(str.GetLength()-5);//取出后面的数字
int index = atoi(temp)-1;
((CEdit*)GetDlgItem(ID_EDIT1+index)->SetWindowText(strValue);

当然,前提是你所用到edit控件的ID都是顺序的,并且第一个就是ID_EDIT1

flyoxs 2009-11-23
  • 打赏
  • 举报
回复
用哈稀表保存:
CMap<CString, CEdit*> mapEdits;

使用时:
CEdit * pEdit;
mapEdits.Lookup(str, pEdit);
pEdit->SetWindowText(strValue);

不过初始化时要:
mapEdits["m_Out1"] = &m_Out1;
...
wangk 2009-11-23
  • 打赏
  • 举报
回复
map[str] = value;
fandh 2009-11-23
  • 打赏
  • 举报
回复
建议将所有edit的ID都改成顺序的,自己到resource.h里面看看每个edit的ID的定义,如果不是顺序的,自己手动改一下!然后,做一个循环:
for (int i=0;i<number;i++)
{
((CEdit*)GetDlgItem(ID_EDIT1+i)->SetWindowText(strValue);
}
number为个数
用户 昵称 2009-11-23
  • 打赏
  • 举报
回复
没有吧,不过这种用法一般要用数组。
aa3000 2009-11-23
  • 打赏
  • 举报
回复
mfc 可以关联到成员变量。

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧