Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please help me the with the multiple created button the get simultaneously value of the multiple created textbox.text

VB
Public Class Form1


    Dim txtadd1 As New TextBox()
    Dim txtadd2 As New TextBox()
    Dim txtadd3 As New TextBox()
    Dim txtadd4 As New TextBox()
    Dim txtadd5 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 = 0 To 10

                    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.Size = New System.Drawing.Size(100, 10)
                    Buttonadd5.TextAlign = HorizontalAlignment.Center

                                   Next
                Panel3.Controls.Add(Paneladd)
                Paneladd.Controls.Add(txtadd1)
                Paneladd.Controls.Add(txtadd2)
                Paneladd.Controls.Add(txtadd3)
                Paneladd.Controls.Add(txtadd4)
                Paneladd.Controls.Add(txtadd5)
                Paneladd.Controls.Add(Buttonadd5)
                   
         End Sub
Posted
Comments
[no name] 26-Jun-13 12:43pm    
What?
Shan Shan 26-Jun-13 13:51pm    
i have sucessfully created Dynamic (Multiple) textbox and given the textbox.name and i like to get the values(textbox.name) of each textbox using Dynamic (Multiple) buttons to show the textbox.name in a messagebox

please help me.

1 solution

If what you are after is "press a dynamically created button and access the related dynamically created Textboxes in the event handler" then it's pretty simple: just use the Tag property:
VB
Private Sub AddControls()
    ...
    Dim panellAdd As New Panel()
    For i As Integer = 1 To 9
        Dim tb1 As New TextBox()
        Dim tb2 As New TextBox()
        Dim b As New Button()
        b.Tag = tb1
        tb1.Tag = tb2
        b.Click += New EventHandler(AddressOf b_Click)
        ...
        panellAdd.Controls.Add(tb1)
        panellAdd.Controls.Add(tb2)
        panellAdd.Controls.Add(b)
    Next
    ...
End Sub

Private Sub b_Click(sender As Object, e As EventArgs)
    Dim b As Button = TryCast(sender, Button)
    If b IsNot Nothing Then
        Dim tb1 As TextBox = TryCast(b.Tag, TextBox)
        Dim tb2 As TextBox = TryCast(tb1.Tag, TextBox)
        ...
    End If
End Sub



"Error 'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
b.Click += New EventHandler(AddressOf b_Click)

im getting an error"


Sorry - I wrote it in C# and used an automatic translator - and forgot to check that bit :doh:
Replace the line with:
VB
AddHandler b.Click, AddressOf b_Click
 
Share this answer
 
v2
Comments
Shan Shan 26-Jun-13 14:26pm    
b.Click += New EventHandler(AddressOf b_Click)

Error 'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

im getting an error
OriginalGriff 26-Jun-13 14:36pm    
Answer updated.
Shan Shan 26-Jun-13 15:33pm    
b.Tag = tb1
tb1.Tag = tb2

i dont get this part, can you please explain or any correction is required
and
nothing is displayed... can you please check
OriginalGriff 27-Jun-13 3:19am    
All it's doing is storing the reference to the textboxes in the related control.

Each Button.Tag property contains the reference to the first related TextBox.
The First TextBox.Tag property contains a reference to the second TextBox.

This way, if you have the Button (which you get handed to you when the event handler is called) then you can get the relevant TextBoxes without having to know any further information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900