karthikeyanTP
asked on
Modularizing in VB .NET
This is a VB.NET question. I would like to modularize code. I have a form with many "tab pages". Each tab page has many controls. I want to separate "code" of each tab in a separate file (.VB file for each tab).
For example, Form1 has tab1 and tab2.
tab1 has a textbox1 and textbox2
tabe2 has datagrid1 and datagrid2.
I would like to separate the code of each tab. Ideally, I would like to create "tab1.vb" (any file name) which populaties data for textbox1, textbox2.
Similarly, tab2.vb will have code for popualting the data grids.
Is there any way to modularize the code? The moment I refer a control name (textbox1) in "tab1.vb", it says the variable is undeclared. I tried putting Form1.textbox1 also. It didn't work.
Thanks
For example, Form1 has tab1 and tab2.
tab1 has a textbox1 and textbox2
tabe2 has datagrid1 and datagrid2.
I would like to separate the code of each tab. Ideally, I would like to create "tab1.vb" (any file name) which populaties data for textbox1, textbox2.
Similarly, tab2.vb will have code for popualting the data grids.
Is there any way to modularize the code? The moment I refer a control name (textbox1) in "tab1.vb", it says the variable is undeclared. I tried putting Form1.textbox1 also. It didn't work.
Thanks
This is really easy with .Net
Create form2.vb:
-This form will be hosted in a tabpage
-Set the form's FormBorderStyle to None
-Set the form's ControlBox to False
-Put any controls and any code you want
Create form1.vb
-This will be your master form
-This is a regular form
-Add a TabControl
-Add a Button
-In the Click event, add this code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As New Form2()
x.TopLevel = False
Me.TabPage2.Controls.Add(x )
x.Left = 100
x.Show()
End Sub
Create form2.vb:
-This form will be hosted in a tabpage
-Set the form's FormBorderStyle to None
-Set the form's ControlBox to False
-Put any controls and any code you want
Create form1.vb
-This will be your master form
-This is a regular form
-Add a TabControl
-Add a Button
-In the Click event, add this code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As New Form2()
x.TopLevel = False
Me.TabPage2.Controls.Add(x
x.Left = 100
x.Show()
End Sub
ASKER
Hai TRUENEUTRAL/ emoreau,
I am newbie to VB. What if I created those controls at design time (not at run time). To put it simply, this is what I tried in the Form1.
1) I created a button 'btnClick' and a label named 'Label1'.
2) click event is as follows:
Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
Dim t As New clickClass
t.setLabel()
End Sub
- The click class is
Public Class clickClass
Public Sub setLabel()
Form1.ActiveForm.Controls. Item.Label 1.text = "It works Now"
End Sub
End Class
But, I see an error at Form1.ActiveForm.Controls. Item. I know that I cannot use item without index. I don't know the index of Label1. Is there any way I can find the index of Label1t? or How do I refer Label1 from clickClass?
Thanks
I am newbie to VB. What if I created those controls at design time (not at run time). To put it simply, this is what I tried in the Form1.
1) I created a button 'btnClick' and a label named 'Label1'.
2) click event is as follows:
Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
Dim t As New clickClass
t.setLabel()
End Sub
- The click class is
Public Class clickClass
Public Sub setLabel()
Form1.ActiveForm.Controls.
End Sub
End Class
But, I see an error at Form1.ActiveForm.Controls.
Thanks
Are you trying to organize your code in a better fashion? VB.Net supports the concept of Code Regions that can be collapsed/expanded with a little +/- icon next to the region name.
To start a region you enter this alone on a line:
#Region "Say... Textbox event handlers"
Your code goes in here that matches the region description...in this example, textbox event handlers.
#End Region
You can have as many regions in your code as you want. This makes organizing the code much easier.
To start a region you enter this alone on a line:
#Region "Say... Textbox event handlers"
Your code goes in here that matches the region description...in this example, textbox event handlers.
#End Region
You can have as many regions in your code as you want. This makes organizing the code much easier.
You are talking of 2 different things:
1. original question = I want to separate "code" of each tab in a separate file (.VB file for each tab).
2. Other post = What if I created those controls at design time (not at run time).
Do you know what you want to do?
1. original question = I want to separate "code" of each tab in a separate file (.VB file for each tab).
2. Other post = What if I created those controls at design time (not at run time).
Do you know what you want to do?
ASKER
Hai Guys,
The question is same. Just I gave examples for it. Sorry for not explanining it properly. I just want to organize the code in a better fashion (I am not looking for the REGIONS to collapse/expand ). My project involves a lot of tab pages and controls. I just don't want to write a lot of lines of code in a single file.
I want to separate things out (like in the second comment). I tried the example in the second comment - clickClass, btnClick... etc.
I just want to refer the control Label1 from the "clickClass.vb". How do I do it?
Public Class clickClass
Public Sub setLabel()
'The following line of code won't work. what is the correct code?
Form1.ActiveForm.Controls. Item.Label 1.text = "It works Now"
End Sub
End Class
Remember I already did the following
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------
1) I created a button 'btnClick' and a label named 'Label1' in Form1.
2) click event is as follows:
Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
Dim t As New clickClass
t.setLabel()
End Sub
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- -------
Thanks
The question is same. Just I gave examples for it. Sorry for not explanining it properly. I just want to organize the code in a better fashion (I am not looking for the REGIONS to collapse/expand ). My project involves a lot of tab pages and controls. I just don't want to write a lot of lines of code in a single file.
I want to separate things out (like in the second comment). I tried the example in the second comment - clickClass, btnClick... etc.
I just want to refer the control Label1 from the "clickClass.vb". How do I do it?
Public Class clickClass
Public Sub setLabel()
'The following line of code won't work. what is the correct code?
Form1.ActiveForm.Controls.
End Sub
End Class
Remember I already did the following
--------------------------
1) I created a button 'btnClick' and a label named 'Label1' in Form1.
2) click event is as follows:
Private Sub btnClick_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick.Click
Dim t As New clickClass
t.setLabel()
End Sub
--------------------------
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hai emoreau,
Yep, it works for me.
Thanks,
Yep, it works for me.
Thanks,
I would like to note that this is a VB.NET forum. The code I posted creates controls at design time, not runtime. The code is tested and works fine.
I showed you how to get the name of the control you are referencing. It would be a simple matter to loop through the controls and look for a name.
If you are coming from the VB world, you should be reading some books on Object Oriented Design before stepping into this mess.
It sounds like you are trying to encapsulate each tab on the form. In my opinion, the best thing for you to do would be to create separate objects for each tab (controls and all) using panels or a similar object and deal with them in that manner.
I showed you how to get the name of the control you are referencing. It would be a simple matter to loop through the controls and look for a name.
If you are coming from the VB world, you should be reading some books on Object Oriented Design before stepping into this mess.
It sounds like you are trying to encapsulate each tab on the form. In my opinion, the best thing for you to do would be to create separate objects for each tab (controls and all) using panels or a similar object and deal with them in that manner.
ASKER
Hai TRUENEUTRAL,
Thanks for your comment. It will try to create objects as you said for each tab (I came from Java world). The idea of emoreau also works for me - having a master form and a separate form for each tab and hosting these tab page forms to master form.
I tired his idea and it works for me. I will also try yours.
Thanks again
Thanks for your comment. It will try to create objects as you said for each tab (I came from Java world). The idea of emoreau also works for me - having a master form and a separate form for each tab and hosting these tab page forms to master form.
I tired his idea and it works for me. I will also try yours.
Thanks again
Code snippet from form1
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(448, 285)
Me.Controls.Add(Me.Button1
Me.Controls.Add(Me.TextBox
'notice the controls are indexed in the order they are added
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
code snippet from form 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New tab
t.stuff()
t.grab()
End Sub
contents of tab.vb file
Public Class tab
Public Sub stuff()
Form1.ActiveForm.Controls.
End Sub
Public Sub grab()
'notice you can get the name of the control too if you need it
MsgBox(Form1.ActiveForm.Co
MsgBox(Form1.ActiveForm.Co
End Sub
End Class