Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event handling for Dynamically created Controls

Status
Not open for further replies.

slowNsteady

Programmer
May 29, 2009
22
0
0
US
Hi

I have a problem in handling the events for dynamically created controls

I am creating a combobox and a textbox dynamically when i click a Button1

for each combobox there is one corresponding textbox

so the number of times i click Button1 i will get that many number of comboboxes and textboxes.

i have added an Event handler to handle the ComboBox_SelectedIndexChanged

Whenever i click any of the combobox the above event is triggered.

So far it is working fine.

Now comes the problem

i have to get the selected item for each combobox in the appropriate textbox

i am not getting it

here is my code

Code:
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        combobox1 = New ComboBox
        textbox1 = New TextBox
        combobox1.Name = "combobox" & i
        textbox1.Name = "textbox" & i
        i = i + 1
        combobox1.Location = New Point(31, 32 + x)
        textbox1.Location = New Point(165, 32 + x)
        x = x + 40
        combobox1.Size = New Size(121, 21)
        textbox1.Size = New Size(100, 21)
        AddHandler combobox1.SelectedIndexChanged, AddressOf Me.ComboBox_SelectedIndexChanged
        'AddHandler textbox1.TextChanged, AddressOf Me.TextBox1_TextChanged
        Me.Controls.Add(combobox1)
        Me.Controls.Add(textbox1)
        combobox1.Items.Add("1")
        combobox1.Items.Add("2")
        combobox1.Items.Add("3")
        combobox1.Items.Add("4")
        combobox1.Items.Add("5")
        combobox1.Items.Add("6")
    End Sub

 Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If TypeOf sender Is ComboBox Then
            Dim cb As ComboBox = DirectCast(sender, ComboBox)
            textbox1.Text = cb.SelectedItem
        End If
    End Sub

Thanks in advance

Sri
 
I don't understand. Is your issue finding out which object triggered the event? If so, look at the sender parameter. For example:

Code:
If sender Is ComboBox1 Then
  Messagebox.Show("ComboBox1")
End If

or

Code:
Dim cbo As ComboBox = CType(sender, ComboBox)
MessageBox.Show(cbo.Text)
 
no that is not the problem

if i click item 4 in combobox3 then i should get the same in the textBox3 in the form

if i click item 2 in combobox5 then i should get the same in the textBox5 in the form

sri
 
If still i am not explaining it properly Please copy this code and run it and follow the steps below .you can see the problem

click button1 for three times continuosly

you will see three comboboxes and three textboxes in the form

now go and select any item from first Combobox.
You should get the selected item in the first textbox
but you will not get it. instead you will get it in last textbox

now go and select any item from second Combobox.
You should get the selected item in the second textbox
but you will not get it .instead you will get it in last textbox

it only works for recently appeared textbox

thats what i am trying to do in my code.

hope this will help you to understand the problem

sri





 
You're saying that you want the value selected in ComboBox1 to be displayed in TextBox1, the value selected in ComboBox2 to be displayed in TextBox2?

Really, just write a generic routine to handle it if you have a static, small number of controls. For example, in your event handler, call code like this:
Code:
Me.TextBox1.Text = Me.ComboBox1.Text
Me.TextBox2.Text = Me.ComboBox2.Text
Me.TextBox3.Text = Me.ComboBox3.Text

If you generate the controls at runtime and the number of them may vary, I assume you're setting the .Name property. If so, you can do the following:
Code:
For i As Integer = 1 To SomeNumberThatSaysHowManyControlsIHave
  Me.Controls("TextBox" & i.ToString).Text = Me.Controls("ComboBox" & i.ToString).Text
Next
 
for example i told you 3 times
but in real they can hit as many as times they need

anyways thanks your help. i got the solution.
here is the code
Code:
Dim controlCounter As Integer = 0
Dim i As Integer = 0
Dim x As Integer = 0
Dim lstCombos As New List(Of ComboBox)
Dim lstTextBoxes As New List(Of TextBox)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        combobox1 = New ComboBox
        textbox1 = New TextBox
        combobox1.Name = "combobox" & i
        textbox1.Name = "textbox" & i
        i = i + 1
        combobox1.Location = New Point(31, 32 + x)
        textbox1.Location = New Point(165, 32 + x)
        x = x + 40
        combobox1.Size = New Size(121, 21)
        textbox1.Size = New Size(100, 21)
        AddHandler combobox1.SelectedIndexChanged, AddressOf Me.ComboBox_SelectedIndexChanged
        Me.Controls.Add(combobox1)
        Me.Controls.Add(textbox1)
        combobox1.Items.Add("1")
        combobox1.Items.Add("2")
        combobox1.Items.Add("3")
        combobox1.Items.Add("4")
        combobox1.Items.Add("5")
        combobox1.Items.Add("6")
        lstTextBoxes.Add(textbox1)
        combobox1.Tag = controlCounter
        lstCombos.Add(combobox1)
        controlCounter += 1
    End Sub

    Private Sub ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If TypeOf sender Is ComboBox Then
            Dim cb As ComboBox = DirectCast(sender, ComboBox)
            Dim txtbox As TextBox = lstTextBoxes(Integer.Parse(cb.Tag.ToString))
            txtbox.Text = cb.SelectedItem
        End If
    End Sub

thanks
Sri
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top