my not getting the values from the textbox when i pressed the button please see the code and give me a solution please..

i want my textbox value to be displayed in the msgbox with textbox.name and button.name

Public Class Form1


    Dim txtadd1 As New TextBox()
    Dim txtadd2 As New TextBox()
    Dim Buttonadd5 As New Button()



    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

                Dim Paneladd As New Panel
                Me.Controls.Add(Paneladd)

                For i = 1 To 2

                    Paneladd.Top = 0
                    Paneladd.Height = 25
                    Paneladd.Dock = DockStyle.Top

                    txtadd1 = New TextBox
                    txtadd1.Name = "txtadd1" + (i).ToString
                    txtadd1.Text = "txtadd1" + (i).ToString
                    txtadd1.Left = 0
                    txtadd1.Size = New System.Drawing.Size(100, 10)
                    txtadd1.Enabled = False

                    txtadd2 = New TextBox
                    txtadd2.Name = "txtadd2" + (i).ToString
                    txtadd2.Text = "txtadd2" + (i).ToString
                    txtadd2.Left = 100
                    txtadd2.Size = New System.Drawing.Size(100, 10)
                    txtadd2.Enabled = False

                    Buttonadd5 = New Button
                    Buttonadd5.Name = "Buttonadd" + (i).ToString
                    Buttonadd5.Text = dr.GetValue(0).ToString()
                    Buttonadd5.Left = 500
                    Buttonadd5.TextAlign = HorizontalAlignment.Center

               Next

                Panel3.Controls.Add(Paneladd)
                Paneladd.Controls.Add(txtadd1)
                Paneladd.Controls.Add(txtadd2)
                Paneladd.Controls.Add(Buttonadd5)

         End Sub
end class

You need to create an action lister (event handler) for the button click.

For example:

Private Sub Button_Click(byval sender as Object, byval e As Eventargs)
    Try
         'Place your code here to find the textbox
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

Add the handler to the button before adding it to the form.

AddHandler Buttonadd5.click, AddressOf Button_Click

This will fire an event handler for when user presses the button.

i dont have a clue about the code in the try section ? please get me through with this ...

Hey Shan, bdev's trying to tell you to fill the Try-Catch with "What you want", for example:

Try
    Dim a as String = ""
    a = txtadd1.Name
    MsgBox(a.ToString)
    '... then do something you want here
Catch ex As Exception
    MsgBox(ex.ToString)
End Try
commented: Trying to force them to think :) +8

You can try something like this:

Private Sub Button_Click(byval sender as Object, byval e As Eventargs)
    Try
    For Each t As TextBox In Me.Controls
        If t.Name ="txtAdd1" or "txtAdd2" Then
            MsgBox("TextBox Value:" & t.Text & vbCrLF & "TextBox Name:" & t.Name & vbCrLf & "Button Name:" & Cstr(TryCast(sender,Button).Name))
        End If
    Next
    Catch ex As Exception
        MsgBox(ex.ToString)
    End Try
End Sub

This will find either of the textboxes, get the values, and the name of the button that you pressed.

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.