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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Values between Classes 2

Status
Not open for further replies.

keen2know

Programmer
Sep 27, 2002
9
GB
I am a .Net novice.

I am building a Windows Forms application and using modal forms to force control to the displayed form.

I want to access data from the calling form and I am not sure how to pass information since the form that is called is another class - I have resourted to writing information to a file in the intrum and reading it in from the other form.

Is there parameter passing between classes or do I have to implement an interface? If I implement an interface will I have full access to datasets etc which have been loaded on the calling form?

Many thanks

JJ
 
I call a Sub or Method with parameters in the form, let it show itself modally then have it return the parameters. emember, it is just another class. Some never think to let the form display itself modally.
..... Form 1
Dim objForm as New Form2

objForm.GetStuff (p1, p2)

....Form 2
Dim mstr1 as string ' value for p1
Dim mstr2 as string ' value for p2

Public GetStuff (p1 as string, p2 as string)
mstr1 = p1 ' Copy IN arguments
mstr2 = p2
me.ShowDialog ' Waits for HIDE, Visible=false etc
p1 = mstr1 ' Copy OUT arguments
p2 = mstr2
End if
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thanks for the insight. It is very helpful.

I have now got about 7 forms and don't want to try and re-write the application(at least in the short term). It also seems like a nice way to break up all the code (in seperate classes). I take it from your response that there is no way to pass parameters to an EXTERNAL class (aside from writing to a file as I am currently doing). If I could pass a reference to an existing dataset for example, that would be great.

In another example, I want to pass back a flag to the calling form to tell it to refresh a dataset. At the moment I can't do this without checking a file every time I return to the calling form...

Thanks for your help

Regards

JJ
 
John,

I have just re-read your reply and finally gathered what it does. Thing is if I use the approach you suggest (calling the original form again), I will be performing all the initialisation code again (heavy SQL usage) just to pass a value, when the other form is still memory resident with the values there, but not attainable. Isn't there some way of referencing it?

Thanks

JJ

 
In your new form, have a public variable. Example: intReceived.

When you call the new form, do something like:

Dim f2 as new Form2()
f2.intReceived = localVariable

OR

f2.dataSet1 = localDataSet

THEN

f2.Show
 
Where do you see From 1 being called anywhere? Public variables are not as good as passing arguments.

Create form2 object
Form1 passes arguments to GetStuff SUB in form2.
GetStuff stores input parameters in form (class) level variables.
Form2 shows itself modally and stops executing in the middle of Getstuff until form2 becomes invisible due to HIDE, CLOSE or VISIBLE=FALSE.
Form2 events like click etc still run. mstr1 and mstr2 are updated with user input or whatever.
Some user action like OK button makes code HIDE form2 so that execution in GetStuff will continue after the ShowDialog. mstr1 an mstr2 are copied back over the passed arguments.
End Sub causes a RETURN to form1 right after the call to Getstuff.

Just a plain old method call to another class.


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
John, maybe you have more insight on to what he is trying to accomplish than me, but the only thing I was seeing is that when he creates an instance of another form and shows it, he wants the modality on the new form.

He said he was a newbie, and I was just offering another solution. In my way, if his form was hidden, the variables would still remain the same until the form was destroyed as well.
 
The
me.ShowDialog in form2
puts the modality on the new form.

In the way I have coded it, Intellisense helps you see the parameters.

Here is a complete example of a modal form that pre-fills a textbox with passed data and returns what the user types in if the OK button is pressed. It is a simple Method that just happend to stop forn user nput just like an input box.

I moved my code up front.

Code:
Public Class Form2
    Inherits System.Windows.Forms.Form
    Private mstrText1 As String
    Public Function GetText(ByVal DefaultText As String) As String
        TextBox1.Text = DefaultText
        Me.ShowDialog()
        Return mstrText1
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mstrText1 = TextBox1.Text
        Me.Hide()
    End Sub

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(6, 3)
        Me.TextBox1.Name = &quot;TextBox1&quot;
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = &quot;&quot;
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(12, 30)
        Me.Button1.Name = &quot;Button1&quot;
        Me.Button1.TabIndex = 1
        Me.Button1.Text = &quot;OK&quot;
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TextBox1})
        Me.Name = &quot;Form2&quot;
        Me.Text = &quot;Form1&quot;
        Me.ResumeLayout(False)

    End Sub

#End Region
    End Class
Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Guys,

Thanks very much for your help.

Sorry I was a bit slow.

JJ
 
Yeah John, I can see how one could even use your method, or a slightly modified version thereof, to pass a variable back to the calling form if you also wanted to do that. Its a pretty good method to use. I'll give you a star.
 
Hi (again),

I am struggling trying to reference a text box on another form.

in MS access it was simply formname.controlname.text

if I try that in vb.net I get a error stating [formname]is not defined.

its doing me Heeed in! can someone help?
 
From one newbie to another :)

If you add a module (Add new item..module) to your project and within that module u type something like

Friend Withevents refOwner as Form1

and then in the form1.load event u type

refOwner = Me

U can then display alle form2, form3, form4s that u like and in their code u can access the form1 variables by typing

refowner.(variablename)

unless they are private. Perhaps not a beautful way to do it, but it works :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top