passing forms as arguments

  • Thread starter Thread starter dennist
  • Start date Start date
D

dennist

In VB6 I was able to define a public form gForm. In some
form, I'd set gForm = Me. In a procedure afterwords I
had access to all tha forms controls, e.g.
gForm.txtTitleText.Text.

There seems to be no such ability in WindowsForms. If I
call a prcedure I need to include Me as a form argument.
For example, cls1.sub1(me), where Me = Form1, for
instance.

In sub1 I need the argument to say,
PublicSub1(frm as Form1)

This can be cumbersome. I can only see a tremendous
overloaded constructor. If 10 forms might be calling
that procedure the process is out of hand.

Is there a more economical way of doing this in
WindowsForms. vb.net seems a step backward from vb6 as a
RAD tool. It's even worse in ado.net.

dennist
 
dennist said:
In VB6 I was able to define a public form gForm. In some
form, I'd set gForm = Me. In a procedure afterwords I
had access to all tha forms controls, e.g.
gForm.txtTitleText.Text.

There seems to be no such ability in WindowsForms.

It's not part of Windows Forms at all - it's the same as anything else.
You can have a relatively global variable using a public shared
variable, although it's not necessarily a very good idea.

Is there a more economical way of doing this in
WindowsForms. vb.net seems a step backward from vb6 as a
RAD tool. It's even worse in ado.net.

I suspect it's just a case of lack of familiarity really.
 
jon, thank you for your reply. It may be a lack of
familiarity, but it surely isn't because a lack of trying.

as you're an MVP, perhaps you can provide me with a
solution.

thanks,

dennist
 
dennist said:
jon, thank you for your reply. It may be a lack of
familiarity, but it surely isn't because a lack of trying.

Oh certainly.
as you're an MVP, perhaps you can provide me with a
solution.

Well, I've already suggested one - you can store a reference to the
form in a public shared variable (or property backed by a private
variable) if you want the same behaviour as you used in VB6.

It really depends on exactly what these methods you're calling are
meant to be doing though - there may well be better design solutions.
What kind of thing are you calling?
 
thank you again, jon,

Here's the ugly situation. I'm 100 percent disabled from
a spinal injury. Got on the wrong bus in 1996. I'm in
constant pain so have to take percoset, a narcotic, 3
times a day. Doesn't keep me sharp. Besides this, I
take a muscle relaxant, clonix, 3 times a day. It's a
cousin of valium. In addition, I take a huge amount of
anti-spastic medicine called baclosal. So much so that
my liver function is checked once a month. So far its
normal, Baruch Hashem. I can go on, but you get the
idea. There's not a whole bunch of joy in my life, but a
certain project is one of them.

Could you do me a favor and actually show me an example
of passing a reference to a form in a public shared
variable? My main interest is access to the properties
of controls in classes. Not just textboxes, but datetime
pickers, comboboxes, listboxes. It'd be even better if I
didnt have to have a constructor that accounts for each
form. Or maybe there's a typeof statement that could
tell me which form it is. That'd not be ideal, but much
faster. You mentioned your solution would be as easy as
vb6. Oh, I'd love if you'd show me how, explicitly.

In hopeful anticipation,

dennist.

By the way, it's 2 am here. I'm up because baclosal
notwithstanding, I'm so spastic that I can't sleep.
 
Hi dennist,

I think you can still write code as vb6, just define a global Form variable.
and you can check the type using the CType Function as
<code>
Module Module1
Public gForm As Form
Public Sub Demo()
If TypeOf gForm Is Form1 Then
Dim f1 As Form1 = CType(gForm, Form1)
MessageBox.Show(f1.TextBox1.Name)
End If
End Sub
End Module

//In form1.vb
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Module1.gForm = Me
End Sub
</code>

In this way, maybe you can reduce the number of overload functions, however
it makes the function very big, since you need handle all cases in one
function.

Could you let me know why you need pass in the form reference and access
the controls directly?

Thanks!




Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
You should not reply this mail directly, "Online" should be removed before
sending.
 
You can also pass the type as it's base type and then cast from there
instead of setting global variables.

Public Class Class1
Public Sub PublicSub1(ByVal frm As Form)
If (TypeOf frm Is Form1) Then
Dim frm1 As Form1 = CType(frm, Form1)
frm1.TextBox1.Text = "type is Form1"
End If
End Sub
End Class


It sounds like your class should be raising an event that the form could
then listen for. This would clean up the code nicely. Here's a simple event
sample:

Form1:
Dim WithEvents cls1 As Class1 = New Class1()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
cls1.PublicSub1(Me)
End Sub
Private Sub cls1_UpdateForm(ByVal Text As String) Handles cls1.UpdateForm
TextBox1.Text = Text
End Sub

Class1:
Public Class Class1
Public Event UpdateForm(ByVal Text As String)
Public Sub PublicSub1(ByVal frm As Form)
'do some work
RaiseEvent UpdateForm("event raised")
End Sub
End Class


HTH;
Eric Cadwell
http://www.origincontrols.com
 
Back
Top