Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Accessing coded textboxes in vb2010

$
0
0
I have created a textbox array and am using the following code to create the textboxes:
Private tb(100) As TextBox
    Private btn(100) As Button
    Dim n As Integer = 0
    Dim intCount As Integer = 0
    Dim r As Integer = 228
    Dim c As Integer = 25
    Dim ProdName(100) As String
    Dim BotSize(100) As Integer
    Dim Optic(100) As Decimal
    Dim CostPerBottle(100) As Decimal
    Dim SellPriceInc(100) As Decimal
    Dim SellPriceEx(100) As Decimal
    Dim Measures(100) As Integer
    Dim MeasureCost(100) As Decimal
    Dim Profit(100) As Decimal
    Dim Target(100) As Decimal
    Dim DeptAvgGP As Decimal
    Dim GPAccumulator As Decimal

    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If tbEnterName.Text = "" Or
                   tbEnterBottleSize.Text = "" Or
                   tbEnterOptic.Text = "" Or
                   tbEnterCostPerBottle.Text = "" Or
            tbEnterSellPrice.Text = "" Then
            MessageBox.Show("All boxes must be completed")
            Exit Sub
        End If
        
        tbEnterName.Select()



        

     
        tb(n) = New TextBox
        tb(n).Name = "tbName" & n.ToString
        ProdName(n) = tbEnterName.Text
        tb(n).Text = ProdName(n)
        tb(n).Size = New Size(148, 20)
        tb(n).Location = New Point(25, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
        btn(n) = New Button
        btn(n).Name = "btnAmend" & n.ToString
        btn(n).Text = "Amend"
        btn(n).Size = New Size(50, 20)
        btn(n).Parent = Me
        btn(n).Location = New Point(425, r)
        AddHandler btn(n).Click, AddressOf Btn_Click
        Me.Controls.Add(btn(n))
    
        tb(n) = New TextBox
        tb(n).Name = "tbBotSize" & n.ToString
        BotSize(n) = tbEnterBottleSize.Text
        tb(n).Text = BotSize(n)
        tb(n).Size = New Size(50, 20)
        tb(n).Location = New Point(183, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))

      
        tb(n) = New TextBox
        tb(n).Name = "tbOptic" & n.ToString
        Optic(n) = tbEnterOptic.Text
        tb(n).Text = Optic(n)
        tb(n).Size = New Size(53, 20)
        tb(n).Location = New Point(240, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
      
        tb(n) = New TextBox
        tb(n).Name = "tbCostBottle" & n.ToString
        CostPerBottle(n) = tbEnterCostPerBottle.Text
        tb(n).Text = CostPerBottle(n).ToString("C")
        tb(n).Size = New Size(51, 20)
        tb(n).Location = New Point(299, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
        'Next i
        'For i = 1 To intCount
        tb(n) = New TextBox
        tb(n).Name = "tbSellInc" & n.ToString
        SellPriceInc(n) = tbEnterSellPrice.Text
        tb(n).Text = SellPriceInc(n).ToString("C")
        tb(n).Size = New Size(64, 20)
        tb(n).Location = New Point(356, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
    
        tb(n) = New TextBox
        tb(n).Name = "tbSellEx" & n.ToString
        SellPriceEx(n) = SellPriceInc(n) / (1 + (20 / 100))
        tb(n).Text = SellPriceEx(n).ToString("C")
        tb(n).Size = New Size(51, 20)
        tb(n).Location = New Point(576, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
    
        tb(n) = New TextBox
        tb(n).Name = "tbMeasures" & n.ToString
        Measures(n) = BotSize(n) / Optic(n)
        tb(n).Text = Measures(n)
        tb(n).Size = New Size(51, 20)
        tb(n).Location = New Point(633, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
    
        tb(n) = New TextBox
        tb(n).Name = "tbMeasureCost" & n.ToString
        MeasureCost(n) = CostPerBottle(n) / Measures(n)
        tb(n).Text = MeasureCost(n).ToString("C")
        tb(n).Size = New Size(54, 20)
        tb(n).Location = New Point(690, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
     
        tb(n) = New TextBox
        tb(n).Name = "tbProfit" & n.ToString
        Profit(n) = SellPriceEx(n) - MeasureCost(n)
        tb(n).Text = Profit(n).ToString("#.00")
        tb(n).Size = New Size(42, 20)
        tb(n).Location = New Point(750, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))
     
        tb(n) = New TextBox
        tb(n).Name = "tbTarget" & n.ToString
        Target(n) = 100 - ((MeasureCost(n) / SellPriceEx(n)) * 100)
        tb(n).Text = Target(n).ToString("0.##")
        tb(n).Size = New Size(50, 20)
        tb(n).Location = New Point(798, r)
        tb(n).BorderStyle = BorderStyle.Fixed3D
        Me.Controls.Add(tb(n))

      
        GPAccumulator += Target(n)
        DeptAvgGP = GPAccumulator / intCount
        DepAvgGp.Text = DeptAvgGP.ToString("0.##")
        r += 24

        tbEnterName.Text = ""
        tbEnterBottleSize.Text = ""
        tbEnterCostPerBottle.Text = ""
        tbEnterOptic.Text = ""
        tbEnterSellPrice.Text = ""
        tbEnterName.Select()
        n = n + 1
        intCount += 1
        TextBox10.Text = n
        TextBox11.Text = intCount
        TextBox12.Text = r

    End Sub


This all works fine; however, I need to code the "amend" button so that when it is pressed it takes the information from that particular row and places it back into the entry boxes:

Private Sub Btn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim PressedButton As Button = CType(sender, Button)
        Dim T As String = PressedButton.Text

        PressedButton.Text = PressedButton.Name
        
        n = 1

        For i = 1 To intCount

            If PressedButton.Name = "btnAmend" & (n) Then
                tbEnterName.Text = ProdName(n)
                tbEnterBottleSize.Text = BotSize(n)
                tbEnterOptic.Text = Optic(n)
                tbEnterCostPerBottle.Text = CostPerBottle(n)
                tbEnterSellPrice.Text = SellPriceInc(n)
                
                btnAdd.Visible = False
                btnAmend.Visible = True
            Else
                n = n + 1

            End If

            
        Next i
        
        r = 228 + (n * 24)
        TextBox10.Text = n
        TextBox11.Text = intCount
        TextBox12.Text = r
        PressedButton.Text = T
        
        

    End Sub


This also works; but when changes are made to the entry boxes I can't get those changes to appear in the appropriate coded textbox. Also If I try to delete the coded textboxes that I've created using

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                Me.Controls.Remove(ctrl)
                ctrl = Nothing
            End If
        Next

    End Sub

there doesn't appear to be a way of removing specific text boxes. This code removes all text boxes including those that were created normally at design time.

Not sure where to go from here.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>