【问题标题】:Change text of selected textbox on button click单击按钮时更改所选文本框的文本
【发布时间】:2013-10-10 08:10:18
【问题描述】:

我遇到了一个问题,涉及在按钮单击事件中更改表单上当前选定文本框的文本。这可能吗?如果是这样,我该怎么做?

编辑:我不知道文本框的名称,但它始终是当前选定或“聚焦”的文本框。

【问题讨论】:

    标签: windows vb.net winforms button textbox


    【解决方案1】:

    主要问题是当您按下按钮时,文本框不再是焦点。 通过解决方案出现here,您可以这样做:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If lastTextBoxFocused IsNot Nothing Then
            lastTextBoxFocused.Text = "Bla bla, bla!"
        End If
    End Sub
    
    Dim lastTextBoxFocused As TextBox
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'find all TextBox's in the Form.
        For Each Ctrl In Me.Controls
            If TypeOf Ctrl Is TextBox Then
                'attach a lambda expression to each Enter event, to "remember" the last enter
                AddHandler CType(Ctrl, TextBox).Enter, Sub(o, ev) lastTextBoxFocused = o
            End If
        Next
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2011-06-18
      • 1970-01-01
      • 2017-10-03
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 2018-08-08
      • 2019-07-08
      • 2022-01-08
      相关资源
      最近更新 更多