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?

Dani AI

Generated

Short summary and practical plan: building on 's dynamic-creation approach and 's grid suggestion, keep two things separate: a lightweight way to track "modified" state for each dynamically created TextBox, and a Paint-based redraw that reads current values and draws the polyline. Attach a single TextChanged handler when you create each TextBox, store the original text in the TextBox.Tag, and update a small map/counter so you can show "how many have been modified" cheaply (no full scan on every keystroke).

Example: keep a Dictionary to track state and update a label/count when a textbox changes.

Private modifiedCount As Integer = 0
Private modifiedMap As New Dictionary(Of TextBox, Boolean)

Private Sub RegisterTextBox(tb As TextBox)
    tb.Tag = tb.Text    ' remember original
    AddHandler tb.TextChanged, AddressOf DynamicTextBox_TextChanged
End Sub

Private Sub DynamicTextBox_TextChanged(sender As Object, e As EventArgs)
    Dim tb As TextBox = DirectCast(sender, TextBox)
    Dim orig As String = If(tb.Tag IsNot Nothing, CStr(tb.Tag), "")
    Dim isModified As Boolean = (tb.Text <> orig)
    Dim previous As Boolean = False
    If modifiedMap.TryGetValue(tb, previous) Then
        If isModified <> previous Then
            modifiedMap(tb) = isModified
            modifiedCount += If(isModified, 1, -1)
        End If
    Else
        modifiedMap(tb) = isModified
        If isModified Then modifiedCount += 1
    End If
    LabelModifiedCount.Text = modifiedCount.ToString()
    PictureBox1.Invalidate()  ' trigger redraw if values affect the plot
End Sub

For drawing the slope between successive points: do it in the PictureBox.Paint handler (avoid CreateGraphics since it is not persistent). Parse textboxes in your container (FlowLayoutPanel or Panel), collect valid numeric values, compute min/max and a scale, build a Point() array and call DrawLines. Example approach: compute spacing from PictureBox width, map each numeric value to a Y pixel using a linear transform, set SmoothingMode, and call e.Graphics.DrawLines. Call PictureBox.Invalidate() when values change so the plot updates automatically.

Notes and cautions: use a container (FlowLayoutPanel) to simplify layout, use SuspendLayout/ResumeLayout when adding many controls, remove handlers when disposing a textbox, validate numeric input with TryParse, and prefer double-buffered drawing or drawing into a Bitmap if you see flicker. If an editable table makes more sense, DataGridView gives built-in editing and change events that you can bind into the same redraw/count workflow.

Recommended Answers

All 5 Replies

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

What do you mean by grid control?

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)
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.