p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   Addressing form from module (http://p2p.wrox.com/showthread.php?t=17443)

MKri August 26th, 2004 01:10 AM

Addressing form from module
 
Please, help me with this problem:
I need to address the text box on the form from the module procedure.
But not on the form known by name, but generally - on the "form from which the module procedure was called".
Thank you in advance.
MK


bmains August 26th, 2004 07:45 AM

Hey,

Why not pass the textbox in as a parameter to the module?

Brian

katsarosj August 26th, 2004 04:07 PM

Put this in your form:

Friend Shared tbNewTextbox as Textbox = New Textbox
-------
In your form load event:

tbNewTextbox = [TextBoxName]

-------
In your module:

[FormName].tbNewTextbox.[xxxx]

See if this helps.

J

MKri August 30th, 2004 08:48 AM

Thanks, but form name is the real problem.
How to pass the form name to the module and how to address
back the form from the module?


bmains August 30th, 2004 02:39 PM

Hey,

I wouldn't pass it the form name, but a reference to the form itself. Just remember, the Form is a control like everything else (inherits from the Control class), and can be referenced as a variable.

Brian

MKri September 5th, 2004 08:40 AM

Thanks, but can you tell me explicitly -
how to "pass ... a reference to the form itself ..." from the form to the module procedure, so that the module procedure adresses back the
form (enters certain number to the form's text box).


jaucourt September 6th, 2004 03:37 AM

Quote:

quote:Originally posted by MKri
 Thanks, but can you tell me explicitly -
how to "pass ... a reference to the form itself ..." from the form to the module procedure, so that the module procedure adresses back the
form (enters certain number to the form's text box).



Right - make the form you're interested in a parameter in the module method, e.g.
Code:

Public Sub FormMethod(ByRef refFrm as Form1)
    ' do stuff with refFrm here...
End Sub

Then back in the form, say when someone presses a button:

Code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' call the module method, passing in this form as a parameter...
    FormMethod(Me)
End Sub


MKri September 12th, 2004 06:09 AM

Thanks, but it is valid for Form1 only and the whole point is that it should be valid for more forms, which all have text boxes txt1, txt2, txt3 and txt4. Otherwise it would be no need to put that procedure in the separate module.


bmains September 13th, 2004 07:16 AM

Hey,

When you open a form, you typically do:

objForm = New Form1
objForm.Show()

Now say you want to pass in a reference to the current form (pass in a reference of the currently opened form to Form1, you could do:

objForm.DoSomethingWithForm(Me)

That function would be defined as:

Public Sub DoSomethingWithForm(ByRef objForm As Form1)
  'code here
End Sub

Also, I like to set the owner property, which is the reference to the owner form. So what I do is:

objForm = New Form1
objForm.Owner = Me
objForm.Show()

And in the Form1 code, you reference the owner.

Brian

MKri September 14th, 2004 09:17 AM

Thanks for your answer, but I'm not sure I understand it. Can you please apply it on my specific problem:

I have forms frm1, frm2 ... frm20. They are opened one at a time.
All of them have 4 text boxes: txt1, txt2, txt3 and txt4.
All of the forms have a procedure proc() which does the following:

txt1.Text = "a"
txt2.Text = "b"
txt3.Text = "c"
txt4.Text = "d"

Now I want to avoid having that procedure in each one of the 20 forms. I want to have it only once and it would probably be in a
separate module.
So, I need an opened form to be able to call that procedure in that
module and get it's text boxes filled with "a", "b", "c" and "d".

In VBA you would simply put procedure

Sub proc()
CodeContextObject.txt1 = "a"
CodeContextObject.txt2 = "b"
CodeContextObject.txt3 = "c"
CodeContextObject.txt4 = "d"
End Sub

in a separate module and call it from any of the forms:

Sub btn1_Click()
Call proc
End Sub

and the text boxes would get their values.

So, how to do it in VB.NET?

Thanks in advance.









All times are GMT -4. The time now is 01:50 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.