Member Avatar for Micheal87

Hi all, I have created an Event handler to a function for clicking a generated button, now when this button is clicked need to paste in a textbox the Combobox.seleteditem (that it's a string. Each generated one (Combobox, textbox, button) will have a selected index of the Combobox that it's different, but that is not the problem because I put it in an if, the issues it's that I can't put inside this sub all the three-component. After all, it tells me that the windows form button cannot be a textbox, etc. Do I need to put another two event handlers for textbox and Combobox? (The code below it's a workaround, but I Like to do this with a button)

Public Sub btn_Click(sender As System.Object, ByVal e As System.EventArgs)
        'Dim btn As New Button
        'btn = CType(sender, Button)
        'Dim txt As New TextBox
        'txt = CType(sender, TextBox)
        Dim cbx As New ComboBox
        cbx = CType(sender, ComboBox)
        TextBox4.Text += cbx.SelectedItem.ToString & vbCrLf
    End Sub

Recommended Answers

All 2 Replies

You may do:

    Public Sub Btn_Click(sender As System.Object, ByVal e As System.EventArgs)
        Try
            Dim btn As Button
            Dim cbx As ComboBox
            Dim txt As TextBox
            Select Case sender.GetType
                Case GetType(Button)
                    btn = CType(sender, Button)
                   ' TODO ... '

                Case GetType(ComboBox)
                    cbx = CType(sender, ComboBox)
                   ' TODO ... '

                Case GetType(TextBox)
                    txt = CType(sender, TextBox)
                    ' TODO ... '

            End Select
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.