MaskedTextBox掩码,用于十进制,实数,浮点数?

编程入门 行业动态 更新时间:2024-10-09 10:20:16
本文介绍了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的maskedtextbox控件不如您希望的那样灵活,目前只允许使用固定宽度的掩码值,因为您将必须键入00002345才能在文本框中输入23.45.掩码的值会为您加上逗号和点,因此您实际上不必按动它们. 您可以通过执行以下操作来使用常规文本框控件实现类似的行为: 1.创建一个文本框控件,将其命名为Textbox1 2.在后面的代码中添加以下内容: sorry to say that the maskedtextbox control in a windows forms for either c# or VB 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 As System.EventArgs)句柄 Me .Load TextBox1.Text = " ___,___.__" 结束 子 ", "") 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掩码,用于十进制,实数,浮点数?

本文发布于:2023-05-25 19:52:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/236211.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实数   掩码   浮点数   MaskedTextBox   十进制

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!