Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrrgh! Trying to reference procs in another form

Status
Not open for further replies.

CraigBest

Programmer
Aug 1, 2001
545
US
This is driving me nuts.

I have 2 forms. I want to call a proc on the first form from the second form. I know that part of the idea is to name the form.proc to find it, but this isn't working for me. I don't really understand why.

In VB6, I was able to send the calling form (form1) as an argument in the procedure call for the proc in Form2 and have it appear as an object. Then when I needed to call a proc on form1, I could just do this:

frmParent.ProcName

and it worked fine.

This does not work in .NET, at least I can't seem to figure out how to explain to the IDE what the called form is and find the proc in it. Trying to reference procs in other forms has been a huge problem for me.

Can anyone help me find a way to make this work?

Thanks





CraigHartz
 
You can create a class file and put your proc in it, then just reference the proc in the class file from each form
 
Not bad, but I could have dumped the proc into a code module IF it weren't for the fact that the proc references controls on the form, so I'm back where I started. AFAIK, the proc can't be in a separate code module or a class because it gets back into I can't reference the form to read the values of the controls or call a proc in it.

There has got to be a way to do this but I just don't understand how this works. It's so very different from VB6 on a very low level. I know one of thse days I'll have a eureka moment and understand it, but for now I'm just confused.


CraigHartz
 
you could create the proc in the class file and pass the form controls to the sub as needed
 
To reference another form, you need to have an object in the calling form set to the instance of the form to be called.

Form1 (Form with proc to be called):

Public Class Form1
Inherits System.Windows.Forms.Form

Public Sub DoSomeCoolStuff()

End Sub

End Class




Form2 (form doing the calling):

Public Class Form2
Inherits System.Windows.Forms.Form

[red]Public FormToCall as Form1[/red]

Public Sub DoSomeCoolStuff()

End Sub

End Class

Note that the type of FormToCall is Form1.

When you create an instance of Form2 from Form1, set the value of FormToCall (in Form2) to the curent instance of Form1:

Dim frm2 As New Form2
[red]frm2.FormToCall = Me[/red]
frm2.Show

Form2 now has a reference to Form1, via the FormToCall object. You would call the proc from Form2 like this:

FormToCall.DoSomeCoolStuff()

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day! Ye has a choice: talk like a pira
 
All right, that sounds like it will work. It even makes sense! I will give it a try, thanks.


CraigHartz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top