Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I'm trying to write a class at the moment that creates a brand new textbox on form1 after each click.

the newly created textbox needs to appear directly under the next textbox.

It's very simple at the moment as you can see.

Public Class Textbox_Create

    Shared Property tb As Control

    Public Shared Sub addtextbox()
        Dim Counttotal As Integer = 1
        Dim count As Integer = 1
        Dim name As String
        Dim tb As New TextBox()
        name = ("TxtBox" & CStr(Counttotal))
        tb.Name = name
        tb.Height = 10
        tb.Width = 5
        Counttotal = count + Counttotal
    End Sub
End Class

The first button click works fine, it creates the first textbox. All good.

But on second button click (Obviously) It is being placed right over the top of the original textbox, not underneath.

It names the textbox as I want to be able to refer to the previous textbox's location (I'm guessing you would refer to the original textbox name and then point to it's x,y location)

Am I going about this the right way?
Updated 3-Feb-14 20:12pm

you could try this one. Sorry, I misunderstood your question..

Dim tb(100) As TextBox
Dim Counttotal As Integer
Dim txtname As String
Dim Xloc As Integer
Dim Yloc, ctr As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Counttotal += 1
        tb(Counttotal) = New TextBox
        txtname = ("TxtBox" & CStr(Counttotal))
        With tb(Counttotal)
            .Location = New Point(Xloc, Yloc + ctr)
            .Text = txtname
            .Height = 10
            .Width = 50
        End With
        ctr += 20
End Sub
Share this answer
Mendaharin 5-Feb-14 23:13pm    
thank you very much kArViD0tnEt! That works perfectly outside of my project. Now to try and figure out how to implement that into my project :) exactly what I needed though!
Just drag a FlowLayoutPanel where the text box to be displayed.

Try the following code.

Private ClickCounter As Integer = 0
Private Overloads Sub CreateTextBox(ByVal container As Control)
    'Create new textbox instance
    Dim m_NewTextBox As New TextBox
    'Set Properties
    m_NewTextBox.Name = String.Format("TextBox{0}", ClickCounter)
    m_NewTextBox.Text = String.Format("TextBox{0}", ClickCounter)
    'Add to container(flowlayout pannel container)
    'Increment ClickCounter
    ClickCounter = ClickCounter + 1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    FlowLayoutPanel1.AutoScroll = True
End Sub
Share this answer
Mendaharin 6-Feb-14 0:55am    
Very interesting. Just wondering, as I'm trying to figure out the best way to do it. Eventually you will be able to delete these textboxes, which would leave an empty space. By using flowlayout, is it easier to fill those spaces when more information comes through?
I will have a look at the class information and see what's available.
if not at runtime, you can just right click on the textbox and click send to back.

but if at runtime, I believe there's this kind of code:

I haven't tried that one, but I've seen it whenever I press '.' after the control's name. :D

maybe you can try. I won't hurt I think..
Share this answer
Mendaharin 5-Feb-14 18:45pm    
Thanks it does do as you say, but I think I've confused. I don't need one textbox underneath another I need it below the first textbox. so textbox1 is at x,y and then textbox 2 is underneath textbox1's lowest point y.
Karen Mitchelle 5-Feb-14 20:27pm    
I'm sorry I'm also confused. So you are saying that the next textbox must be UNDER the first textbox? The word UNDERNEATH is confusing, you know..
I'll try making a code for that. Wait.. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900