Problem in using control arrays in VB.NET

Asked By 0 points N/A Posted on -
qa-featured

I'm having a problem using arrays in VB.NET. 

Here's the thing:

This is the initial design of the form during design time. What I want to do is repeatedly create a combo box and a text box for every row in list view.

Here's how it looks like at runtime; since there are two items in list view, there will be two combo boxes and two text boxes:

Now here's where I lost it: the text of the text box will depend on the item chosen from the combo box. Say, if  Good  is selected from the combo box, $10 should be placed in the text box. What happens is that when I choose Good from the second combo box, the $10 still appears in the first textbox. This, I think, is where I lost it. The $10 appears on the  first textbox because I wrote TextBox1.Text = $10; what should I write to call the second text box(created dynamically in runtime)?

 

        If cbo.Text = "Good" Then
            TextBox1.Text = "$10"
        End If
 
Here's the whole code, take a look:
Public comboarr() As ComboBox, textarr() As TextBox
    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MS_ListviewSetting(ListView1)
        ListView1.Columns.Add("Name")
        ListView1.Columns.Add("Contact")
        Dim lvitem As New ListViewItem
        lvitem.Text = "AA"
        lvitem.SubItems.Add("aa")
 
        Dim lvitem1 As New ListViewItem
        lvitem1.Text = "BB"
        lvitem1.SubItems.Add("bb")
        ListView1.Items.Add(lvitem)
        ListView1.Items.Add(lvitem1)
        ReDim comboarr(ListView1.Items.Count)
        ReDim textarr(ListView1.Items.Count)
        comboarr(0) = ComboBox1
        textarr(0) = TextBox1
        loadData()
        ComboBox1.Items.Add("Good")
        ComboBox1.Items.Add("Lost")
        ComboBox1.Items.Add("Damaged")
    End Sub
    Sub loadData()
        For i = 1 To ListView1.Items.Count – 1
            Dim btn As New ComboBox()
            comboarr(i) = btn
            btn.Location = New System.Drawing.Point(comboarr(i – 1).Location.X, comboarr(i – 1).Location.Y + 20)
            btn.Size = New System.Drawing.Size(ComboBox1.Size)
            btn.DropDownStyle = ComboBoxStyle.DropDownList
            btn.Items.Add("Good")
            btn.Items.Add("Lost")
            btn.Items.Add("Damaged")
            btn.SelectedIndex = 0
            Me.Controls.Add(btn)
            AddHandler btn.Click, AddressOf Me.ClickButton
 
            Dim lbl As New TextBox()
            textarr(i) = TextBox1
            lbl.Location = New System.Drawing.Point(textarr(i – 1).Location.X, textarr(i – 1).Location.Y + 20)
            lbl.Size = New System.Drawing.Size(TextBox1.Size)
            Me.Controls.Add(lbl)
        Next
    End Sub
    Private Sub ClickButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.Click
        Dim cbo As ComboBox
        cbo = CType(sender, ComboBox)
        '   MsgBox(btn.Text)
        If cbo.Text = "Good" Then
            TextBox1.Text = "$10"
        End If
    End Sub
SHARE
Answered By 0 points N/A #125135

Problem in using control arrays in VB.NET

qa-featured

Hi There,

Here is a solution for it:

Please understand the logic; it's more important

I have done some changes to your code:

1.Textbox is named to txt1 and combo box is named to cbo1 (for ease of reference)

2. The code is changed appears in bold.

Please check this out and if this works, you can improve more on the logic.

Thanks

Dipendra Shah

The changed code is :

Public comboarr() As ComboBox, textarr() As TextBox

    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MS_ListviewSetting(ListView1)
        ListView1.Columns.Add("Name")
        ListView1.Columns.Add("Contact")
        Dim lvitem As New ListViewItem
        lvitem.Text = "AA"
        lvitem.SubItems.Add("aa")

        Dim lvitem1 As New ListViewItem
        lvitem1.Text = "BB"
        lvitem1.SubItems.Add("bb")
        ListView1.Items.Add(lvitem)
        ListView1.Items.Add(lvitem1)
        ReDim comboarr(ListView1.Items.Count)
        ReDim textarr(ListView1.Items.Count)
        comboarr(0) = cbo1
        textarr(0) = txt1
        loadData()
        cbo1.Items.Add("Good")
        cbo1.Items.Add("Lost")
        cbo1.Items.Add("Damaged")
    End Sub
    Sub loadData()
        For i = 1 To ListView1.Items.Count – 1
            Dim btn As New ComboBox()

            comboarr(i) = btn
            btn.Location = New System.Drawing.Point(comboarr(i – 1).Location.X, comboarr(i – 1).Location.Y + 20)
            btn.Size = New System.Drawing.Size(cbo1.Size)
            btn.DropDownStyle = ComboBoxStyle.DropDownList
            btn.Items.Add("Good")
            btn.Items.Add("Lost")
            btn.Items.Add("Damaged")
            btn.SelectedIndex = 0
            btn.Name = "cbo" + (i + 1).ToString()
            Me.Controls.Add(btn)
            AddHandler btn.SelectedIndexChanged, AddressOf Me.ClickButton

            Dim lbl As New TextBox()
            textarr(i) = txt1
            lbl.Name = "txt" + (i + 1).ToString()
            lbl.Location = New System.Drawing.Point(textarr(i – 1).Location.X, textarr(i – 1).Location.Y + 20)
            lbl.Size = New System.Drawing.Size(txt1.Size)
            Me.Controls.Add(lbl)
        Next
    End Sub
    Private Sub ClickButton(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo1.SelectedIndexChanged
        Dim cbo As ComboBox
        cbo = CType(sender, ComboBox)
        '   MsgBox(btn.Text)
     
        Dim intArrIndex As String
        intArrIndex = cbo.Name.Substring(3)

        Dim objTXT As New TextBox
        For Each ctl As Control In Me.Controls
            If (Not TypeOf ctl Is TextBox) Then Continue For
            If (ctl.Name.Equals("txt" & intArrIndex)) Then
                objTXT = DirectCast(ctl, TextBox)

            End If
        Next ctl

        If cbo.Text = "Good" Then
            objTXT.Text = "$10"
        ElseIf cbo.Text = "Lost" Then
            objTXT.Text = "$20"
        Else
            objTXT.Text = "$30"
        End If
    End Sub

Login/Register to Answer

Related Questions