格式化文本框(电话号码,手机号码,电子邮件) [英] Formatting a Textbox (Phone Numbers, Mobile Numbers, Emails)

查看:109
本文介绍了格式化文本框(电话号码,手机号码,电子邮件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将文本框输入转换为不同的格式.我尝试使用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;
    }

这篇关于格式化文本框(电话号码,手机号码,电子邮件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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