Calling subs between forms

visualsteve
03-21-2006, 12:42 AM
Hello, I am new to VB .NET. I would like to call a picturebox1_click sub from a form2_keydown sub, how can I do this? Also, how can I pass variables between forms? I tried Public and Shared Dim's, but strangely that didn't work. Any assistance is appreciated.

cosmosis
03-21-2006, 02:33 AM
check out this thread

http://www.xtremevbtalk.com/showthread.php?t=257209

visualsteve
03-21-2006, 07:02 PM
I still don't understand! And it doesn't address variables. Thanks, but I may need some more help. I've declared "Public" variables in both forms and their values weren't retained. Do I also have to declare Public both forms in each other also?


I've declared the forms public, and the program was able execute when I called subs from that other form, but halted and gives the error:
Object variable or With block variable not set.
I don't understand.

cosmosis
03-22-2006, 12:43 AM
I still don't understand! And it doesn't address variables. Thanks, but I may need some more help. I've declared "Public" variables in both forms and their values weren't retained. Do I also have to declare Public both forms in each other also?


who said you should declare public variables in both forms? if you study the thread behind the link i gave you more carefully, you'd see that a Module is the way to go. Rightclick on your project, Add -> Add New Item... -> Module. In this module you should keep all subs, functions and variables that you want to share between forms






I've declared the forms public, and the program was able execute when I called subs from that other form, but halted and gives the error:
Object variable or With block variable not set.
I don't understand.

Again, put the declaration of the forms in the Module, and add an AppManager at the end your your startup form... like i said in the other thread

visualsteve
03-22-2006, 10:48 PM
I did this, cosmosis, but there's no "Startup Object" option under properties. So as far as I know I can't set the application mamager as the startup module. Is there another way?

I know I'm not calling the routine correctly.
I was saying "CALL Form1.yatayatayata" and form1.anything would give the same error. So, that wasn't right on my part.
How should one call a subroutine from another form?

cosmosis
03-23-2006, 02:34 AM
are you using vb2003?

did you make sure to add the AppMgr class BELOW all other code in the codewindow of your FORM (not Module)?
there should be a menu item (like File, Edit, View, etc..) called Project. the last item in there is called "NameOfYourProject Properties..."
Inside this, you should see Common Properties -> General -> Startup object (combobox)

visualsteve
03-23-2006, 06:21 PM
I'm using the 2005 Express Edition. Yes, I copied the App manager by itself under the startup form (form1). Okay, I unchecked something and the "Starup form" allowed me to set the App manager to startup. But it's like nothing happened. I don't know how to call the subroutine if it's from another form.
I type in from form2
Call PictureBox1_Click(sender, e)
But form2 also has a picturebox1. So that's pointless of me.
Anyways, it gives ther error this thing is undeclared.
I don't know what to do from this point.

Could you provide a call line that successfully calls a subroutine from another form? It should be simple, people probably do this in most projects. Thank you.

...it's worth noting that I can't do MainForm.Initialize().
".Initialize" isn't recognized, so I replaced it with ".Activate".

visualsteve
03-24-2006, 11:34 PM
Okay, I solved the problem. I must call a form1 function from form2 like "call form1.whatever", fairly simple. To pass variables, it appears as if the only way to keep values of the variables I want, I make a special function in form1 that accepts the variables I want that are passed from form2. I don't know for the life of me why variables declared "Public" aren't shared between forms. The Application manager I deleted, it wasn't helping and it seems I don't need it.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum