|
-
Jan 23rd, 2004, 12:18 PM
#1
Thread Starter
Lively Member
Accessing subroutines from another form
Hello,
I was wondering if there's a way to access a subroutine from another form?
Before in VB6, all you need to do is form2.cmdClear_Click()
or something..... is there a vb.net equivalent to this?
Thanks,
Chris
-
Jan 23rd, 2004, 12:31 PM
#2
Frenzied Member
If I understand correctly, what you want to do is pretty easy. If it's a shared (static) method, you can just call it without creating an instance of that object first. If it's not a shared method, you need an instance of the object before you can call one of it's methods.
-
Jan 23rd, 2004, 01:37 PM
#3
Frenzied Member
This code is in Form2:
VB Code:
Shared Sub Hello1()
MessageBox.Show("Hello1")
End Sub
Sub Hello2()
MessageBox.Show("Hello2")
End Sub
This code is in Form1:
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form2.Hello1()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim f As New Form2
f.Hello2()
End Sub
You can call a shared method without first creating an instance of Form2, but if the method is not shared, you need an instance first. If you tried to call Form2.Hello2(), you'd get an error that says reference to a non-shared method requires an object reference.
HTH,
Mike
-
Jan 23rd, 2004, 02:28 PM
#4
This small project has an example of how to call a form. Its not exactly what your looking for per se, but you can probly work things out looking at it. You need to learn the differences between object instances and declarations, its one of the major differences between vb6 and .net and had been covered many many times in these forums.
http://www.vbforums.com/attachment.p...postid=1600084
btw, try searching.
-
Jan 24th, 2004, 08:08 AM
#5
PowerPoster
Hi,
I think we all may be answering different questions here.
If you want to access procedures or other routines in Form2 from Form1, then, unless they are declared as Shared,you must ensure that you have instanced Form2. However, I do not think you can declare an object's event as Shared and achieve this. Remember that in .NET a form does not exist in your application until you have created an instance of it. This was not so in VB6.
If you are looking for the method to access the Click event of another object it is:
form2.cmdClear.PerformClick
but remember that you must create an instance of Form2 before you can do this.
Last edited by taxes; Jan 24th, 2004 at 08:49 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 24th, 2004, 10:04 AM
#6
Addicted Member
Yeah this is easy. You can do something like:
VB Code:
Public Shared Function AccessForm1Name(ByVal sender As Object) As String
Return "The Caller was " & sender.GetType.ToString & " Accessing " & "Form1"
End Function
Put this in form1, and call it from form2. By using sender in the args you can determine what class called the method.
-
Jan 24th, 2004, 12:21 PM
#7
PowerPoster
Hi Jason,
How would you apply this to activate the Click event of a button?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 24th, 2004, 12:24 PM
#8
Sleep mode
If it's not eventhandler (related to any control) , then put it in a module or class file shared flaged .
-
Jan 24th, 2004, 12:45 PM
#9
Addicted Member
just change the button click sub to a Public Shared Sub:
VB Code:
Public Shared Sub Button1_Click(ByVal Sender as Object, ByVal e as System.EventArgs) Handles Button1.click
End Sub
Now you can simulate the button being clicked from another form.
-
Jan 24th, 2004, 03:23 PM
#10
PowerPoster
Hi Jason,
Sorry but I can't get this to work. What code do you place in the calling procedure/event?
I tried:
form2.button1_click
and
form2.button1.performclick
without success.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 24th, 2004, 04:31 PM
#11
Addicted Member
OK place following code in Form1 and replace the word Button1 with whatever your button name is:
VB Code:
Public Shared Sub Button1_Click(ByVal Sender as Object, ByVal e as System.EventArgs) Handles Button1.click
End Sub
In Form2 use this in your procedure:
-
Jan 24th, 2004, 07:51 PM
#12
PowerPoster
Hi Jason,
I have already done all that.
When I enter In Form2 procedure:
form1.Button1_Click
I get the intellisense comment;
Argument not specified for parameter 'e' of 'Public Shared Sub Button1_Click(Sender as object, e as system.EventArgs)'.
On running, a build error as above is displayed together with a second build error of:
: Argument not specified for parameter 'sender' of 'Public Shared Sub Button1_Click(sender As Object, e As System.EventArgs)'.
but the application will continue and the startup form appears. The procedure in form 2 does NOT implement the Click method of the button in form1.
HOWEVER, something very funny is happening!!
I tried putting a Public Shared sub in form1 which placed a string in a textbox on form2 and called it from form2. This, of course, worked perfectly BUT I then deleted that sub and tried again. The string which WAS included in the form1 sub still appeared in the form2 textbox - even though all the code had been deleted. I tried rebuilding the projectand also exiting VB.NET completely but this behaviour continues. It was just as though I have never removed the code at all! Also, although I could amend the base form2 by adding additional text boxes and buttons, these additions would not appear in the instantiated form2 (named form22) at runtime.I eventually got round this by deleting the calling sub completely and then re-entering it. This restored the expected behaviour, including the proper appearance of the instantiated form2.
Have you ever run into this?
Many thanks for your help.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jan 24th, 2004, 08:59 PM
#13
Addicted Member
My bad bro, its Form1.Button_Click(me,nothing)
God! I cant believe i had you going in circles on that. Well, guess you learn a little bit more with every problem.
me = the object that is calling the function, in this case it would be the form class name in which you placed that function call.
nothing - Means you are not sending any event args with the call.
Sorry.
-
Jan 25th, 2004, 07:33 AM
#14
PowerPoster
Hi Jason,
Got it!!!
Thanks a million for your effort.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|