Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 格式化文本框。文本特定格式,如:###.######/####-##_.net_Vb.net_Textbox_Format - Fatal编程技术网

.net 格式化文本框。文本特定格式,如:###.######/####-##

.net 格式化文本框。文本特定格式,如:###.######/####-##,.net,vb.net,textbox,format,.net,Vb.net,Textbox,Format,我需要14个字符的格式: 10.257.938/0001.45 但是当我执行这个代码时,我得到 .###.###/####- 在我的txtcpnpj.text上 我真的不喜欢用面具盒。 这是我的代码(关于失去焦点): 返回 由安德鲁·莫顿解决, 泰安德鲁。乌胡尔 在我的案例中,解决方案是: Private Sub txtcnpj_LostFocus(sender As Object, e As EventArgs) Handles txtcnpj.LostFocus If Len(t

我需要14个字符的格式:

10.257.938/0001.45

但是当我执行这个代码时,我得到

.###.###/####- 在我的
txtcpnpj.text

我真的不喜欢用面具盒。 这是我的代码(关于失去焦点):

返回

由安德鲁·莫顿解决, 泰安德鲁。乌胡尔

在我的案例中,解决方案是:

Private Sub txtcnpj_LostFocus(sender As Object, e As EventArgs) Handles txtcnpj.LostFocus

    If Len(txtcnpj.Text) > 0 Then

        Select Case Len(txtcnpj.Text)

                      Case Is = 14
                Dim A As String
                A = txtcnpj.Text.Replace("."c, "").Replace("/"c, "")
                txtcnpj.Text = String.Concat(A.Substring(0, 2), ".", A.Substring(2, 3), ".", A.Substring(5, 3), "/", A.Substring(8, 4), "-", A.Substring(12, 2))

        End Select
    End If

End Sub

这样的格式是针对数字,而不是字符串

编写自己的方法最简单,例如:

Option Strict On

Module Module1

    Function FormatWithSlash(s As String) As String
        s = s.Replace("."c, "").Replace("/"c, "")
        Return String.Concat(s.Substring(0, 2), ".", s.Substring(2, 3), ".", s.Substring(5, 3), "/", s.Substring(8, 4), ".", s.Substring(12, 2))
    End Function

    Sub Main()
        Console.WriteLine(FormatWithSlash("12345678901234"))
        Console.WriteLine(FormatWithSlash("10.257.938/0001.45"))

        Console.ReadLine()

    End Sub

End Module
产出:

12.345.678/9012.34
10.257.938/0001.45


使用
#
。。。而不是
@
…相同的错误查看图像:erro,因为返回字符串不是值。需要转换。有什么想法吗?你想要一个Sub,而不是一个函数,你需要使用
yourControl.Text=String.Concat(…)
。你是最棒的,需要解决方案代表。像天堂里的婴儿一样工作。我没有15分喜欢你的代表,但你解决了我的问题。我真的很感谢你,因为我没有足够的知识来解决这个问题,现在评估你写的代码,我有知识来格式化任何值;非常感谢。你能告诉我一个足够好的地方供你进一步学习吗?例如,在这个案例中,你是在哪里学习的?或者随着时间的推移你学会了。。。
Option Strict On

Module Module1

    Function FormatWithSlash(s As String) As String
        s = s.Replace("."c, "").Replace("/"c, "")
        Return String.Concat(s.Substring(0, 2), ".", s.Substring(2, 3), ".", s.Substring(5, 3), "/", s.Substring(8, 4), ".", s.Substring(12, 2))
    End Function

    Sub Main()
        Console.WriteLine(FormatWithSlash("12345678901234"))
        Console.WriteLine(FormatWithSlash("10.257.938/0001.45"))

        Console.ReadLine()

    End Sub

End Module