I don't know how to explain this more clearly than I can.

The codes in my form are getting too many/long. How do you split the codes into another? and still having the same classes and events in order to reduce the amount of codes in the form.

Sorta like using two monitors(form1/form2) in one PC, the second monitor(form2) acts as an extension.

Example

Form1

Form2? Module?
Private sub Form1 Load
End Sub

Recommended Answers

All 9 Replies

Add a module file to your project and change the declaration of your subs and functions to public in the module file. And if your coding in the seperate file is naming something specific on your form such as Label1.Text =; you need to specific in the module the name of the form such as Form1.Label1.Text = ""

As for variable declaration, in a module form you want to use the keyword Public instead of Dim, if you want the variable values to be seen from other forms.

hmm could be even easier.
if you have functions that you need to call from different forms you can do two things:
hand over the control you want to write, edit or w/e
in example

Friend sub setLabelText (byval lbl as Label)
'do some calculations here
lbl.text=calculation
End Sub

'in your form you then call
setLabelText (Label1)

or you create functions that returns values

Friend Function setLabelText () As string
'do some calculations here
return calculation
End Function

'in your form you then call
Label1.Text=setLabelText

on this way you dont need this confusing Form1.textbox1.text stuff
Another thing....the functions can be declared as Friend (which means known inside your application)

The functions and subs are okay, how about events such as button click?

to be honest i would keep the control events in the code file of the form. else you will get into trouble to find the events if you have to edit something.
instead of exporting the whole event why not exporting the content of the event to the module?

I agree, the control events are specific to that form therefore they should stay in that form. However the code within the events can be broken up into sub or functions. The subs and functions can be in the same for or a module file.

Button1_Click
Call Sub1
Call Funtion2
Call Sub3
End Sub

If the sub or function you define is specific to the one form, then I would code it within that form. If the sub/function could be used by multiple forms then you should put it into the module file, so that all forms can call that sub/function and you dont have to write the same coding more then once.

Also you can use the Region method to help group and organaize your coding better within any of the files. Type #Region followed by the name you want to use within double quotes. Then you can move some of the coding subs/functions/events into each of the Regions you define. You can then expand or collapse the region by clicking on the + or - sign shown.

#Region " Form Button Events "

Button1_Click...

Button2_Click...

Button3_Click...

End Region

Thank you for replying.

Like I said, my code window contains too many codes. I am trying to reduce it by separating categories of codes such as exporting all button events to another window.

OMG Region events!, I never knew VB.NET had such. I guess I still have a lot to learn. This is what I am looking for in the first place. I thought you can only collapse bodies of methods. I guess this is embarrassing.

Thanks Tom and Geek.

hmm could be even easier.
if you have functions that you need to call from different forms you can do two things:
hand over the control you want to write, edit or w/e
in example

Friend sub setLabelText (byval lbl as Label)
'do some calculations here
lbl.text=calculation
End Sub

'in your form you then call
setLabelText (Label1)

or you create functions that returns values

Friend Function setLabelText () As string
'do some calculations here
return calculation
End Function

'in your form you then call
Label1.Text=setLabelText

on this way you dont need this confusing Form1.textbox1.text stuff
Another thing....the functions can be declared as Friend (which means known inside your application)

In reading this post, I noticed that one of the main things being overlooked is the use of objects.

Objects (classes) are 'self contained' units of work, and is used throughout the .NET framework.

If the code used in your form is becoming long and unmanagable, I will suggest you begin looking at your UI (User Interface) differently than you may have in the past.

A good way of doing this, is simply seperate out each of the pieces of the user interface, and instead of drawing controls on the form, create and use User Controls. A user control can easily be created in a seperate file and one or more individual form controls can be placed within it.

As you design this control, understand it will be placed on the form, so to get your control to communicate back to your form, do this by creating your own Public Events within your control. This will allow for a very easy Object Oriented approach to allowing your projects to encapsulate individual functionality in an Object Oriented way.

If you'd like an example of this, simply let me know.

In reading this post, I noticed that one of the main things being overlooked is the use of objects.

Objects (classes) are 'self contained' units of work, and is used throughout the .NET framework.

If the code used in your form is becoming long and unmanagable, I will suggest you begin looking at your UI (User Interface) differently than you may have in the past.

A good way of doing this, is simply seperate out each of the pieces of the user interface, and instead of drawing controls on the form, create and use User Controls. A user control can easily be created in a seperate file and one or more individual form controls can be placed within it.

As you design this control, understand it will be placed on the form, so to get your control to communicate back to your form, do this by creating your own Public Events within your control. This will allow for a very easy Object Oriented approach to allowing your projects to encapsulate individual functionality in an Object Oriented way.

If you'd like an example of this, simply let me know.

Thank you for your brilliant suggestion, and yes I would like to have an example.

I have created a thread based on your method. http://www.daniweb.com/forums/post992180.html#post992180

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.