I have a one form with code that has lots of subs and was wondering if i can shift some of these subs to a class or module to make the main form coding less clutered.


4 Years
Discussion Span
Last Post by poojavb

The short answer is yes. If you provide more information then perhaps a longer answer would be possible.

Edited by Reverend Jim


Yes u can move ur code in some other module and then directly call the function or so....

like database connections could be in different modules....
the user inputs like u need a text or a number can be in different modules....

creating modules is very good step as if the code is reusable no need to enter the complete code....one u create a module just calling the function will be fine....


My code is used for the datagridview so a calculation can be made on form load. Whenever i try to shift this code to a module or class i get a error message of object not found

Thanks for any help

Public Sub StartUpData()

    DG1.RowCount = 2
    DG2.RowCount = 1
    DG3.RowCount = 10
    DG4.RowCount = 5

    DG1.DefaultCellStyle.SelectionBackColor = Color.White
    DG1.DefaultCellStyle.SelectionForeColor = Color.Black
    DG2.DefaultCellStyle.SelectionBackColor = Color.White
    DG2.DefaultCellStyle.SelectionForeColor = Color.Black
    DG3.DefaultCellStyle.SelectionBackColor = Color.White
    DG3.DefaultCellStyle.SelectionForeColor = Color.Black
    DG4.DefaultCellStyle.SelectionBackColor = Color.White
    DG4.DefaultCellStyle.SelectionForeColor = Color.Black

    With DG1.Rows(0)
        .Cells(0).Value = True
        .Cells(1).Value = "Jan"
        .Cells(2).Value = 2000
        .Cells(3).Value = 1.00
    End With

    With DG1.Rows(1)
        .Cells(0).Value = True
        .Cells(1).Value = "Feb"
        .Cells(2).Value = 2000
        .Cells(3).Value = 2.59
    End With

    With DG1.Rows(2)
        .Cells(0).Value = True
        .Cells(1).Value = "March"
        .Cells(2).Value = 2000
        .Cells(3).Value = 5.36
    End With

    End Sub

In ur module along with the control names u need to mention the form names with it...

Form1.dg1.Datasource = dt .... 'somthing like this....else the module wont understand its is referring to which object...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.