Craig Emerson
Craig Emerson

Reputation: 27

Input a button's text into a text box

Following on from my last Question Buttons to be renamed by the user which was answered quickly and helped a lot.

At the moment I have got this code, which I need to have on 100's of buttons.

What I need to know is how to use the text from the button to put into the different text boxes that requires them.

    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

Everything works apart from the bits between >>> <<<, where I need to get the button that is clicked and return the text from the button into the text boxes/list box.

I have 100's of buttons that need this code.

Upvotes: 1

Views: 3109

Answers (1)

Gaffi
Gaffi

Reputation: 4367

You use With DirectCast(sender, Button) from your previous question, which can also be used to help with your other parts. For example:

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

Upvotes: 1

Related Questions