如何修复文本框中的格式

时间:2014-07-23 23:36:20

标签: vb.net

如何在文本框中修复我的格式,因为有时我的输出没有给我正确的格式,因此无法在我的数据库中读取。

我的文本框中的示例输出:

Sample problem 1
Textbox7.text = 01.0107/23/2014 or ##.####/##/#### 
it should be:
Textbox7.text = 01.01 07/23/2014 or ##.## ##/##/####

Sample problem 2
Textbox7.text = 01.017/23/2014 or ##.###/##/####
it should be:
Textbox7.text = 01.01 07/23/2014 or ##.## ##/##/####

Sample problem 3
Textbox7.text = 01.07/23/2014 or ##.###/##/####
it should be:
Textbox7.text = 01.00 07/23/2014 or ##.## ##/##/####

它应该有一个空间" "在我的dateformat之前。

这是我目前的条件代码但没有运作:

If TextBox7.Text = Format((TextBox7.Text), "##.####/##/####") Then
    TextBox7.Text = Format((TextBox7.Text), "##.## ##/##/####")

ElseIf TextBox7.Text = Format((TextBox7.Text), "##.###/##/####") Then
    TextBox7.Text = Format((TextBox7.Text), "##.## ##/##/####")
Else
    TextBox7.Text = Format((TextBox7.Text), "##.## ##/##/####")
End If

1 个答案:

答案 0 :(得分:0)

在所有评论之后,这个答案假设您坚持使用当前的输入法出于某种原因并且输入总是:

FirstDate: HH.mm MM / dd / yyyy SecondDate HH.mm

在此之前:我认为您的IF声明无效。所有条件的比较总是假的。除非你输入##。## ## / ## / ####?

If TextBox7.Text = Format((TextBox7.Text), "##.## ##/##/####") Then

enter image description here

试试这个?

Private Sub SumDate()
    Dim firstDate As New DateTime
    Dim sumDate As New DateTime

    Dim firstDateString As String = String.Format(CultureInfo.InvariantCulture, "{0:00.00 00/00/0000}", TextBox1.Text)
    Dim secondDateString As String = String.Format(CultureInfo.InvariantCulture, "{0:00.00}", TextBox2.Text)

    firstDate = DateTime.ParseExact(firstDateString, "HH.mm MM/dd/yyyy", CultureInfo.InvariantCulture)

    Dim timeParts As String() = secondDateString.Split(".")

    Dim totalHours As Integer = timeParts(0)
    'Update: this allow the input of (30) or (30.00) by checking the length of timeParts
    Dim totalMins As Integer = 0
    If timeParts.Length = 2 Then totalMins = timeParts(1)

    sumDate = firstDate.AddHours(totalHours).AddMinutes(totalMins)

    TextBox3.Text = sumDate.ToString("HH.mm MM/dd/yyyy", CultureInfo.InvariantCulture)
End Sub