Solved

Modularizing in VB .NET

Posted on 2004-08-02
10
670 Views
Last Modified: 2010-08-05
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




0
Comment
Question by:karthikeyanTP
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 5

Expert Comment

by:TRUENEUTRAL
ID: 11699035
In tab1.vb, you must include fully qualify the control name.

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.TextBox1)
'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.Item(1).Text = "test"


      End Sub

      Public Sub grab()
'notice you can get the name of the control too if you need it
            MsgBox(Form1.ActiveForm.Controls.Item(1).Text)
            MsgBox(Form1.ActiveForm.Controls.Item(1).Name)

      End Sub

End Class
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 11699465
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
0
 
LVL 2

Author Comment

by:karthikeyanTP
ID: 11699879
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.Label1.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
0
 
LVL 14

Expert Comment

by:ptakja
ID: 11699911
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.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 11699934
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?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Author Comment

by:karthikeyanTP
ID: 11700139
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.Label1.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
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 50 total points
ID: 11700258
Your difficulty if you come from the VB6 world is that all forms and controls were usable without problems.

In .Net, you need to create specific instance of forms (dim MyForm as Form1) and be sure that controls are Public (the Modifiers property). Only then you can tell something like MyForm.Label1.Text = "New value"

It is not a good practice to have class talking or using forms directly.

I really think the way to go in your case is to create forms that will be use in your tabpage like the example I gave you earlier.
0
 
LVL 2

Author Comment

by:karthikeyanTP
ID: 11700367
Hai emoreau,


Yep, it works for me.

Thanks,


0
 
LVL 5

Expert Comment

by:TRUENEUTRAL
ID: 11700528
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.  
0
 
LVL 2

Author Comment

by:karthikeyanTP
ID: 11703488
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
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now