1

我在表格中有一个标签。在表单加载时,我逐行从文本文件中获取文本,并将它们显示为表单 Tabcontrol Tabpage 上的标签,并在它们旁边动态显示按钮。现在在这些按钮上单击我想复制相关标签中的文本。谁能建议在Nextbtn_Click活动中放什么?

Dim FILE_NAME As String = "D:\1.txt"    
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim i As Integer = 1
    For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
        Dim NextLabel As New Label
        Dim Nextbtn As New Button
        NextLabel.Text = line
        Nextbtn.Text = "Copy"
        NextLabel.Height = 22
        Nextbtn.Width = 55
        Nextbtn.Height = 22
        NextLabel.BackColor = Color.Yellow
        TabPage2.Controls.Add(NextLabel)
        TabPage2.Controls.Add(Nextbtn)
        NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
        Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
        AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
        i += 1
    Next
End Sub

Private Sub Nextbtn_Click(sender As Object, e As EventArgs)

End Sub
4

2 回答 2

1

存储组件。标签属性中的标签,您可以在单击按钮时将其转换回来。sender 对象是当前单击的按钮。

Dim FILE_NAME As String = "D:\1.txt"    
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim i As Integer = 1
    For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
        Dim NextLabel As New Label
        Dim Nextbtn As New Button
        Nextbtn.Tag = NextLabel
        NextLabel.Text = line
        Nextbtn.Text = "Copy"
        NextLabel.Height = 22
        Nextbtn.Width = 55
        Nextbtn.Height = 22
        NextLabel.BackColor = Color.Yellow
        TabPage2.Controls.Add(NextLabel)
        TabPage2.Controls.Add(Nextbtn)
        NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
        Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
        AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
        i += 1
    Next
End Sub

Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
    Dim s As String = DirectCast(DirectCast(sender, Button).Tag, Label).Text
End Sub
于 2013-09-14T17:33:28.477 回答
0
Private Sub Clicked(ByVal sender As Object, ByVal e As EventArgs)
    Dim b As Button = DirectCast(sender, Button)
    TextBox2.Text = b.Name
    Clipboard.SetText(b.Name)

End Sub
于 2016-04-07T20:46:26.107 回答