格式化文本框(电话号码,手机号码,电子邮件)

编程入门 行业动态 更新时间:2024-10-17 13:26:55
本文介绍了格式化文本框(电话号码,手机号码,电子邮件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道如何将文本框输入转换为不同的格式.我尝试使用Format.String(),因为它是经过一些研究后在网上找到的解决方案",但对我而言效果并不理想.

I'd like to know how to cast textbox input into different formats. I tried using Format.String() since it's the 'solution' I found online after doing some research but it did not end well for me.

private void RegHomePhoneTBox_TextChanged(object sender, EventArgs e) { string s = RegHomePhoneTBox.Text; double sAsD = double.Parse(s); RegHomePhoneTBox.Text = string.Format("{0:###-####}", sAsD).ToString(); }

这是我使用的代码块,并且不断抛出错误.

This was the code block I used and it just keep on throwing an error.

"System.FormatException"类型的未处理异常发生在 mscorlib.dll附加信息:输入字符串不正确 格式.

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format.

推荐答案

尝试一下:

private void RegHomePhoneTBox_TextChanged(object sender, EventArgs e) { string s = RegHomePhoneTBox.Text; if (s.Length == 7) { double sAsD = double.Parse(s); RegHomePhoneTBox.Text = string.Format("{0:###-####}", sAsD).ToString(); } if (RegHomePhoneTBox.Text.Length > 1) RegHomePhoneTBox.SelectionStart = RegHomePhoneTBox.Text.Length; RegHomePhoneTBox.SelectionLength = 0; }

更多推荐

格式化文本框(电话号码,手机号码,电子邮件)

本文发布于:2023-11-24 23:22:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手机号码   文本框   电话号码   电子邮件

发布评论

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

>www.elefans.com

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