您的位置:程序门 -> vb -> 控件



请教:如何在combobox中只能输入两位数字?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


请教:如何在combobox中只能输入两位数字?[已结贴,结贴人:supernicksun]
发表于:2007-01-12 09:03:21 楼主
我想在combobox中只能输入00至23,例如当输入3时就不能输入第2个数字了,
应该如何实现?
(combobox初始化时已经被添加了00至23共24个项目,可选择也可输入)
发表于:2007-01-12 09:29:401楼 得分:20
在combobox的keypress事件中加如下代码:

if   keyascii <asc( "0 ")   or   keyascii> asc( "9 ")   then   keyascii=0

发表于:2007-01-12 09:32:542楼 得分:10
在combobox的keypress事件中加如下代码:

if   keyascii <asc( "0 ")   or   keyascii> asc( "9 ")   then   keyascii=0:exit   sub
if   len(combobox.text)=2   then   keyascii=0
发表于:2007-01-12 09:34:053楼 得分:0
sty..=2
发表于:2007-01-12 10:40:364楼 得分:40
private   sub   combo1_keypress(keyascii   as   integer)
        dim   i   as   long
        dim   str   as   string
        if   keyascii   > =   &h30   and   keyascii   <=   &h39   then
                combo1.seltext   =   " "
               
                str   =   combo1.text   &   chr(keyascii)
                if   len(str)   >   2   then
                        keyascii   =   0
                end   if
               
                str   =   left(str   &   "* ",   2)
                for   i   =   0   to   combo1.listcount   -   1
                        if   combo1.list(i)   like   str   then
                                exit   for
                        end   if
                next
               
                if   i   > =   combo1.listcount   then
                        keyascii   =   0
                end   if
        end   if
end   sub

private   sub   form_load()
        dim   i   as   long
        for   i   =   0   to   23
              combo1.additem   format(i,   "00 ")
        next
end   sub
发表于:2007-01-12 10:46:365楼 得分:0
稍微改了一点
private   sub   combo1_keypress(keyascii   as   integer)
        dim   i   as   long
        dim   str   as   string
        if   keyascii   > =   &h30   and   keyascii   <=   &h39   then
                combo1.seltext   =   " "
               
                if   combo1.selstart   >   0   then
                        str   =   combo1.text   &   chr(keyascii)
                else
                        str   =   chr(keyascii)   &   combo1.text
                end   if
               
                if   len(str)   >   2   then
                        keyascii   =   0
                end   if
               
                str   =   left(str   &   "* ",   2)
                for   i   =   0   to   combo1.listcount   -   1
                        if   combo1.list(i)   like   str   then
                                exit   for
                        end   if
                next
               
                if   i   > =   combo1.listcount   then
                        keyascii   =   0
                end   if
        end   if
end   sub
发表于:2007-01-12 19:56:576楼 得分:20
private   sub   cbo_keypress(keyascii   as   integer)
        if   keyascii   > =   48   and   keyascii   <=   57   then     '判断输入的是否是数字
                if   cbo.sellength   =   0   then     '判断是否有选中的字符
                        if   len(cbo.text)   > =   2   then     '判断组合框中的内容是否已是两位数(如果不写这句会输入多个0)
                                keyascii   =   0
                        else
                                if   cint(cbo.text   &   chr(keyascii))   >   23   then   keyascii   =   0     '判断输入后数字是否已大于23
                        end   if
                end   if
        elseif   not   (keyascii   =   8)   then     '判断输入的   不是   退格键(此判断不能少)
                keyascii   =   0
        end   if
end   sub
发表于:2007-01-14 00:07:497楼 得分:10
if   val(combo1.text)> 23   then   combo1.text= "23 "
发表于:2007-01-14 00:08:178楼 得分:0
if   val(combo1.text)=0   then   combo1.text= "00 "


快速检索

最新资讯
热门点击