MaskedTextBox掩码,用于十进制,实数,浮点数? [英] MaskedTextBox mask for decimal, real, float ?

查看:77
本文介绍了MaskedTextBox掩码,用于十进制,实数,浮点数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我试图在掩码文本框中设置仅用于十进制或实数的掩码.
我已经尝试999,999.00或000,000.00或###,###.##.
但是当我尝试输入例如23.45值在按或之后没有任何改变.和
我收到234 5 __.__的值.我认为按或后. carret应该移动到小数点后.

怎么做?掩盖的文本框有可能以这种方式设置吗?

请帮助我.

Rafal

Hi All,

I''m trying to set up mask in masked text box for decimal or real numbers only.
I''ve tried 999,999.00 or 000,000.00 or ###,###.##.
But when i''m trying input e.g. 23.45 value nothing changed after pressing , or . and
I receive 234 5__.__ value. I think after pressing , or . carret should move on decimal place.

How to do it? Has masked text box the possibility to set it up in that way?

Please help me in this.

Rafal

推荐答案

很抱歉,Windows窗体中的C#或VB.net的maskedtextbox控件不如您希望的那样灵活,目前只允许使用固定宽度的掩码值,因为您将必须键入00002345才能在文本框中输入23.45.掩码的值会为您加上逗号和点,因此您实际上不必按动它们.

您可以通过执行以下操作来使用常规文本框控件实现类似的行为:
1.创建一个文本框控件,将其命名为Textbox1
2.在后面的代码中添加以下内容:
sorry to say that the maskedtextbox control in a windows forms for either c# or VB.net is not as flexible as you would hope it to be, it currently allow a fixed width masked value only, as in you will have to type 00002345 to get 23.45 in the textbox. the masked value puts the comma and dot for you so you really don''t have to press them.

you can achieve a similar behavior using a regular textbox control by doing the following:
1. Create a textbox control let''s call it Textbox1
2. In the code behind add the following:
Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
    If TextBox1.Text = "___,___.__" Then
        'removes the mask once the mouse cursor is within the textbox
        TextBox1.Text = ""
    End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    Dim ListofChars As String = "0123456789.,"
    If ListofChars.IndexOf(e.KeyChar) = -1 Then
        e.Handled = True
    End If
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
    If TextBox1.Text = "" Then
        'puts the mask back if you didn't enter anything into the textbox once the mouse cursor leave the textbox
        TextBox1.Text = "___,___.__"
    End If
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    TextBox1.Text = TextBox1.Text.Trim
    If Not TextBox1.Text = "" And Not TextBox1.Text = "___,___.__" Then
        Dim aDecimal As Decimal = CType(TextBox1.Text, Decimal)
        TextBox1.Text = Format(aDecimal, "c").Replace("


", ") 结束 如果 结束 私有 Form1_Load( ByVal 发​​件人目标 对象 ByVal e 句柄 " 结束
", "") End If End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load TextBox1.Text = "___,___.__" End Sub


这篇关于MaskedTextBox掩码,用于十进制,实数,浮点数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆