This my code in button click event. I want to add dynamic text boxes. It is focused to line no 7 and show "Object reference not set to an instance of an object."

        Dim txtBx() As TextBox
        Static x As Integer
        Static i As Integer

        x = x + 20

        txtBx(i).Location = New Point(10, 10 + x) ''''Object reference not set to an instance of an object
        txtBx(i).Size = New Size(100, 20)

        i = i + 1

        Me.Controls.Add(txtBx(i))

Please help me to correcting this problem.

Dim txtBx() As New TextBox

To further explain

Dim txtBx As TextBox

declares a variable which can contain the address of a TextBox control. It does not create the control.

Dim txtBx As New TextBox

both creates the control (New) and assigns the address to txtBx. The above line is equivalent to (shorthand for)

Dim txtBx As TextBox
txtBx = New TextBox

Once again I tryed this code

        Dim txtBx As TextBox
        Static x As Integer

        txtBx = New TextBox

        txtBx.Location = New Point(10, 10 + x)
        txtBx.Size = New Size(100, 20)

        x = x + 20

        Me.Controls.Add(txtBx)

It is working well. But I dont know How to name (when draging a tool from toolbox, we can go to properties and can be able to ) the dynamically generated textboxes.
for an example if we want to assign a value to a variable we can use this code.

Dim var1 as string
var1 = textbox1.text 

But I have no idea how to assign values to variables from dynamically generated textboxes like above code.

Please tell me how to do this.

You've already done that in the statement

txtBx.Location = New Point(10, 10 + x)

How is that different from what you are asking? Perhaps it would help if you stated what problem you are trying to solve without using code in the explanation.

You are correct. But I've tried a lot with this code also.

txtBx.name = "txt" & i

But I can not call txt1, txt2, txt3 etc. from some other procidures like button2 click event. it is not accepted txt1.text = "Some value". (it is shown blue color underline below txt1)

Is there no any way to do this?

Sorry, I did not take the time to try and understand your goals the first time. I think this example should get you going.

Public Class Form1

   ' Assuming you want to add 10 textboxes
   ' By declaring this reference at the Form level you can directly access the TB by index

   Private AddedTBs(0 To 9) As TextBox

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

      Dim startY As Int32 = 10 'Starting vertical position

      For i As Int32 = 0 To UBound(AddedTBs)
         AddedTBs(i) = New TextBox
         With AddedTBs(i)
            ' setting the Name will allow you to identify the TB later
            ' in the Form's control collection
            .Name = "AddedTB" & (i + 1).ToString 'start with AddedTB1
            .Size = New Size(100, 20)
            ' set vertical position based on even spacing of the controls height
            ' plus 5 pixels between the controls
            .Location = New Point(10, If(i = 0, startY, startY + ((.Size.Height + 5) * i)))
            .Parent = Me
            ' If you need to handle events for the TB's, you need to wire-up
            ' the handler yourself

            ' assume you want to handle the textchanged event for this TB
            AddHandler .TextChanged, AddressOf AddedTBs_TextChange

            ' assume you want to handle the textchanged event for this TB
            AddHandler .KeyPress, AddressOf AddedTBs_KeyPress

         End With
      Next
   End Sub

   Private Sub AddedTBs_TextChange(ByVal sender As Object, ByVal e As System.EventArgs)
      'get a reference to the TB whose text is changing
      Dim tb As TextBox = CType(sender, TextBox)
      'some code
   End Sub

   Private Sub AddedTBs_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
      'get a reference to the TB whose text is changing
      Dim tb As TextBox = CType(sender, TextBox)
      Select Case tb.Name
         Case "AddedTB1"
            'some special handling code
         Case "AddedTB2"

         '.
         '. any others you need individual code for
         '.

      End Select

      If e.KeyChar = Chr(Keys.Enter) Then
         ' Move to next control, emulates pressing Tab key
         tb.FindForm.SelectNextControl(tb, True, True, True, True)
         e.Handled = True 'stop the beep
      End If
   End Sub


   'The following shows 2 ways to reference the TBs

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      ' This button was added through the designer
      ' If you need to do something say with the 3rd added textbox, then this will
      ' do the trick
      CType(Me.Controls("AddedTB3"), TextBox).Text = "fred"
   End Sub

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
      ' This button was added through the designer
      ' If you need to do something say with the 1st added textbox, then this will
      ' do the trick via the form reference
      AddedTBs(0).Text = "Bill"
   End Sub
End Class

I think we are wasting our time playing twenty questions and trying to guess what problem the OP is actually trying to solve. Once we know what he/she is trying to accomplish we can provide a useful answer.

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.