如何保证wxTextCtrl只输入数字 Topic is solved

这是wxWidgets论坛的中文版本。在这里,您可以用您的母语汉语讨论上面任一子论坛所涉及的所有关于wxWidgets的话题。欢迎大家参与到对有价值的帖子的中英互译工作中来!
Post Reply
kingkamg
I live to help wx-kind
I live to help wx-kind
Posts: 187
Joined: Tue Apr 08, 2008 1:45 pm

如何保证wxTextCtrl只输入数字

Post by kingkamg » Tue Jul 15, 2008 1:03 am

wxTextValidator validator(wxFILTER_NUMERIC);
new wxTextCtrl( this, wxID_ANY, _T(""), wxDefaultPosition, wxDefaultSize, 0, validator );
这样虽然可以只输入数字,单小数点确可以输入多次,难道一个数据里面可以多个小数点,谁有解决办法吗?

00061205
Knows some wx things
Knows some wx things
Posts: 41
Joined: Mon Jun 16, 2008 3:43 am
Location: Beijing, China

Post by 00061205 » Tue Jul 15, 2008 3:31 am

在OnTextEnter()里写个if语句判断一下,如果超过1个小数点就禁止。
Regards,

00061205

kingkamg
I live to help wx-kind
I live to help wx-kind
Posts: 187
Joined: Tue Apr 08, 2008 1:45 pm

Post by kingkamg » Tue Jul 15, 2008 4:38 am

单我希望在用户输入之前就过滤掉,而不是让它输入后再过滤

liuqi5521
Earned some good credits
Earned some good credits
Posts: 103
Joined: Thu Apr 03, 2008 5:35 am
Location: China
Contact:

Post by liuqi5521 » Tue Jul 15, 2008 12:06 pm

可以在Change事件里面判断并纠正。

liuqi5521
Earned some good credits
Earned some good credits
Posts: 103
Joined: Thu Apr 03, 2008 5:35 am
Location: China
Contact:

Post by liuqi5521 » Tue Jul 15, 2008 11:53 pm

首先,响应 wxEVT_COMMAND_TEXT_UPDATED 事件,该事件在文本框内容改变后发生。

然后编写事件处理函数如下:

Code: Select all



/*
 * WxEdit2Updated
 */
void test1Dlg::WxEdit2Updated(wxCommandEvent& event)
{
	// insert your code here
	//MessageBox(0,"a","a",MB_OK);
	static wxString oldstr="";
	double d;
	if (NULL == WxEdit2)return;
	if (WxEdit2->GetValue().ToDouble(&d)  ||  0== WxEdit2->GetValue().Length() )
	{
        oldstr =WxEdit2->GetValue();

    }
	else
	{
        WxEdit2->SetValue(oldstr);
        WxEdit2->SetInsertionPoint(WxEdit2->GetValue().Length());
    }
}



Post Reply