vb怎樣做到限制字元長度的這種效果?比如在文字框裡輸入文字,輸入到字,游標就一直停在那裡

時間 2021-10-14 22:38:41

1樓:veket的小號

maxlenth屬性設為5

或者keypress事件

private sub text1_keypress(keyascii as integer)

if len(text1.text) = 5 thenif keyascii <> 8 then 'ascii碼為8的是 退格刪除鍵

keyascii = 0

end if

end if

end sub

2樓:匿名使用者

還有一種實現方法:

給文字框新增一個變化動作函式

private sub textbox1_change()var = textbox1.text

if len(var) >= 5 thentextbox1.text = left(var, 5)end if

end sub

附件是在vba裡實現的效果

3樓:難得糊塗

‘簡單一句:

if len(text1) > 5 then text1 = left(text1, 5): text1.selstart = 5: text1.sellength = 5

vb中如何輸入文字框文字輸入4個字元,在這4個字元後加上一個-?

4樓:匿名使用者

private sub text1_change()

if len(text1.text) = 4 then text1.text = text1.text & "-"

end sub

5樓:匿名使用者

我精心寫了一段哦!現在針對的是text1這個控制元件只需要放一個text1控制元件在窗體上在複製這段**貼上執行就可以了。。我想了3種方法,,一個個都試了,最後還是這個分配陣列的比較正確~

dim cl as boolean

private sub form_load()

text1.text = ""

end sub

private sub text1_change()

on error resume next

if cl = true then

cl = false

else

dim a() as string

a = split(text1.text, "-")

if len(text1.text) = 4 then

text1.text = text1.text & "-"

else

if len(a(ubound(a))) > 4 then

redim preserve a(ubound(a) + 1)

a(ubound(a)) = right(a(ubound(a) - 1), len(a(ubound(a) - 1)) - 4)

a(ubound(a) - 1) = left(a(ubound(a) - 1), 4)

else

end if

if len(a(ubound(a))) < 4 then exit sub

for p = 0 to ubound(a)

v = v & a(p) & "-"

next

cl = true

text1.text = v

end if

text1.selstart = len(text1.text)

end if

end sub

private sub text1_keydown(keycode as integer, shift as integer)

if keycode = 8 then

if right(text1.text, 1) = "-" then

cl = true

end if

end if

text1.selstart = len(text1.text)

end sub

private sub text1_keypress(keyascii as integer)

if keyascii = 45 then

keyascii = 0

end if

end sub

vb程式題輸入字元,判斷該字元是字母字元 數字字元還是其他字元,並做相應顯示

dim a as string a inputbox 請輸入一個字元 select case a case 0 to 9 msgbox 是數字 case a to z a to z msgbox 是字母 case else msgbox 是其它字元 end select private sub co...

vb字串和位元組陣列的轉換,VB字串和位元組陣列的轉換

當我們在vb中呼叫win32 api函式時,如果函式的返回值是乙個字串,那一般有如下三種情況 1.函式預先要求你提供乙個有固定空間的字串,以供儲存函式的返回值。2.函式的返回是乙個以null結尾的c字串,而不是正規的vb字串。3.win32 api函式有時候會返回另一種型別的字串。這種型別的字串在單...

VB中如何輸入文字框文字輸入字元,在這字元後加上

private sub text1 change if len text1.text 4 then text1.text text1.text end sub 我精心寫了一段哦!現在針對的是text1這個控制元件只需要放一個text1控制元件在窗體上在複製這段 貼上執行就可以了。我想了3種方法,一個...