# Visual Basic > Visual Basic .NET > VS 2022 code to insert textbox

## surbek

Hi

I am learning visual basic by myself and I am making an app which will have let's say 50 rows with 3 textboxes. in each row the first textbox will have number of product, second textbox in row price and third textbox in row will have multiplication of first two. on the end i have a textbox with sum of multiplication textboxes.
sorry for my English, I hope you will understand
I have a few questions

1. how do i get that sum? do i have to write all the textboxes names (textbox_sum.text= textbox3 + textbox9 +....+textbox 30) or is there a faster way

2. if one day I will need more then 50 rows of textboxes, can I put two buttons one for adding rows with textboxes and another for deleting rows and so that my textbox with sum would automaticaly add the new textboxes. should I maybe copy and paste rows with textboxes?

thanks

----------


## surbek

I have done the code for sum, is this ok?



```
Dim d As Single
        Dim ctl1 As Control
        d = "0"
        tb_suma.Text = "0"

        For Each ctl1 In Me.Controls
            If TypeOf ctl1 Is TextBox And (ctl1.Name.Substring((ctl1.Name.Length) - 5)) = "iznos" Then
                d = ctl1.Text
                tb_suma.Text = tb_suma.Text + d
            End If
        Next
```

----------


## jmcilhinney

Firstly, I would suggest that you ought to use a DataGridView rather than individual TextBoxes, if you have the option. You could store the data in a DataTable and bind it to the grid. The DataTable could automatically do the multiplications for you and also provide the sum on demand.

----------


## jmcilhinney

Assuming that you must use TextBoxes, I would first suggest that you add a TableLayoutPanel to the form and add your TextBoxes to that. It will handle the layout for you, plus act as a container. You can then access each TextBox in a specific column using the column index, e.g.

vb.net Code:
Dim columnIndex = 2
Dim sum = 0
 For rowIndex = 0 To TableLayoutPanel1.RowCount - 1
    Dim tb = DirectCast(TableLayoutPanel1.GetControlFromPosition(columnIndex, rowIndex), TextBox)
     sum += CInt(tb.Text)
Next
 'Use sum here.
For the record, you could use LINQ for this too make the code more concise. You could set the Tag property of each TextBox in a column to make them easy to identify, then do something like this:

vb.net Code:
Dim sum = TableLayoutPanel1.Controls.
                            OfType(Of TextBox)().
                            Where(Function(tb) CStr(tb.Tag) = "The tag you set").
                            Sum(Function(tb) CInt(tb.Text)

----------


## surbek

Thank you for helping.
I want to print it every day and I dont really like how it looks with datagridview but I will give it a try.
I think I will use TableLayoutPanel and textboxes inside it.

What about adding and deleting rows of textboxes?
When I add a row, they must contain some code to do all the math. 
I thought i might add lets say 50 more rows, then I will have around 100 alltogether which must be enough and then i would make rows enabled/disabled or visible/hidden. 
Would that be ok?

----------


## jmcilhinney

> I want to print it every day and I dont really like how it looks with datagridview but I will give it a try.


How it looks in the grid is irrelevant to printing. You're printing the data, not the controls.



> What about adding and deleting rows of textboxes?
> When I add a row, they must contain some code to do all the math. 
> I thought i might add lets say 50 more rows, then I will have around 100 alltogether which must be enough and then i would make rows enabled/disabled or visible/hidden. 
> Would that be ok?


That's a very bad idea. That many controls is going to start making your form chug. You really should just use a grid. They are optimised for displaying large amounts of data without the overhead of a separate control for every value.

----------


## surbek

Ok, thank you.

----------

