将按钮的文本输入文本框

时间:2012-07-27 15:31:41

标签: vb.net button textbox

继我上一期的问题Buttons to be renamed by the user之后,该问题迅速得到了解答并得到了很多帮助。

目前我已经获得了这个代码,我需要在100个按钮上使用。

我需要知道的是如何使用按钮中的文本放入需要它们的不同文本框中。

    If Label4.Text = "Admin" Then
        With DirectCast(sender, Button)
            .Text = InputBox("Button Name", "Button Name", .Text)
        End With
    Else
        Me.TransactionBindingSource.AddNew()

        Product_NameTextBox.Text = >>>>>>Button name<<<<<<<<
        Try
            Me.ProductTableAdapter.FillByProductName(Me.Database1DataSet.Product, Product_NameTextBox.Text)
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try

        ProductTextBox.Text = >>>>>>>Button2.Text<<<<<<<<
        GroupTextBox.Text = GroupTextBox1.Text
        AmountTextBox.Text = AmountTextBox1.Text
        PriceTextBox.Text = PriceTextBox1.Text
        TimeTextBox.Text = TimeOfDay
        DateTextBox.Text = DateString

        Me.Validate()
        Me.TransactionBindingSource.EndEdit()
        Me.TransactionTableAdapter.Update(Me.Database1DataSet)

        Timer2.Enabled = True
        TransNameLB.Items.Add(>>>>>>>Button2.Text<<<<<<<<)
        TransPriceLB.Items.Add(PriceTextBox.Text)

        Dim sum As Double
        For x As Integer = 0 To TransPriceLB.Items.Count - 1
            sum += Val(TransPriceLB.Items.Item(x).ToString)
        Next
        TextBox1.Text = sum.ToString

        QTYDrinksTB.Text = TransNameLB.Items.Count
    End If

End Sub

除了>>> <<<之间的位置之外,所有内容都有效,我需要点击该按钮并将按钮中的文本返回到文本框/列表框中。

我有100个需要此代码的按钮。

1 个答案:

答案 0 :(得分:1)

您使用上一个问题中的With DirectCast(sender, Button),该问题也可用于帮助您处理其他部分。例如:

If Label4.Text = "Admin" Then
    With DirectCast(sender, Button)
        .Text = InputBox("Button Name", "Button Name", .Text)
    End With

Else
    Me.TransactionBindingSource.AddNew()

    With DirectCast(sender, Button)

    Product_NameTextBox.Text = .Text

    Try
        Me.ProductTableAdapter.FillByProductName(Me.Database1DataSet.Product, Product_NameTextBox.Text)
    Catch ex As System.Exception
        System.Windows.Forms.MessageBox.Show(ex.Message)
    End Try

    ProductTextBox.Text = .Text
    GroupTextBox.Text = GroupTextBox1.Text
    AmountTextBox.Text = AmountTextBox1.Text
    PriceTextBox.Text = PriceTextBox1.Text
    TimeTextBox.Text = TimeOfDay
    DateTextBox.Text = DateString

    Me.Validate()
    Me.TransactionBindingSource.EndEdit()
    Me.TransactionTableAdapter.Update(Me.Database1DataSet)

    Timer2.Enabled = True
    TransNameLB.Items.Add(.Text)
    TransPriceLB.Items.Add(PriceTextBox.Text)

    End With

    Dim sum As Double
    For x As Integer = 0 To TransPriceLB.Items.Count - 1
        sum += Val(TransPriceLB.Items.Item(x).ToString)
    Next
    TextBox1.Text = sum.ToString

    QTYDrinksTB.Text = TransNameLB.Items.Count
End If