送分100分:怎么控制在文件框中输入的字符格式
送分100分:如何控制在文件框中输入的字符格式。
现有一个问题是:
在一个文本框中输入时间,包含时和分两部分,输入的形式可能会有003(即0:03)、830(即8:30)、1212(即12:12)
现在的要求是,用户输入(1)003后,文本框自动显示为00:03
(2)830后,文本框内容自动显示为08:30
(3)1212后,文本框内容自动显示为12:12
------解决方案--------------------
'文本框 Text1,Text2,Text1中输入时间(3-4位),Text2中自动显示转换,具体其它处理自己再优化一下
Private Sub Text1_Change()
If Len(Text1.Text) = 3 Then
Text2.Text = Format(Text1.Text, "0:00")
ElseIf Len(Text1.Text) = 4 Then
Text2.Text = Format(Text1.Text, "00:00")
Else
Text2.Text = ""
End If
End Sub
------解决方案--------------------
现有一个问题是:
在一个文本框中输入时间,包含时和分两部分,输入的形式可能会有003(即0:03)、830(即8:30)、1212(即12:12)
现在的要求是,用户输入(1)003后,文本框自动显示为00:03
(2)830后,文本框内容自动显示为08:30
(3)1212后,文本框内容自动显示为12:12
------解决方案--------------------
'文本框 Text1,Text2,Text1中输入时间(3-4位),Text2中自动显示转换,具体其它处理自己再优化一下
Private Sub Text1_Change()
If Len(Text1.Text) = 3 Then
Text2.Text = Format(Text1.Text, "0:00")
ElseIf Len(Text1.Text) = 4 Then
Text2.Text = Format(Text1.Text, "00:00")
Else
Text2.Text = ""
End If
End Sub
------解决方案--------------------
- VB code
Private Sub Text1_KeyPress(KeyAscii As Integer) If (KeyAscii < 32) Then If (KeyAscii = 13) Then If (Len(Text1) < 2 Or Len(Text1) > 4) Then MsgBox "输入数据错误!", 48 Else Select Case i Case 2: Text1 = "0" & Left(Text1, 1) & ":0" & Right(Text1, 1) Case 3: Text1 = "0" & Left(Text1, 1) & ":" & Right(Text1, 2) Case 4: Text1 = Left(Text1, 2) & ":" & Right(Text1, 2) End Select End If Else Exit Sub End If End If If (KeyAscii < 48 Or KeyAscii > 57) Then KeyAscii = 0 End Sub
------解决方案--------------------
- VB code
Dim IsSetText As Boolean Private Sub Text1_Change() If IsSetText = True Then Exit Sub If Len(Text1.Text) > 3 Then Text1_LostFocus End If End Sub Private Sub Text1_GotFocus() Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) End Sub Private Sub Text1_LostFocus() IsSetText = True Text1.Text = Format(Text1.Text, "00:00") IsSetText = False Text1.SelStart = 0 Text1.SelLength = Len(Text1.Text) End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then Text1_LostFocus End If End Sub
------解决方案--------------------
简单的字符串处理,用right和left函数分解字符串为2部分,再用format格式为需要的格式.相比来说1楼的更简洁
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------
= format (txt, "#0:00")
------解决方案--------------------
用不着两个 Format 格式。
Format("830", "00:00") 的结果就是 "08:30"。