Solved

Modularizing in VB .NET

Posted on 2004-08-02
10
685 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 70

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 70

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
 
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 70

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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