Hi,

In Form 3 I have one Text box and Button.

Question #1: If I enter 200 value in Form3 Texbox.1 And button1 click and then next I want to create 200 Textboxes in the form3. I already the code but it is not working. It is creating the text boxes but box to box distance is enlarging. How can I maintain same distance to each textbox.
Question #2: Whatever the input I am giving in Textbox.1 same number of lables should be created in form1. How can I do it.
Question #3: These 200 Textboxes / labels text or values to be populated into Excel sheet as headers.

Here is the code for Question #1:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim txtb As New TextBox
        Dim i As Integer
        i = TextBox1.Text
        If i < 1 Or i > 257 Then
            MsgBox("Please provide DB Headers Count less then 256", MsgBoxStyle.OkOnly = True, "DB Header Count")
        End If
        For Row = 1 To 1
            For Col = 1 To i
                If i < 257 = True Then
                    txtb = New TextBox
                    txtb.Size = New Drawing.Size(200, 20)
                    txtb.Location = New Point(10 + 100 * Row, 10 + 25 * Col)
                    txtb.Name = txtb.Name
                    boxes(Row, Col) = txtb
                    Me.Controls.Add(txtb)
                Else
                    MsgBox("Please provide DB Headers Count less then 256", MsgBoxStyle.OkOnly = True, "DB Header Count")
                    If vbOK Then
                        GoTo txtb
                    End If
                End If
            Next
        Next
txtb:
    End Sub

By the code you can understand that I am new to Coding / Programming. Thanks for you valuable time & Suggestions.

Recommended Answers

All 41 Replies

Good luck getting 200 text boxes on the same form.

txtb.Name = txtb.Name

Huh? Why are you setting Name equal to itself?

Question 3: What version of vb.net are you using -- I know vb.net 2013 makes it very easy to create excel worksheets, here is an example:

fced9aa485ce9e9b0e3cecf6f0c9e27e

Hi,

I am using VS2012 Can you please provide me the correct code for this. Thanks for your response.

There is no code -- it was generated by the wizard. I'm using 2013 Pro, don't know if 2013 Express has the wizard or not. It's under the heading vb automation.

I was expecing some help here. Is this is not possible with VS2012. Request for positive response.

What you are attempting to do is called "Office Automation" -- using vb.net (or some other .net language) to automate one of the Microsoft Office programs. google "vb.net office automation" and you will find a lot of information on how to do that. Another topic to google is "vb.net excel open workbook" And another article here.

Ok. I am looking at your suggested sources to complete my project. But can you please help me with the above code. When I am trying to create the textboxes the distance is keepon increasing to each other.

For example first box to second box distance was 20, 99 box to 100 box distance 200. how can fix it.

Request a quick help.

And guys, thanks for all your replies.

I ran your function and all the text boxes look equal distance from each other -- the problem is that I can see only the first few, depending on how large I make the window, because there is no scroll bar to make them visible. The only change I made was that I commented out the line 'boxes(Row, Col) = txtb and I just hard coded an initial value or i instead of entering it in a text box.

And the boxes have vertical alignment as in rows of an Excel worksheet, not horizontal alignmemt as in columns.

Hi,

For time to time I need the textboxes to be created in different numbers.
Once I might need 5 textboxes next time I may need 256.

Can't we handle this with textbox by giving the value of numbers instead of giving an Initial Solid Value.

Well, yes -- I just hard coded it for my own convenience. But you still have the problem of being able to view all those text boxes without a scrollbar. An easy way to do that would probably put them in a ListView control or DataGridView control.

You can put a VScrollBar control on the form then handle it's events in order to scroll the textboxs into view.

ok. I have got code from this forum. Below is the modified code.
Scroll bar will not be an issue. I will put an fix for it or I will enable autoscroll option.

The problem with the below code is distance between each other.
Check the complete boxes you will know the exact problem.

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newbox As TextBox
        For row As Integer = 1 To 1 'TextBox1.Text
            For col As Integer = 1 To 100 'TextBox2.Text
                'create a new textbox and set its properties
                newbox = New TextBox
                newbox.Size = New Drawing.Size(100, 20)
                newbox.Location = New Point(80 + (120 * row), 120 + (25 * col))
                newbox.Name = "TextBox" & row & "_" & col
                newbox.Text = newbox.Name
                'connect it to a handler, save a reference to the array and add it to the form controls
                AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
                '    boxes(row, col) = newbox
                Me.Controls.Add(newbox)
            Next
        Next

    End Sub

Please provide an fix for this.

I don't see any problem with it. Here is what I see. How do you know the distance is wrong for the last few boxes, you can't even see them?

130fa4d501d9e42de67b9e8c31dace8d

I have set the form with autoscroll option enabled after 75 to 80 boxes distance is keep on increasing. Till 1 to around 65 or 75 boxes distance were same. But next to them distance changed. Please look into this issue and provide an fix for this.

3e74300b820c56d7b5514cf94aedd738

On second thought, yes I do see a problem -- the text boxes appear to be getting closer and closer together, the gap between the first two are a lot greater than the gap between the 12th and 13th. That behavior occurs with both VB.NET versions 2012 and 2013. I don't know how to fix it either.

So is there any fix for this.

I turned on AutoScroll -- it appears to be just a visual thing because when I scroll the boxes at the top of the windows appear to be ok. I don't know if there is a fix for it or not, most likely not. But don't take my word for it -- there are lots of people who are more experienced with vb.net than I am, especially Reverend Jim.

So we can get help for this issue. Since it is stopping to complete my project.

Why don't you try an alternative way of doing it, like I've mentioned before?

I didn't understand what you are saying please. I want export data to 3 formats. xlm, csv, Access mdb data base.

How you present the data on the screen is completely independent of what format the data will be written. Is there only one column of those boxes? Instead of using all those boxs you can do the same with a DataGridView control. With that control you just tell it how many rows and columns you want, then you can populate then with anything you want -- the boxes are even editable so that someone can enter the data into the grid manually from the keyboard.

If you are going to save the data in csv format then you don't need to do anything with Office Automation and I previously mentioned. CSV is just a simple text file with columns separated by either commas or tabs. I don't know anything about xml format, never worked with it. Saving to an Access database isn't terribly difficult, there are lots of tutorials how to do it.

Here is an example using DataGridView. I just hard-coded it to contain 10 rows and 125 columns, but you could get this info from a textbox if you wish.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        DataGridView1.RowCount = 10
        DataGridView1.ColumnCount = 125
        Dim row As Integer = 1
        DataGridView1.ColumnHeadersVisible = True
        For col = 0 To DataGridView1.ColumnCount - 1
            DataGridView1.Columns(col).Name = "TextBox" & row & "_" & col
        Next

    End Sub

Just tried your code. Just removed
'AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
because it threw an error. Textboxes line up ok. 23837161e29387458df840de5d1411e4

@ Reverend Jim,

I made this your code. I am facing the same problem like above. What is the exact problem with the below code.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newbox As TextBox
        For row As Integer = 1 To 1
            For col As Integer = 1 To 28

                'create a new textbox and set its properties
                newbox = New TextBox
                newbox.Size = New Drawing.Size(100, 20)
                newbox.Location = New Point(100 + 30 * row, 400 + 25 * col)
                'newbox.Name = "TextBox" & row & "_" & col
                newbox.Name = "TextBox" & col
                newbox.Text = newbox.Name

                'connect it to a handler, save a reference to the array and add it to the form controls
                'AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
                Me.Controls.Add(newbox)
            Next
        Next
        Dim newl As Label
        For row As Integer = 1 To 1
            For col As Integer = 1 To 28

                'create a new textbox and set its properties
                newl = New Label
                newl.Size = New Drawing.Size(100, 20)
                newl.Location = New Point(1 + 30 * row, 400 + 25 * col)
                newl.Name = "TextBox" & col
                newl.Text = newl.Name
                'connect it to a handler, save a reference to the array and add it to the form controls
                'AddHandler newbox.TextChanged, AddressOf TextBox_TextChanged
                Me.Controls.Add(newl)
            Next
        Next
    End Sub

50eb88cfa74bb801fd93755f972c625450eb88cfa74bb801fd93755f972c6254

Hi Minimalist,

I have tried what you were saying but still getting the same output.

I am updating my VS once it is done I will try it again.

Thanks.

Bilaluddin I have used your new code. I do get a misalignment if the form is too small and I need to enlarge it. On a maximised form I do net get any issues with the alignment. Have a look at some related issues in this forum.
I also put another button on to remove all the controls and redraw them.

 Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        For i As Integer = Me.Controls.Count - 1 To 0 Step -1
            If TypeOf Me.Controls(i) Is TextBox Then
                Me.Controls.RemoveAt(i)
            End If
        Next
        For k As Integer = Me.Controls.Count - 1 To 0 Step -1
            If TypeOf Me.Controls(k) Is Label Then
                Me.Controls.RemoveAt(k)
            End If
        Next
    End Sub

This will allow you to redraw the controls at diffrent form sizes-

I just checked out this example and it is excellent
"adding multiple TextBoxes to a form (control array)"

I have found a solution by myself. I don't know what is causing this but found a solution. I have created a panel inside the form and I have enabled AutoScroll Option. And this is sorted out my issue.

I think issue with form size itself.

Example if a form height is 1200 then If textboxes are creating beyond this limit the textboxes alignment is changing. Also I have enabled AutoScroll for this form as well.

I am not sure but this might be the one of the reason.

I will wait for one more day and If everything is working fine then I will close this thread as "Solved".

But Still my 3rd question is still pending. Need to find out by myself. If I am not getting the help.

Please remind us again what your 3d question is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.