Friends...

I'm doing INVENTORY project...

front-end : vb.net

back-end : Access

I'm having PURCHASE, SALES tables...

In sales form, i need to create CONTROLS during RUN-TIME...

i.e, when we gone for shopping to a DEPARTMENT store, we buy LOT things... they will give us only one BILL... when they are entering BILL, they will enter only CODE of the product, all the other particulars will be loaded automatically... if v buy 10 items, there will be 10 items in BILL... if it s 15, 15 in bill...

i need to create like this only...

i.e in SALES form, CODE, CATEGORY, BRAND, S.P, DATE, QUANTITY, TOTAL etc., are available... when i click a BUTTON say NEXT, the controls specified(CODE, CATEGORY, BRAND, S.P, DATE, QUANTITY, TOTAL) has to be created automatically, in same size,and in same COLUMN (in form)(i.e CODE is a COMBO BOX, WHEN I PRESS NEXT another COMBO BOX has to be created in same COLUMN), whenever i press the NEXT...

and also, when i press SAVE BUTTON, ALL THE DETAILS HAVE TO BE STORED IN DATABASE...

I know the concept of CONTROL ARRAY... but i don't know how to implement in vb.net...

please help me....

Thanks in advance...

Dear...

Please help me to solve this...

I'm in need to finish my project and have to submit to company...

Please help me...

Thanks in advance...

See if the following Start-Up Project helps.

Paste in new Project as is.

Public Class Form1
    '// create Original controls.
    Private cmbCODE1 As New ComboBox With {.Name = "cmbCODE1", .Location = New Point(0, 0), .Size = New Size(121, 21)}
    Private cmbCATEGORY1 As New ComboBox With {.Name = "cmbCATEGORY1", .Location = New Point(125, 0), .Size = New Size(121, 21)}
    Private txtTOTAL1 As New TextBox With {.Name = "txtTOTAL1", .Location = New Point(249, 0), .Size = New Size(75, 21)}
    Private WithEvents btnNEXT As New Button With {.Location = New Point(334, 0), .Size = New Size(75, 21), .Text = "Next"}
    '// lists to populate the ComboBoxes.
    Private lstCODEitems() As String = {"code 1", "code 2", "code 3"}
    Private lstCATEGORYitems() As String = {"category 1", "category 2", "category 3"}
    '// used to name controls for each row, ex: new cmbCODE will be named cmbCODE2, etc.
    Private ctlRow As Integer = 1
    '// used to add controls to a preset Y location since the X location is constant.
    Private ctlRowLocation As Integer = 0

    '// save data to File.
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim myFile As String = Application.StartupPath & "savedData.txt" '// location of file.
        Dim myWriter As New IO.StreamWriter(myFile)

        Dim tempStr As String = "" '// declare new TEMP string.
        For Each ctl As Control In Me.Controls '// loop thru all controls for cmbCODE controls.
            If ctl.Name.StartsWith("cmbCODE") Then tempStr &= "#" & ctl.Text '// add the text values.
        Next
        myWriter.WriteLine(tempStr) '// write to line.

        tempStr = "" '// clear string.
        For Each ctl As Control In Me.Controls '// loop thru all controls for cmbCATEGORY controls.
            If ctl.Name.StartsWith("cmbCATEGORY") Then tempStr &= "#" & ctl.Text '// add the text values.
        Next
        myWriter.WriteLine(tempStr) '// write to line.

        tempStr = "" '// clear string.
        For Each ctl As Control In Me.Controls '// loop thru all controls for txtTOTAL controls.
            If ctl.Name.StartsWith("txtTOTAL") Then tempStr &= "#" & ctl.Text '// add the text values.
        Next
        myWriter.WriteLine(tempStr) '// write to line.

        myWriter.Close()
        Process.Start(myFile) '// preview file.
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbCODE1.Items.AddRange(lstCODEitems) '// add items.
        cmbCATEGORY1.Items.AddRange(lstCATEGORYitems) '// add items.
        '// add original controls to Form.
        With Me.Controls : .Add(cmbCODE1) : .Add(cmbCATEGORY1) : .Add(txtTOTAL1) : .Add(btnNEXT) : End With
    End Sub

    Private Sub btnNEXT_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNEXT.Click
        ctlRow += 1
        ctlRowLocation += 25
        '// new cmbCODE ComboBox.
        Dim new_cmbCODE As New ComboBox
        new_cmbCODE.Name = "cmbCODE" & ctlRow '// name control.
        new_cmbCODE.Items.AddRange(lstCODEitems) '// add items.
        new_cmbCODE.Location = New Point(cmbCODE1.Location.X, ctlRowLocation) '// set new location.
        new_cmbCODE.Size = cmbCODE1.Size '// keep same size as the original.
        Me.Controls.Add(new_cmbCODE) '// add control to Form.
        '// new cmbCATEGORY ComboBox.
        Dim new_CATEGORY As New ComboBox
        new_CATEGORY.Name = "cmbCATEGORY" & ctlRow '// name control.
        new_CATEGORY.Items.AddRange(lstCATEGORYitems) '// add items.
        new_CATEGORY.Location = New Point(cmbCATEGORY1.Location.X, ctlRowLocation) '// set new location.
        new_CATEGORY.Size = cmbCODE1.Size '// keep same size as the original.
        Me.Controls.Add(new_CATEGORY) '// add control to Form.
        '// new txtTOTAL TextBox.
        Dim new_TOTAL As New TextBox
        new_TOTAL.Name = "txtTOTAL" & ctlRow '// name control.
        new_TOTAL.Location = New Point(txtTOTAL1.Location.X, ctlRowLocation) '// set new location.
        new_TOTAL.Size = txtTOTAL1.Size '// keep same size as the original.
        Me.Controls.Add(new_TOTAL) '// add control to Form.
        '// move the Button to the next row's location.
        btnNEXT.Location = New Point(btnNEXT.Location.X, ctlRowLocation)
    End Sub
End Class

A few notes:
It only creates the original controls as when project is started.

Also, this project-sample saves data to a file, each line for each similarly named control, with the controls value.
Since this code saves in preset lines to the .txt file, I can easily open the file and locate any line to extract data from it by using .Split("#").

Do let me know if the above helps and if you need further help with this.

Good luck with your submission to the company.
All the best,
codeorder.

Dear...

I saved ur code in NEW PROJECT... i.e i created a NEW PROJECT...

i copied ur code and created a BUTTON, named btnNEXT...

but it is showing error...

It displays error in ALL WITH statement...

With {.Name = "cmbCODE1", .Location = New Point(0, 0), .Size = New Size(121, 21)}

The error message is END OF STATEMENT EXPECTED.

But when i delete that WITH satement...

It is automatically creating COMBO BOX for CODE...

Public Class Form1
    '// create Original controls.
    Private cmbprdcde1 As New ComboBox

    Private txtCtgry1 As New ComboBox
    Private txtTOTAL1 As New TextBox
    ' Private WithEvents btnNext As New Button 
    '// lists to populate the ComboBoxes.
    '// used to name controls for each row, ex: new cmbCODE will be named cmbCODE2, etc.
    Private ctlRow As Integer = 1
    '// used to add controls to a preset Y location since the X location is constant.
    Private ctlRowLocation As Integer = 0

    '// save data to File.
    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        '// add original controls to Form.
        With Me.Controls : .Add(cmbprdcde1) : .Add(txtCtgry1) : .Add(txtTOTAL1) : .Add(BtnNext) : End With
    End Sub

    Private Sub btnNEXT_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNEXT.Click
        ctlRow += 1
        ctlRowLocation += 25
        '// new cmbCODE ComboBox.
        Dim new_cmbCODE As New ComboBox
        new_cmbCODE.Name = "cmbCODE" & ctlRow '// name control.
        'new_cmbCODE.Items.AddRange(lstCODEitems) '// add items.
        new_cmbCODE.Location = New Point(cmbprdCDE1.Location.X, ctlRowLocation) '// set new location.
        new_cmbCODE.Size = cmbprdCDE1.Size '// keep same size as the original.
        Me.Controls.Add(new_cmbCODE) '// add control to Form.
        '// new cmbCATEGORY ComboBox.
        Dim new_CATEGORY As New ComboBox
        new_CATEGORY.Name = "cmbCATEGORY" & ctlRow '// name control.
        'new_CATEGORY.Items.AddRange(lstCATEGORYitems) '// add items.
        new_CATEGORY.Location = New Point(txtCtgry1.Location.X, ctlRowLocation) '// set new location.
        new_CATEGORY.Size = cmbprdCDE1.Size '// keep same size as the original.
        Me.Controls.Add(new_CATEGORY) '// add control to Form.
        '// new txtTOTAL TextBox.
        Dim new_TOTAL As New TextBox
        new_TOTAL.Name = "txtTOTAL" & ctlRow '// name control.
        new_TOTAL.Location = New Point(txtTOTAL1.Location.X, ctlRowLocation) '// set new location.
        new_TOTAL.Size = txtTOTAL1.Size '// keep same size as the original.
        Me.Controls.Add(new_TOTAL) '// add control to Form.
        '// move the Button to the next row's location.
        btnNEXT.Location = New Point(btnNEXT.Location.X, ctlRowLocation)
    End Sub

End Class

This is d code i used...

It is creating 1 COMBO BOX...

OK.
New Project, DO NOT ADD ANY CONTROLS TO THE FORM, LEAVE FORM BLANK, AND PASTE THE CODE ENTIRELY AS I PREVIOUSLY POSTED INTO THE VB.NET CODE WINDOW. Run the Application.

The code creates the first controls, 2 ComboBoxes, 1 TextBox, and a Next Button. Click the Next button to add a new row of similar controls.

Dear...

I opened a NEW PROJECT and The FORM IS BLANK... i done NOTHING... i just COPIED ur CODE and pasted it in NEW PROJECT...

but, It is not working... I don't know why...

it shows 7 errors...

Error 1 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 3 38 WindowsApplication2

Error 2 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 4 42 WindowsApplication2

Error 3 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 5 38 WindowsApplication2

Error 4 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 6 46 WindowsApplication2

Error 5 Name 'btnNEXT' is not declared. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 46 89 WindowsApplication2

Error 6 Name 'btnNEXT' is not declared. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 73 9 WindowsApplication2

Error 7 Name 'btnNEXT' is not declared. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 73 38 WindowsApplication2

These are the errors...

Please help me to clear...

Thanks in advance...

Dear...

I opened a NEW PROJECT and The FORM IS BLANK... i done NOTHING... i just COPIED ur CODE and pasted it in NEW PROJECT...

but, It is not working... I don't know why...

it shows 7 errors...

Error 1 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 3 38 WindowsApplication2

Error 2 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 4 42 WindowsApplication2

Error 3 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 5 38 WindowsApplication2

Error 4 End of statement expected. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 6 46 WindowsApplication2

Error 5 Name 'btnNEXT' is not declared. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 46 89 WindowsApplication2

Error 6 Name 'btnNEXT' is not declared. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 73 9 WindowsApplication2

Error 7 Name 'btnNEXT' is not declared. D:\Ariff\WindowsApplication2\WindowsApplication2\Form1.vb 73 38 WindowsApplication2

These are the errors...

Please help me to clear...

Thanks in advance...

Hmmm.
I get only 4 errors COPYING AND PASTING THE CODE YOU RECENTLY POSTED INTO A NEW PROJECT.

I get 32 errors pasting my code in Private Sub Form1_Load(.. of a new Project's code window.

I get no errors IF I FIRST CLEAR ALL CODE IN THE NEW PROJECT'S CODE WINDOW before pasting the code I recently provided in this thread.

And Dear...,
the code was compiled with Visual Studios 2010 Professional that uses a 2.0 .Net Framework.

Overall, no problems running such a simple code in a new project. I can just overlook the code I posted and know that it will run without any errors, how about you?

Hi Friend...

THANKS... THANKS... THANK YOU SO MUCH...

I CHANGED SOME CODING IN THIS AND NOW ITS WORKING...

THANKS ONCE AGAIN FOR ALL MEMBERS...

Friends...

Sorry to disturb u again...

Public Class Frm
    Private cmbprdcde1 As New ComboBox
    Private txtCtgry1 As New TextBox
    Private txtbrnd1 As New TextBox
    Private txtdesc1 As New TextBox
    Private txtprce1 As New TextBox
    Private txtdate1 As New TextBox
    Private txtqty1 As New TextBox
    Private txtdscnt1 As New TextBox
    Private txtsp1 As New TextBox
    Private ctlRow As Integer = 1
    Private ctlRowLocation As Integer = 0
   
    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
        ctlRow += 1
        ctlRowLocation += 25
        '// new cmbCODE ComboBox.
        Dim new_cmbCODE As New ComboBox
        new_cmbCODE.Name = "cmbCODE" & ctlRow '// name control.
        'new_cmbCODE.Items.AddRange(lstCODEitems) '// add items.
        new_cmbCODE.Location = New Point(cmbprdcde1.Location.X + 2, ctlRowLocation) '// set new location.
        new_cmbCODE.Height = 20
        new_cmbCODE.Width = 92
        'new_cmbCODE.Size = cmbprdCDE1.Size '// keep same size as the original.
        Me.Controls.Add(new_cmbCODE) '// add control to Form.
        Dim new_CATEGORY As New TextBox
        new_CATEGORY.Name = "cmbCATEGORY" & ctlRow
        new_CATEGORY.Location = New Point(txtCtgry1.Location.X + 107, ctlRowLocation)
        new_CATEGORY.Height = 20
        new_CATEGORY.Width = 102
        Me.Controls.Add(new_CATEGORY)
        Dim new_BRND As New TextBox
        new_BRND.Name = "txtBRND" & ctlRow
        new_BRND.Location = New Point(txtbrnd1.Location.X + 219, ctlRowLocation)
        new_BRND.Height = 20
        new_BRND.Width = 102
        Me.Controls.Add(new_BRND)
        Dim new_desc As New TextBox
        new_desc.Name = "txtdesc" & ctlRow
        new_desc.Location = New Point(txtdesc1.Location.X + 330, ctlRowLocation)
        new_desc.Height = 20
        new_desc.Width = 196
        Me.Controls.Add(new_desc)
        Dim new_prce As New TextBox
        new_prce.Name = "txtprce" & ctlRow
        new_prce.Location = New Point(txtprce1.Location.X + 534, ctlRowLocation)
        new_prce.Height = 20
        new_prce.Width = 56
        Me.Controls.Add(new_prce)
        Dim new_date As New TextBox
        new_date.Name = "txtdate" & ctlRow
        new_date.Location = New Point(txtdate1.Location.X + 598, ctlRowLocation)
        new_date.Height = 20
        new_date.Width = 102
        Me.Controls.Add(new_date)
        Dim new_QTY As New TextBox
        new_QTY.Name = "txtQTY" & ctlRow
        new_QTY.Location = New Point(txtqty1.Location.X + 706, ctlRowLocation)
        new_QTY.Height = 20
        new_QTY.Width = 43
        Me.Controls.Add(new_QTY)
        Dim new_DSCNT As New TextBox
        new_DSCNT.Name = "txtDSCNT" & ctlRow
        new_DSCNT.Location = New Point(txtdscnt1.Location.X + 762, ctlRowLocation)
        new_DSCNT.Height = 20
        new_DSCNT.Width = 51
        Me.Controls.Add(new_DSCNT)
        Dim new_SP As New TextBox
        new_SP.Name = "txtSP" & ctlRow
        new_SP.Location = New Point(txtsp1.Location.X + 827, ctlRowLocation)
        new_SP.Height = 20
        new_SP.Width = 56
        Me.Controls.Add(new_SP)
        BtnNext.Location = New Point(BtnNext.Location.X, ctlRowLocation)

    End Sub
End Class

This is working...

But the problem is, i already created those Controls (textboxes, combobox).

If i delete, it is showing error...

But with that controls, i already created control is not suiting...

i.e i need those controls to COME AFTER MY CONTROLS...

i.e in NEXT ROW TO my CONTROLS and then on ASUSUAL(IT HAS TO CREATE THOSE CONTROLS)...

Please help me to finish this...

Thanks in advance...

See if this helps.

Public Class Frm
    Private iCtlSpace As Integer = 5 '// used to add spaces between Controls.
    '// all other Controls will set their Locations from this first Control.
    Private cmbprdcde1 As New ComboBox With {.Location = New Point(2, 2), .Width = 92, .Height = 20}
    '// .Location="location of previous Control "+" the previous Control's .Width "+" the space between Controls, and Location.Y of previous Control.
    Private txtCtgry1 As New TextBox With _
        {.Location = New Point(cmbprdcde1.Location.X + cmbprdcde1.Width + iCtlSpace, cmbprdcde1.Location.Y), .Width = 102, .Height = 20}
    Private txtbrnd1 As New TextBox With _
        {.Location = New Point(txtCtgry1.Location.X + txtCtgry1.Width + iCtlSpace, txtCtgry1.Location.Y), .Width = 102, .Height = 20}
    Private txtdesc1 As New TextBox With _
        {.Location = New Point(txtbrnd1.Location.X + txtbrnd1.Width + iCtlSpace, txtbrnd1.Location.Y), .Width = 196, .Height = 20}
    Private txtprce1 As New TextBox With _
        {.Location = New Point(txtdesc1.Location.X + txtdesc1.Width + iCtlSpace, txtdesc1.Location.Y), .Width = 56, .Height = 20}
    Private txtdate1 As New TextBox With _
        {.Location = New Point(txtprce1.Location.X + txtprce1.Width + iCtlSpace, txtprce1.Location.Y), .Width = 102, .Height = 20}
    Private txtqty1 As New TextBox With _
        {.Location = New Point(txtdate1.Location.X + txtdate1.Width + iCtlSpace, txtdate1.Location.Y), .Width = 43, .Height = 20}
    Private txtdscnt1 As New TextBox With _
        {.Location = New Point(txtqty1.Location.X + txtqty1.Width + iCtlSpace, txtqty1.Location.Y), .Width = 51, .Height = 20}
    Private txtsp1 As New TextBox With _
        {.Location = New Point(txtdscnt1.Location.X + txtdscnt1.Width + iCtlSpace, txtdscnt1.Location.Y), .Width = 56, .Height = 20}
    Private ctlRow As Integer = 1
    Private ctlRowLocation As Integer = 0

    Private Sub Frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// add original controls to Form.
        With Me.Controls : .Add(cmbprdcde1) : .Add(txtCtgry1) : .Add(txtbrnd1) : .Add(txtdesc1) : .Add(txtprce1) : .Add(txtdate1) : .Add(txtqty1) : .Add(txtdscnt1) : .Add(txtsp1) : End With
    End Sub

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        ctlRow += 1
        ctlRowLocation += 25
        '// new cmbCODE ComboBox.
        Dim new_cmbCODE As New ComboBox '// ========== New Control =========
        With new_cmbCODE
            .Name = "cmbCODE" & ctlRow '// name control.
            .Location = New Point(cmbprdcde1.Location.X, ctlRowLocation) '// set new location.
            .Size = cmbprdcde1.Size '// keep same size as the original.
        End With
        Me.Controls.Add(new_cmbCODE) '// add control to Form.
        Dim new_CATEGORY As New TextBox '// ========== New Control =========
        With new_CATEGORY
            .Name = "cmbCATEGORY" & ctlRow
            .Location = New Point(txtCtgry1.Location.X, ctlRowLocation)
            .Size = txtCtgry1.Size
        End With
        Me.Controls.Add(new_CATEGORY)
        Dim new_BRND As New TextBox '// ========== New Control =========
        With new_BRND
            .Name = "txtBRND" & ctlRow
            .Location = New Point(txtbrnd1.Location.X, ctlRowLocation)
            .Size = txtbrnd1.Size
        End With
        Me.Controls.Add(new_BRND)
        Dim new_desc As New TextBox '// ========== New Control =========
        With new_desc
            .Name = "txtdesc" & ctlRow
            .Location = New Point(txtdesc1.Location.X, ctlRowLocation)
            .Size = txtdesc1.Size
        End With
        Me.Controls.Add(new_desc)
        Dim new_prce As New TextBox '// ========== New Control =========
        With new_prce
            .Name = "txtprce" & ctlRow
            .Location = New Point(txtprce1.Location.X, ctlRowLocation)
            .Size = txtprce1.Size
        End With
        Me.Controls.Add(new_prce)
        Dim new_date As New TextBox '// ========== New Control =========
        With new_date
            .Name = "txtdate" & ctlRow
            .Location = New Point(txtdate1.Location.X, ctlRowLocation)
            .Size = txtdate1.Size
        End With
        Me.Controls.Add(new_date)
        Dim new_QTY As New TextBox '// ========== New Control =========
        With new_QTY
            .Name = "txtQTY" & ctlRow
            .Location = New Point(txtqty1.Location.X, ctlRowLocation)
            .Size = txtqty1.Size
        End With
        Me.Controls.Add(new_QTY)
        Dim new_DSCNT As New TextBox '// ========== New Control =========
        With new_DSCNT
            .Name = "txtDSCNT" & ctlRow
            .Location = New Point(txtdscnt1.Location.X, ctlRowLocation)
            .Size = txtdscnt1.Size
        End With
        Me.Controls.Add(new_DSCNT)
        Dim new_SP As New TextBox '// ========== New Control =========
        With new_SP
            .Name = "txtSP" & ctlRow
            .Location = New Point(txtsp1.Location.X, ctlRowLocation)
            .Size = txtsp1.Size
        End With
        Me.Controls.Add(new_SP)

        btnNext.Location = New Point(btnNext.Location.X, ctlRowLocation)

    End Sub
End Class

NOTES:
..I added Locations and Sizes for each Control created when your application starts.
..Also, I had to add code to Frm_Load in order to add the "Original" Controls to your Form.
..And finally, I updated code in btnNext_Click to use only the location.X of the Control it cloned AND keep the same size.

Overall Summary for your recent post:
..You did not have pre-set Locations and Sizes for your original Controls located at the top of the code.
..AND those controls were never added to your Form.

Final Note:
..You might need to add a .Name for the original controls, if you plan to located them later. Should be similar to adding .Name to the Controls created from btnNext.Click.

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.