Hey everyone,

I'm writing a program for work. I need to do the following:

The user inputs an unknown number of values to be processed. I want to allow them to basically click "new" and be able to add a new textbox or something that will allow them to input their data. I want to do this to keep from having 30 textboxes on the form, when only 7 are needed.

Does anyone know how I can do this?

Try something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ans As Integer = CInt(InputBox("How many textboxes do you want?"))
        Dim txtDist As Integer = 10
        Dim txtLocation As New Point(10, 10)
        Dim txtSize As New Size(100, 25)
        For x As Integer = 1 To ans
            Dim txt As New TextBox
            txt.Size = txtSize
            txt.Name = "txName" & CStr(x)
            txt.Location = txtLocation
            txt.Visible = True
            txt.Text = txt.Name
            Me.Controls.Add(txt)
            txtLocation.X += txt.Width + txtDist
            If txtLocation.X + txtSize.Width + txtDist > Me.ClientRectangle.Width Then
                txtLocation.X = 10
                txtLocation.Y += txtSize.Height + txtDist
            End If
            My.Application.DoEvents()
        Next
    End Sub

Hey everyone,

I'm writing a program for work. I need to do the following:

The user inputs an unknown number of values to be processed. I want to allow them to basically click "new" and be able to add a new textbox or something that will allow them to input their data. I want to do this to keep from having 30 textboxes on the form, when only 7 are needed.

Does anyone know how I can do this?

Hi, My suggestion, use Grid control.
> Get the input from single text box
> If it is not empty then add to the Grid

Try something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ans As Integer = CInt(InputBox("How many textboxes do you want?"))
        Dim txtDist As Integer = 10
        Dim txtLocation As New Point(10, 10)
        Dim txtSize As New Size(100, 25)
        For x As Integer = 1 To ans
            Dim txt As New TextBox
            txt.Size = txtSize
            txt.Name = "txName" & CStr(x)
            txt.Location = txtLocation
            txt.Visible = True
            txt.Text = txt.Name
            Me.Controls.Add(txt)
            txtLocation.X += txt.Width + txtDist
            If txtLocation.X + txtSize.Width + txtDist > Me.ClientRectangle.Width Then
                txtLocation.X = 10
                txtLocation.Y += txtSize.Height + txtDist
            End If
            My.Application.DoEvents()
        Next
    End Sub

Thanks! This is working great.
Maybe you can help me with my next part. I need to draw the slope that connects the two points. I have the drawing part figured out, as far as what methods to call and whatnot (I think) but I'm having problems with drawing multiple lines. I'd like to take the textbox inputs and draw the slopes of each one.

So for example, if they chose to enter 10 points with 10 associated elevations I would like to draw a line connecting point 1 to 2, 2 to 3, 3 to 4, etc.

Can you help me with designing the for loop for this? I'm having a hard time with associating the drawline method with the appropriate textboxes.

Here's what I'm doing right now

Dim Gr As Graphics = PictureBox1.CreateGraphics()
        Dim skyBluePen As New Pen(Brushes.DeepSkyBlue)
        Gr.DrawLine(skyBluePen, (2 * 5), Convert.ToInt16(1759 * 0.067), (103 * 5), Convert.ToInt16(1768 * 0.067 - 20))

The multiplication is to adjust for the size of the picture box and the location. I don't think this will be a permanent fix, but this is yet another problem I'm having. I had to do -20 at the end because the elevations were so close (within a pixel) it wasn't even noticeable.

What do you mean by grid control?

Grid Control Means DataGridView control. With the DataGridView control, you can display data in tabular form.

So for example, if they chose to enter 10 points with 10 associated elevations I would like to draw a line connecting point 1 to 2, 2 to 3, 3 to 4, etc.

I think you may consider about to draw polygon

Example

Dim Gr As Graphics = PictureBox1.CreateGraphics()
        Dim Points As Point() = New Point() {New Point(100, 100), New Point(200, 100), New Point(200, 200)}
        Gr.DrawPolygon(Pens.Blue, Points)
This article has been dead for over six months. Start a new discussion instead.