Calling a function from another form

  • Thread starter Kevin Humphreys
  • Start date
K

Kevin Humphreys

Hi There,
I have 2 forms and I need to execute a subroutine on formA from FormB
passing specific parameters. Can this be done using VB.NET?
I would prefer not to use a global function. Just something local to 2
forms.

Thanks,
Kevin.
 
T

Tim Mackey

hi kevin,
since the forms are objects, it is a simple matter of ObjectB having a
reference to ObjectA, which can be done with any object-oriented programming
language. In other words, you need a variable of type ObjectA contained in
Object B, initialised to the instance of ObjectA that you want to invoke the
method on.

in depends on which way your forms are created, but you need to ensure that
ObjectB contains a correctly initialised variable for ObjectA.

if you need help setting it up, post the code that you use to create both
the forms (and your main() code if either A or B are your main form). here
is some pseudo code anyway:

function main()
dim form1Object as new form1() 'create a form1
dim form2Object as new form2() 'create a form2, but don't show it yet
form1Object.Form2Reference = form2Object 'set the reference to
form2Object
form1Object.showdialog()
'now inside form1 you can do things like:
me.Form2Reference.whatever(whatever)
end function

your form 2 should look like this:

class form2
..... other variables here etc
public form2 Form2Reference

end class

there are some design patterns like Model View Controller which have a
technique for achieving this.

hope this helps
tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top