はじめまして
VB6.0を使用しています。
テキストに「20060101」と入力すると自動的に「2006/01/01」となるように
プログラムを生成したいのですが、
5文字目と8文字目に「/」を入れるという処理がいまいちわからなくて・・・。
試しにやってみたのですが、
Private Sub txt_Birth_Change()
If Len(txt_Birth.Text) >= 5 Then
txt_Birth = Mid(txt_Birth, 1, 4) + "/" + Mid(txt_Birth, 5)
End If
End Sub
5文字目に「/」が入るように出来たのですが、その後どうしてよいのかわからなくて・・・。
どなたかアドバイスをいただけないでしょうか。
宜しくお願いします。
サンプルを持っていたので、
Private Sub txt_Birth_Change()
If Trim(txt_Birth.Text) = Replace(Trim(txt_Birth.Text), "/", "") Then
If Len(txt_Birth.Text) = 4 Then
txt_Birth.Text = txt_Birth.Text & "/"
txt_Birth.SelStart = 5
End If
Else
If Len(txt_Birth.Text) = 7 Then
txt_Birth.Text = txt_Birth.Text & "/"
txt_Birth.SelStart = 8
End If
End If
End Sub
いな様
ご回答ありがとうございます。
早速試してみたところ、うまく「/」が自動的に挿入されることが出来ましたが、
ご入力の際など、Backspaceキーが効かなくなってしまいます。
こちらに関しましてもご教授願いませんでしょうか。
1:BSキーを入力したかどうかを判定
2:文字数を調べて「/」の部分なら、最後の文字を消去
でできませんか?
マスクエディットコントロールを使ってみるとか。
通ってみたさんや弁士様の方法がベストっぽいですが。。
文字が減っているときは処理しないようにしてみました。
他の処理のときに変になるかもしれませんので、
一応参考までに。
-------------------------------------
Dim f As Integer
Private Sub txt_Birth_Change()
If f < Len(txt_Birth.Text) Then
If Trim(txt_Birth.Text) = Replace(Trim(txt_Birth.Text), "/", "") Then
If Len(txt_Birth.Text) = 4 Then
txt_Birth.Text = txt_Birth.Text & "/"
txt_Birth.SelStart = 5
End If
Else
If Len(txt_Birth.Text) = 7 Then
txt_Birth.Text = txt_Birth.Text & "/"
txt_Birth.SelStart = 8
End If
End If
End If
f = Len(txt_Birth.Text)
End Sub
>ご入力の際など、Backspaceキーが効かなくなってしまいます。
が〜ん、これ古いサンプルでした。
すいません。新しいサンプルは自社にあるようで、
手元にありませんでした。
ツイート | ![]() |