Newby Question!

  • Thread starter Thread starter Carl Howarth
  • Start date Start date
C

Carl Howarth

Hello,

I think I'm being a bit daft but I have an app with 2 forms.... I select a
record from the first form which opens a new window and shows the record
which can then be updated.

Once updated I want the second form to close and the first form to be
updated. I have a sub procedure on the first form (Public) that I want to
call prior to closing the second form, and it won't let me simply do
frName.Procedure().... as I would expect...

Help! I know it should be so simple but am not getting any joy!

Thanks in advance,

Carl Howarth
 
Carl Howarth said:
Hello,

I think I'm being a bit daft but I have an app with 2 forms.... I select a
record from the first form which opens a new window and shows the record
which can then be updated.

Once updated I want the second form to close and the first form to be
updated. I have a sub procedure on the first form (Public) that I want to
call prior to closing the second form, and it won't let me simply do
frName.Procedure().... as I would expect...

Help! I know it should be so simple but am not getting any joy!

When you create the second form, pass the reference to the first form
(eg this (C#) or Me (VB.NET) if you're creating the second form from
the code of the first form) to the constructor for the second form. The
second form can then keep that reference to the first form in an
instance variable, and use it later when it needs it.
 
If I understand where you're coming from, you're trying to treat the forms
as a "VB 6" style forms where a single form has a global identity you can
reference. In .NET, there's no such intrinsic global identity.

You have four choices (really three probably) that I can think of off the
top of my head to approach this.

1) Pass the "to be updated" form as an argument to the second form and
invoke the method from the 2nd form.
2) Make the first form more VB6-esque by making an instance of it available
as a shared/static member somewhere in your project (static keyword in C#,
Shared keyword in VB.NET)
3) Open the 2nd form with ShowDialogue. When it exits, check the dialogue
result from the main form and update appropriately.
4) If the 1st form's method you are calling doesn't act on instance data,
you can make the method you are trying to call a Shared method. Shared
methods don't require instances.
 
Hi Jon,

Does this mean that I do 'f.ShowDialog(Me)'?

If so, how then do I access the 'me' from the secondary form?

Many thanks, Carl
 
Hi Jon,

Does this mean that I do 'f.ShowDialog(Me)' in the example of point 1?

If so, how then do I access the 'me' from the secondary form?

Many thanks, Carl
 
Carl Howarth said:
Does this mean that I do 'f.ShowDialog(Me)'?

That's not what I meant, no - but that would suggest another solution,
where you'd use the Owner property in the other form to get a reference
to the original form. However, that only works if you want to show the
second form as a modal dialog.

I was suggesting that where you create the instance of the other form
(not where you show it) you pass another parameter ("Me") to the
constructor, and then in the constructor you'd copy that parameter's
value to an instance variable.
 
Does this mean that I do 'f.ShowDialog(Me)' in the example of point 1?

Overload the constructor of your form2 to take an instance of form1. Then
when you create form2, pass in a reference to form1 (this is quick and
dirty code):

'---------------Form1----------------------------
Private Sub SomeSubThatCallsForm2()

'Create an instance of form2 passing an instance of form1 (Me)
Dim MyForm2Instance As New Form2(Me)

'Show it
MyForm2Instance.Show

End Sub



'---------------Form2----------------------------

'Private Form1 instance variable
Dim m_MyForm1 As Form1

'Overload the constructor to take an instance of form 1
Public Sub New(f As Form1)
MyBase.New
'Assign the reference to the private variable
m_MyForm1 = f
End Sub


Public Sub SomeMethodThatManipulatesForm1FromForm2()
'Do something with a textbox on m_MyForm1
m_MyForm1.TextBox1.Text = "Some text value"

'similar code here
End Sub
 
Back
Top