How do I write a event for form1 in a module+vb.net 2005

Hi,

I want to write a form event example form1_load in a  'abc' module.

Cheers
RIASAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DjDezmondCommented:
Its not very clear what you mean?

You want an event within your form, that when raised some code in your abc module will execute?
0
RIASAuthor Commented:
Is it possible to have a form event outside a form?


Cheers
0
nepaluzCommented:
Sub New
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Jorge PaulinoIT Pro/DeveloperCommented:
You can do this (for example) for Click Event
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Me.Click, AddressOf FormClickEvent
    End Sub

End Class

Module abc

    Public Sub FormClickEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("ok")
    End Sub


End Module

Open in new window

0
DjDezmondCommented:
Yea. There are a few ways to do this (depending on what you need to do). The following example shows how to attach to an existing event within your form, from the "Program" class:

static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            frmEventTest EvTest = new frmEventTest();
            EvTest.Load += new EventHandler(EvTest_Load);

            Application.Run(EvTest);
        }


        static void EvTest_Load(object sender, EventArgs e) {
            //This will execute when your form has loaded.
        }

    }

Open in new window


This is the best way to attach to the form_load event. In the best equivelant of a module.
class clsEventTest {
        frmEventTest _parentForm;

        public clsEventTest(frmEventTest ParentForm) {
            _parentForm = ParentForm;

            _parentForm.Load += new EventHandler(_parentForm_Load);
        }

        void _parentForm_Load(object sender, EventArgs e) {
            //This wil execute just after your Forms_Load event.
        }
    }

Open in new window

0
RIASAuthor Commented:
The problem is I have not access to the form1 .So need to write a event in a module.

AddHandler Me.Click, AddressOf FormClickEvent

wont be possible as no access.

Cheers
0
DjDezmondCommented:
ignore that last code post... thats the way to do it from within a class that your form created, but couldn't work for a form_load event.
0
DjDezmondCommented:
So your in VB.Net, and you are trying to run this from a module?

   
'Module:
    Public Sub TestSub()
        'Some code here.
        MsgBox("Test")
    End Sub

'Form:
    Private Sub frmEventTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler Me.Click, AddressOf TestSub
    End Sub

Open in new window

0
RIASAuthor Commented:
Thanks for that.But as I do not have access to  frmEventTest_Load
Adding the code
 AddHandler Me.Click, AddressOf TestSub  

is not possible.Is there any other way?Sorry for being so complicated..

Cheers
0
DjDezmondCommented:
Any code inside a module is globally visible throughout that project. So adding event handlers from within your form is perfectly possible this way. If you are asking whether this is possible to add an event handler without adding any code into your form, then the answer is no.
0
DjDezmondCommented:
Why don't you have access to this form? If it is in your project, then you must be able to modify it. The only other way to add event handlers, is via the properties page within Visual Studio. Click on the lightening symbol to show all the possible events to handle...
0
DjDezmondCommented:
Sorry forgot to add:

You will need to have your form selected, so that it is displaying the properties for that form.
0
RIASAuthor Commented:
The reason I do not have access is that I am doing a customisation on the form which is not my department project.My department just have access to a customisation module.
Form_load is just an example given by me.But I need to have another event written in a module which is a component one true db grid.
I need to write a event fetchcelltip.

Cheers
0
Jorge PaulinoIT Pro/DeveloperCommented:
Here you have one way:

Application Events
 
Namespace My

    Partial Friend Class MyApplication

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            start()
        End Sub
    End Class

End Namespace

Open in new window


Then your module:

 
Module abc

    Public Sub FormClickEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("ok")
    End Sub


    Sub DefineEvents()
        AddHandler Form1.Click, AddressOf FormClickEvent
    End Sub

End Module

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DjDezmondCommented:
Ok, I could be wrong, but here is another solution to try...

It appears that VB works slightly different to C#, in the sense that it is not hugely "instance" driven. This means that there should be away to access the existing instances in memory. And I think you do it like this:

   
AddHandler TestVB.frmEventTest.Click, AddressOf TestEventSub

Open in new window


Replace "TestVB" with your project name, and that should work.

Now the next issue, is that if you are actually trying to access private controls within that form, then they will not be visible. You can overcome this (apparently), by knowing the control name, and using the publically accessible "Controls" collection:

   
AddHandler TestVB.frmEventTest.Controls("GridView1").Click, AddressOf TestEventSub

Open in new window

0
RIASAuthor Commented:
Cheers mate!!Really appreciate your help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.