如果那个控件本来就是焦点,再用setfocus就会出错

解决方案 »

  1.   

    Private Sub Command1_Click()
        Command1.SetFocus
    End SubPrivate Sub Text1_GotFocus()
        Text1.SetFocus
    End Sub没问题的,你看上面的,它本来就有焦点,但是再用SetFocus,还是没有任何问题的如果你想知道焦点在哪个空件上面
    Me.ActiveControl,就可以告诉你哪个空间有焦点了if Me.ActiveControl.name="你想知道的空件名称" then
        debug.print "你这个空件已经有焦点了"
    end if
      

  2.   


    哦,这样实现就可以了  
        MsgBox TypeName(Me.ActiveControl)
    放在定时器里试试,应该可以的
      

  3.   

    你真是没有法子说了
    在控件的getfocts中记录,在lostfoctus中去掉记录不就行了
      

  4.   

    用API函数:GetActiveWindow()得到当前焦点的控件hwnd,再列举窗体里的控件,检验其hwnd
      

  5.   

    写个函数:Private Sub Form_Click()
    MsgBox ctlinfocus(Text1)
    End Sub
    Function ctlinfocus(ByVal ctl As Control) As Boolean
    ctlinfocus = False
    If Me.ActiveControl.Name = ctl.Name Then ctlinfocus = True
    End Function
      

  6.   

    "如果那个控件本来就是焦点,再用setfocus就会出错"是对的,我曾在一本书上看过,经试验也确实如此. 上面几位仁兄提出的解决方案也是可行的.
      

  7.   

    但是我刚试过,没出现像楼上说得那样!没有出错程序如下!:
    Option ExplicitPrivate Sub Command1_Click()
    Text1.SetFocus
    Text1.SetFocus
    Text1.SetFocus
    Text1.SetFocus
    Text1.SetFocus
    End SubPrivate Sub Command2_Click()
       Command1.SetFocus
    End Sub
      

  8.   

    msgbox me.activecontrl.name,vbokonly,"attention"
      

  9.   

    你的控件有没有被设为无效值(Enabled=false) 或者它的Visible属性被设为False了,如果是,就会出现楼主说的问题!!!
      

  10.   

    对于一般控件:
    if ActiveControl.hWnd=yourcontrol.hWnd then
        msgbox yourcontrol.name
    endif对于没有句柄的控件,还是:
    if ActiveControl.Name=yourcontrol.Name _
       and ActiveControl.index=yourcontrol.index  then
        msgbox yourcontrol.name
    endif另外,焦点只能移到可视的,并且Enabled 属性为 False 的窗体或控件。