How do you call Subs/Functions inside a User-Control?

Modules and Forms does not recognize the public subs/functions inside a user control.

Recommended Answers

All 12 Replies

There is no such restriction in VB.NET I believe.

You can call public methods of Classes and Modules inside a UserControl.
You need to call them the way you are calling the public methods of Classes and Modules in a Form.

I think you can activate another button control and functions in VB.Net.
Here , this is my way ...

Public Class Form1
    Dim a As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "button clicked "
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        test(a)
    End Sub
    Public Function test(ByVal a)
        TextBox1.Text = "by function  "
    End Function

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Button1_Click(AcceptButton, e)
    End Sub
End Class

[This is in vb.net]

Modules does not recognize the function inside a User-Control
Error: Declaration Expected
As if the function does not exist.

1. Method is Public
2. Spelling is Correct

Error: Declaration Expected
Which Declaration Expected ??
I think you missed Dim a As String
And this is working for me [ i am using vb2008 ]

Put a breakpoint and test else please carefully look for the error !
Best of luck ......

Error: Declaration Expected
Which Declaration Expected ??
I think you missed Dim a As String
And this is working for me [ i am using vb2008 ]

Put a breakpoint and test else please carefully look for the error !
Best of luck ......

Not your code but my code.

I am trying to call the function from a module, the function is located inside a User-Control.
http://img33.imageshack.us/img33/3659/20271916.jpg

Ok. So you are trying to call a function in a Module/Form which is defined in a User Control.

You should not call the public method of an UserControl in a Module.

To call the function, you need to instantiate the user control in the module. For example,

Dim  uc1 As UserControl1
 uc1.myFunction()

But this is not the right approach as the user control is GUI component where as the Module is reusable business component.

lets say you have created a usercontrol named myTest then you do this in your module

dim myControl as myTest
myControl.myFunction

functions and Subs you want to call from outside the usercontrol have to be declared as Friend not Private
and the usercontrol have to be initialized already before calling any functions/subs

lets say you have created a usercontrol named myTest then you do this in your module

dim myControl as myTest
myControl.myFunction

functions and Subs you want to call from outside the usercontrol have to be declared as Friend not Private
and the usercontrol have to be initialized already before calling any functions/subs

Sorry for the late reply.

It seems that it does not work.

please give me more details. do u get an error? how looks your code like?

please give me more details. do u get an error? how looks your code like?

Thank you for replying

Error:
NullReferenceExemption was unhandled
Object reference not set to an instance of an object

means the usercontrol is not initialised yet. so do

dim myControl as New myTest
myControl.myFunction

means the usercontrol is not initialised yet. so do

dim myControl as New myTest
myControl.myFunction

Thank you very much for your time

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.