Newbie: trying to run one forms event from another form.

  • Thread starter Thread starter Rico
  • Start date Start date
R

Rico

Hello,

I'm trying to run an event in form1 from form2. The code I have is similar
to this;

Dim RunFrm as form1

RunFrm.form1ActiveateEvent()

But I keep getting an "Object is not set to an instance of an object" error.
Any help would be greatly appreciated.

Thanks!
 
Sorry, I should have mentioned, the form is already open and was used to
call this form. Do I still need to add "New" to the declaration? IIRC I
tried that and still had the same problems, but I'll give it another shot.
 
Hi Rico,

Before you can Raise an event in a class, you have to instantiate the
class. If the class (a form in this instance) is already running,
you'll need to gain access to that instance.

Even If you changed your code to :

Dim RunFrm as New form1()
RunFrm.form1.ActivateEvent()

By this code, you're creating a new instance of Form1 (which you
mentioned is already running.) What you probably want to do instead, is
to access the instance of the Form1(running) and activate the event in
that instance.

The way is to access the instance of the already running form is to
create a shared instance of Form 1 that is accessible from your
"RunFrm".

Try this way :

This code goes in Form1 :

#Region " Global variables "

' Variables which store an instance of Launched form and of This form.
Private varMyNewForm As RunFrm
Public Shared thisForm As Form1

#End Region

In the Sub that Launches the New form (RunFrm)

'Create a variable to store this instance of Form 1
thisForm = Me
varMyNewForm = New RunFrm()
varMyNewForm.Show()
Me.Hide()

Now we're coding in RunFrm : (Remember "thisForm" refers to the already
open, but hidden instance of Form 1)

thisForm.ActivateEvent()

That should do it.

Don't forget to again show your main form in the Closing event of
RunFrm.

HTH,

Regards,

Cerebrus.
 
Hmmm, I'm wondering if I'm doing something wrong. in the Parent form
(thisForm) I've created the following;

Public Shared frmTForm as thisForm

in the second form, if I try to reference frmTForm then I get a "type
frmTForm is not defined".

To compound things, I'm trying to use a static dataset in the parent or
"calling" form and populate it with various dataviews entered in the new
form.

Any direction would be appreciated.

Thanks!
 
Hi Rico,

Did you include this part of the code I suggested, somewhere ?
=======================================
In the Sub that Launches the New form (RunFrm)

'Create a variable to store this instance of Form 1
frmTForm = Me

=======================================
This code gives a value (Me) to the Public Shared variable that you
created.

Now to access that shared variable in the 2nd form, you must fully
qualify it : (It is a public variable of the 1st Form)

thisForm.frmTForm.Show()

If it still gives an error, try appending the Namespace name (Usually
project name) to the statement as :

ProjectName.thisForm.frmTForm.Show()


As far as your DataSet is concerned, use the same procedure (making it
Public Shared) and then update it's contents in the new form as :

thisForm.frmTForm.MySharedDataSet.Tables.Add(MyDataTable)

When you come back to the parent form, this shared Dataset will contain
all the DataViews you added.

Hope this helps,

Regards,

Cerebrus.
 
Alright, well I got the first part working (running the event from teh
second form). The rows of the table in the dataset don't seem to be saving.
when I run the following code;

Private Sub RicksTest_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim dv As DataView
Dim dr As DataRow
Dim dt As DataTable

dgSerialNumbers.Select(0) ' (this is a data grid on the RicksTest form

dv = dgSerialNumbers.DataSource

For Each dr In dv.Table.Rows

With VendorReceiveOrder.StaticDs.Tables(0).NewRow()

.Item(0) = dr.Item(0)
.Item(1) = dr.Item(1)
Console.WriteLine(.Item(0) & " - Adding - " & .Item(1))


End With

Next

Console.WriteLine(VendorReceiveOrder.StaticDs.Tables(0).Rows.Count)

'The following npart doesn't run at all since there are no rows

For Each dr In VendorReceiveOrder.StaticDs.Tables(0).Rows
Console.WriteLine(dr.Item(0) & " - static - " & dr.Item(1))
Next

End Sub



The first Console.writeline is showing the values are being set, but when I
get down to the second console.writeline, the rows are zero and the second
for/Each doesn't run because of that.



ANy ideas?



Rick
 
DOH! Never mind, I got it. I realized I'm not adding the row back in.

Thanks for your help!

Rick

"Rico" <r c o l l e n s @ h e m m i n g w a y . c o mREMOVE THIS PART IN
CAPS> wrote in message news:3tKLf.67781$B94.5725@pd7tw3no...
 
Back
Top