在初始化函数中加入
CEdit *e_Port = (CEdit*)this->GetDlgItem(IDC_EDIT2);
e_Port->SetLimitText(5);
重写编辑框的EN_CHANGE事件:
不能输入非数字的字符,且限制位数5位,且范围为0~65535.
void CLoginDIalog::OnEnChangeEdit2()
{
// TODO: 如果该控件是 RICHEDIT 控件,它将不
// 发送此通知,除非重写 CDialogEx::OnInitDialog()
// 函数并调用 CRichEditCtrl().SetEventMask(),
// 同时将 ENM_CHANGE 标志“或”运算到掩码中。
// TODO: 在此添加控件通知处理程序代码
CEdit *p_port = (CEdit*)GetDlgItem(IDC_EDIT2);
CString port_content;
p_port->GetWindowText(port_content);
int intNo = _tstoi(port_content);
CString sNo;
sNo.Format(_T("%d"), intNo);
if (sNo != port_content)
{
p_port->SetWindowText(sNo);
int position = sNo.GetLength();
p_port->SetSel(position, position, FALSE);
return;
}
UpdateData(TRUE);
if (mV_port < 0 || mV_port > 65535)
{
MessageBox("端口的范围为0~65535\n");
if (mV_port > 65535)
{
mV_port = 65535;
}
else
{
mV_port = 1;
}
UpdateData(FALSE);
return;
}
}