回 帖 发 新 帖 刷新版面

主题:如何判断Textbox是否获取了焦点

运行环境:winXP
开发软件:Microsoft Visual Studio .NET 2003
开发语言:VB.NET
问题:往窗体上拖放Textbox1、Textbox2和Button1,Textbox1.text和Textbox2.text
      为空,Button.text为0。如何编一段代码当Textbox1获取焦点时,单击
      Button1,Textbox1.text赋值为1。当Textbox2获取焦点时,单击Button1,
      Textbox2.text赋值为1。希望高手能提供正确代码。在此先谢过!!

回复列表 (共5个回复)

沙发

        private void button1_Click(object sender, System.EventArgs e)
        {
            if(textBox1.Focused)
            {
                textBox1.Text = "1";
            }
            if(textBox2.Focused)
            {
                textBox2.Text = "1";
            }
        }

板凳

好像用VB.NET中的一个控件也可以的

3 楼

好象在2005中用不了focused,当点按钮时,焦点到按钮上了

4 楼

Dim GotFocusTextboxName As String
    Private Sub ChangeButtonText(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus, TextBox1.LostFocus
        GotFocusTextboxName = sender.name
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case LCase(GotFocusTextboxName)
            Case LCase("Textbox1")
                Button1.Text = "1"
            Case LCase("Textbox2")
                Button1.Text = "1"
        End Select
    End Sub

    Private Sub Button1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.LostFocus
        GotFocusTextboxName = ""
    End Sub

5 楼


你在北京工作吗?
可以加你MSN吗

我来回复

您尚未登录,请登录后再回复。点此登录或注册