我是用来做用户注册时在密码确认中,要求两个编辑框中的字符串内容一样。
先说明一下 该字符串类型是CString
曾经试用过 Compare来比较,但是都不成功。
不知道是 UpdateData(TRUE)时出错,还是在比较时该转换字符串的类型。
先说明一下 该字符串类型是CString
曾经试用过 Compare来比较,但是都不成功。
不知道是 UpdateData(TRUE)时出错,还是在比较时该转换字符串的类型。
if(0 == str1.Compaer(str2)) //相同
有错误,它说 error C2660: 'GetBuffer' : function does not take 0 parameters
是不是 GetBuffer 函数在什么头文件中,而我没有包含进来?
UpdateData(TRUE);
str1=m_Edit1;
str2=m_Edit2;
if(!str1.Compare(str2))
{
AfxMessageBox("两个字符串相等");
}
我的是这样的,但是总不成功。除非你什么都不填,按提交时,它就成功。
CString str1,str2;
UpdateData(TRUE);
if(!str1.Compare(str2))
{
AfxMessageBox("两个字符串相等");
}
首先把两个变量str1,str2关联到两个CEdit控件上,再运行代码
((CEdit *)GetDlgItem(IDC_EDIT1)) ->GetWindowsText(str1) ;
((CEdit *)GetDlgItem(IDC_EDIT2)) ->GetWindowsText(str2) ;
if(str1 == str2)
{
//...
}
如果字符串是一样的则返回非零值;如果CString对象小于lpsz则返回值<0,如果CString对象大于lpsz则返回值>0。参数: lpsz 要用于比较的另一个字符串。 说明:
此成员函数通过使用通用文本函数_tcscmp来比较这个CString对象和另一个字符串。此通用文本函数_tcscmp是在TCHAR.H中定义的,根据在编译时设置的字符来与strcmp,wcscmp,或_mbscmp对应。这些函数的每一个都根据当前使用的代码页来进行一次区分大小写的比较,而且不会被现场影响。更多的信息,参见“Microsoft Visual C++ 6.0运行库参考”中的strcmp,wcscmp,_mbscmp。示例:下面的例子说明了如何使用CString::Compare。
// CString::Compare示例:
CString s1( "abc" );
CString s2( "abd" );
ASSERT( s1.Compare( s2 ) == -1 ); // 与另一个CString比较。
ASSERT( s1.Compare( "abe" ) == -1 ); // 与LPTSTR字符串比较。
str2=m_Edit2;
你不会是用这两条语句进行的变量关联吧?
如果m_Edit1和m_Edit2是关联的字符串变量,那CString str1,str2; 就没必要定义
if (m_Edit1.Compare(m_Edit2) == 0)即可,但愿m_Edit你定义的不是CEdit类
////////////
xx.h
CEdit m_str2;
CEdit m_str1;
/////////////////
xx.cpp
CString mstr1,mstr2; m_str1.GetWindowText(mstr1);
m_str2.GetWindowText(mstr2); if (!mstr1.Compare(mstr2)) {
AfxMessageBox("两个字符串相等");
}
else{
AfxMessageBox("两个字符串不相等");
}
////////////
xx.h
CEdit m_str2;
CEdit m_str1;
/////////////////
xx.cpp
CString mstr1,mstr2; m_str1.GetWindowText(mstr1);
m_str2.GetWindowText(mstr2); if (!mstr1.Compare(mstr2)) {
AfxMessageBox("两个字符串相等");
}
else{
AfxMessageBox("两个字符串不相等");
}
//在我这里试是正确的。
UpdateData();
if(!m_Edit1.Compare(m_Edit2))
{
}
CString 和CEdit你怎么定义就怎么来么。如果定义CString类型的,16楼的代码没问题,我测试也通过。如果定义CEdit类型,在里面定义临时变量CString str1,str2来接收内容12楼的代码也测试过了。此贴结束!
CString str2;
GetDlgItemText(IDC_EDIT1, str1);
GetDlgItemText(IDC_EDIT2, str2);
if(str1 == str2)
{
AfxMessageBox("相等");
}
else
{
AfxMessageBox("不相等");
}
CString str1
CString str2
直接
if (str1.Compare(str2))
使用vc提供的向导(ctrl+w),选择"Member Variables"页,双击要关联变量的ID,让两个编辑框的变量名分别,为 m_Edit1, m_Edit2;然后在确定按钮下加上下面代码:CString str1,str2;
UpdateData(TRUE);
str1=m_Edit1;
str2=m_Edit2;
if(!str1.Compare(str2))
{
AfxMessageBox("两个字符串相等");
}
大哥真的不行啊。
UpdateData(TRUE);
if(!m_Edit1.Compare(m_Edit2))
{
AfxMessageBox("两个字符串相等");
}
即可,还有一种情况就是楼主你的VC有问题,请检查DoDataExchange函数下是否有:
DDX_Text(pDX, IDC_EDIT1, m_Edit1);
DDX_Text(pDX, IDC_EDIT2, m_Edit2);
如果没有,请加上去。
上面诸位大哥都说的很清楚了,为什么我这里就是OK呢?
对你的vc 6.0设置表示怀疑,是不是哪里没设置好。
帮不上了……
我看了看,是有下面的代码的。
DDX_Text(pDX, IDC_EDIT1, m_Edit1);
DDX_Text(pDX, IDC_EDIT2, m_Edit2);
不打勾时,测试就通过,打上勾后就不行,请问这有什么方法解决?
CString cstr1 = m_edt1.GetWidowsText();
CString cstr2 = m_edt2.GetWidowsText();
if(0 == cstr1.Compare(cstr2))
{
//相同
} 这样做还有问题你来找我,肯定相同。