在VB文字框中只允許輸入5位數字,格式為123 12 如何實現

2022-07-19 03:55:19 字數 6117 閱讀 6684

1樓:匿名使用者

對了忘記說了text控制元件的maxlength屬性設定為6 你自己試試吧。沒問題的

private sub text1_keypress(keyascii as integer)

select case len(text1.text)

case 0

if isnumeric(chr(keyascii)) = true or keyascii = 8 then

else

keyascii = 0

end if

case 1

if isnumeric(chr(keyascii)) = true or keyascii = 8 then

else

keyascii = 0

end if

case 2

if isnumeric(chr(keyascii)) = true or keyascii = 8 then

else

keyascii = 0

end if

case 3

if keyascii = 46 or keyascii = 8 then

else

keyascii = 0

end if

case 4

if isnumeric(chr(keyascii)) = true or keyascii = 8 then

else

keyascii = 0

end if

case 5

if isnumeric(chr(keyascii)) = true or keyascii = 8 then

else

keyascii = 0

end if

end select

end sub

private sub text1_change()

if val(text1.text) >= 1000 then text1.text = mid(text1.text, 2, len(text1.text) - 1)

end sub

2樓:

private sub command1_click()dim 值 as single

if len(text1) <> 6 thenmsgbox "你輸入的位數不對啊"

exit sub

end if

值 = val(text1)

if 值 >= 1000 or 值 < 100 thenmsgbox "你輸入的不是三位整數和兩位小數啊"

else

msgbox "你的輸入符合要求"

end if

end sub

private sub form_load()command1.caption = "確定"

text1 = ""

end sub

答案補充:

為了你這個三維整數難場咋咧!到頭來,你卻「當我輸入完123.12後,然後將最後的一位刪掉,將游標移動到小數點前,輸入數字」。那你前面說那些幹什麼?

vb文字框如何實現只能輸入1-100數字

3樓:

方法1change事件裡限制ascii除了數字外其他的都禁用禁用滑鼠右鍵(防止複製)

把文字框內容轉為整數型後(預設為字串型)判斷是否屬於1~100如果不屬於則報錯

方法2在文字框失去焦點事件裡

增加判斷輸入內容是否為整數~是否為1~100這種是隻要求結果不要求過程的方法

那就用方法1

4樓:

文字框的 maxlength 屬性設定最大

一個新的text 都是空的。要是最小要求1那麼你可以在提交的時候 檢測下。是否為空?

if text1.text=""then

5樓:網海1書生

private sub text1_lostfocus()

if val(text1.text) < 0 or val(text1.text) > 100 then msgbox "請輸入1-100的數字"

end sub

vb文字框怎樣限制只能輸入數字值

6樓:匿名使用者

1、 新建一個標準exe程式。

2、 繪製介面,新增一個 textbox 控制元件,改名為 txbnumber。

3、 編寫**。在**視窗中,新增 txbnumber_keypress 事件。

4、查ascii碼錶,得到0的ascii碼是48。輸入以下語句:

if keyascii < 48 or keyascii > 57 then keyascii = 0

這條語句用來判斷輸入的字元是否在0-9的範圍,如果不在這個範圍,就把這個輸入的字元遮蔽掉。

5、但這麼做會產生一個問題,就是使用backspace刪除字元的時候,由於按鍵被遮蔽,無法刪除。只要在上面的語句前在新增一條語句:if keyascii = 8 then exit sub,意思是,如果按了backspace,就直接退出該過程,按鍵就不會被遮蔽了。

6、除錯執行。按f5,在文字框中輸入任意字元,可以看到只有數字能顯示在文字框中。按退格鍵,字元也能正常刪除。

7、 如果輸入的數字可能是小數,那麼還要新增如下**:

if keyascii = 46 and not cbool(instr(txbnumber, ".")) then exit sub

當輸入小數點時,程式判斷文字框中是否已有小數點(因為一個小數中不可能有多個小數點),如果沒有小數點,則允許輸入。

7樓:丁滿

(二樓那個不能輸入小數點和科學計算,還有其他進位制,我重新編了一個,要事先把文字框的tag屬性設為"0")

private sub text1_change()

if isnumeric(text1.text) then

text1.tag = text1.text

end if

end sub

private sub text1_lostfocus()

if text1.text = "" then text1.text = "0": exit sub

if not isnumeric(text1.text) then

if msgbox("只能輸入數字!單擊「是」重新編輯。", vbyesno) = vbyes then

text1.setfocus

else

text1.text = val(text1.tag)

end if

end if

end sub

如需特殊的數字要求(比如只能輸入整數,不能為0等,請繼續追問,很願意為你解答

8樓:匿名使用者

新增以下事件即可。

private sub text1_keypress(keyascii as integer)

if keyascii < 48 or keyascii > 57 then keyascii = 0

end sub

9樓:

用numericupdown

或者maskedtextbox設定mask = 9999.99

vb中text文字框的位數一定,輸入位數不夠,到前面怎麼補0 5

10樓:博學多才的小子

text1.text = format(txthuor.text,"000")'其中000為多少位,txthour為文字框的數值

11樓:匿名使用者

主要你需要什麼樣的格式,把你要求貼出來呀,幾位數,怎麼不夠法.才可以用format函式進行相應的格式化

vb中,如何限制輸入的文字框的內容只能是數字數或者字母?

12樓:

這個需要在文字框的keypress事件中對輸入的keyascii進行檢查, 如果不滿足要求則輸入無效,例如只能輸入字母程式如下:

private sub text1_keypress(keyascii as integer)

select case keyascii

case 65 to 90,97 to 122 'a-z, a-z

case else

keyascii=0

end select

end sub

13樓:悠悠周郎

dim 輸入限制 as string = "0123456789.abcdefghijklmnopqrstuvwxyz"

dim 輸入字元 as char = e.keychar

if instr(輸入限制, 輸入字元) <> 0 or e.keychar = chrw(8) then

if 輸入字元 = "." and instr(textbox1.text, 輸入字元) <> 0 then

e.handled = true

else

e.handled = false

end if

end if

這段**寫在文字框的 keypress 事件裡,.net 的,6.0 好像類似,但沒有 e.

handled = true 或者 false,這裡為 true 時改成直接退出過程「exit sub」,false 不寫了好像行,沒有 6.0 沒法給你試試

14樓:學古箏

只能輸入數字

private sub form_resize()if not isnumeric(text1.text) then'如果輸入的不是數字,刪除非數字

text1.text = val(text1.text)text1.selstart = len(text1.text)end if

end sub

private sub text1_change()if not isnumeric(text1.text) then'如果輸入的不是數字,刪除非數字

text1.text = val(text1.text)text1.selstart = len(text1.text)end if

end sub

只能輸入整數

private sub form_resize()tmp = ""

for n = 1 to len(text1.text)if asc(mid(text1.text, n, 1)) > 47 and asc(mid(text1.

text, n, 1)) < 58 then

tmp = tmp & mid(text1.text, n, 1)end if

next

text1.text = val(tmp)text1.selstart = len(text1.text)end sub

private sub text1_change()tmp = ""

for n = 1 to len(text1.text)if asc(mid(text1.text, n, 1)) > 47 and asc(mid(text1.

text, n, 1)) < 58 then

tmp = tmp & mid(text1.text, n, 1)end if

next

text1.text = val(tmp)text1.selstart = len(text1.text)end sub

只輸入字母

private sub text1_change()'65 97 90 122

tmp = ""

for n = 1 to len(text1.text)if (asc(mid(text1.text, n, 1)) > 64 and asc(mid(text1.

text, n, 1)) < 91) or (asc(mid(text1.text, n, 1)) > 96 and asc(mid(text1.text, n, 1)) < 123) then

tmp = tmp & mid(text1.text, n, 1)end if

next

text1.text = tmp

text1.selstart = len(text1.text)end sub

asp中怎樣判斷文字框中是否輸入了空格

instr ddddd 如果反回值為0是沒有空格,如果大於0剛為有空格 可以用vbscript中的trim 函式 去掉空格用的 if 字串 trim 字串 then.end if if instr 字元bai 串du,0 or instr 字元zhi串,0 or instr 字串,chr 32 0 ...

asp中驗證文字框輸入字元個數的函式

一般asp中的驗證都是由js 實現的 哎身份證號碼按照什麼編排的你知道不就行了嗎?可是我也不知道身份證是按照什麼規律編的。反正我試過了,到身份證驗證的時候我隨便輸入就會顯示錯誤,說明他是有規律的。建議你去你當地的派出所問問,看看能不能告訴你暈!樓上的只是判斷了長度有什麼用啊?他說的是真假啊!靠你要把...

怎樣用VB做個編寫程式,在文字框中顯示當前系統時間,並隨時間進行變化

你新增一個timer控制元件 interval屬性 100 新增事件 private sub timer1 timer text1.text format now,hh mm ss end sub private sub form load timer1.enabled trueend sub pr...