Referencing TextBoxes in a Form – both created dynamically

Tags:
VB.NET
WinForms
Hi! I am using a datagridview to display a table, and I want that, on mouseClick on any row of the datagrid, something like a popup/sub-form should show the row items in it and the user is able to edit it. For this, I managed to successfully display rows in the datagridview, created a form dynamically and added controls (textboxes and Add/Update buttons with click-event handlers) to it. The problem is how to access the controls of this form (i.e the values of the textboxes) in the Button Click even Handling Sub, so that the table/dataset is updated? I am UNABLE to 'reference' the controls in the dynamically created form.


Software/Hardware used:
VB.Net, VS 2012, MS-Access
0

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

Discuss This Question: 2  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.
  • ToddN2000
    Post a sample of how you are trying to reference it. We can take a look and see where the problem is.
    133,740 pointsBadges:
    report
  • Abcecedarian
    Right @ToddN2000, here it is:
    ----------------------------------------------------------------------------
    Private Sub DGVClients_RowHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGVClients.RowHeaderMouseClick

            Dim txtClientId As New TextBox
            With txtClientId
                .Size = New System.Drawing.Size(150, 20)
                .Location = New System.Drawing.Size(150, 20)
            End With
            Dim txtClientName As New TextBox
            With txtClientName
                .Size = New System.Drawing.Size(150, 20)
                .Location = New System.Drawing.Size(150, 45)
            End With
            Dim txtClientAddress As New TextBox
            With txtClientAddress
                .Size = New System.Drawing.Size(200, 20)
                .Location = New System.Drawing.Size(150, 75)
            End With

            Dim lblCId, lblCName, lblCAdd, As Label

            lblCName = New Label
            lblCId = New Label
            lblCAdd = New Label

            With lblCId
                .Text = "Client Id:"
                .Font = New Font("Arial", 12)
                .Location = New System.Drawing.Size(20, 20)
                .Show()
            End With
            With lblCName
                .Text = "Client Name:"
                .Font = New Font("Arial", 12)
                .Location = New System.Drawing.Size(20, 45)
                .Show()
            End With
            With lblCAdd
                .Text = "Client Address:"
                .Font = New Font("Arial", 12)
                .Location = New System.Drawing.Size(20, 75)
                .Show()
            End With

            Dim btnAddC As Button
            btnAddC = New Button
            With btnAddC
                .Text = "Add Client"
                .Font = New Font("Arial", 10)
                .Size = New System.Drawing.Size(100, 35)
                .Location = New System.Drawing.Size(20, 260)
            End With
            AddHandler btnAddC.Click, AddressOf Me.AddNewClient


            Dim curForm As Form = New Form
            With curForm
                'Definition of the form
                .Text = "Add / Update Client"
                .Size = New System.Drawing.Size(400, 350)
                .StartPosition = FormStartPosition.CenterScreen
                .Show()
                .Controls.Add(lblCId)
                .Controls.Add(txtClientId)
                .Controls.Add(lblCName)
                .Controls.Add(txtClientName)
                .Controls.Add(lblCAdd)
                .Controls.Add(txtClientAddress)
                .Controls.Add(lblCCity)

                .Controls.Add(btnAddC)

            End With
        End Sub

        Friend Sub btnAddC(ByVal sender As System.Object, ByVal e As System.EventArgs)
       
            MsgBox(txtClientId.text)            <<== At this point is the problem!   
       
        End Sub
    --------------------------------------------------------------------------

    I get NullReferenceException. The object throwing it, is as obvious the textbox. I understand that the dynamic instances must be in some way 'made available' to any external method/subroutine. But how can I do that? Pls. suggest. Better still, if any working code could be provided, I would appreciate it.
    Thanks!
    15 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

Thanks! We'll email you when relevant content is added and updated.

Following

Share this item with your network: