Read data between parent and child form

  • Thread starter kevininstructor
  • Start date
K

kevininstructor

I have a ListBox on the main form, in the child form when accessing items I
get an error message "Object reference not set to an instance of an object."
here is the code in the child form. Is there something I am missing i.e.
setting the reference to the main form to access ListBox items?

Public Class frmWinZipExecution
Inherits System.Windows.Forms.Form

Public MainForm As frmMainForm ' For accessing calling form

....

Private Sub cmdBuild_Click(ByVal sender As System.Object,....
Dim iFileCount As Integer
Dim iFile As Integer

Try
iFileCount = MainForm.ListBox1.Items.Count
For iFile = 1 To iFileCount - 1
Debug.WriteLine(MainForm.ListBox1.GetItemText(iFile))
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub


Thanks for any help you can provide,
Kevin
 
C

Cor Ligthert

Kevin,

This instruction
\\\
Public MainForm As frmMainForm ' For accessing calling form
///

This tells that MainForm is from the type (format) frmMainForm, it is not
even instanced.

What are you up to, because probably your problem is easier to tackle like
this (I find it not nice, however when you want it).

In your mainform
\\\
dim frm as new SecondForm
frm.mylistbox = me.listbox
frm.showdialog
///
And than in the other form
\\\
public mylistbox as listbox
////

I hope this helps

Cor
 
K

kevininstructor

Cor,

Yes I understand what you are saying. Usually as I code questions like the
one asked here help me to better understand how things run in WinForms. So I
am still looking how to properly access the main form from the current child
form.

Thanks for you assistance,
Kevin
 
K

kevininstructor

Found the solution I was hunting for;


In the main form

Dim f As New frmWinZipExecution
Try
f.MainForm = Me
f.ShowDialog(Me)
Finally
f.Dispose()
End Try


In the child form

#Region "Access Main Form Code"
Public Property MainForm() As frmMainForm
Get
Return mMainForm
End Get
Set(ByVal Value As frmMainForm)
mMainForm = Value
End Set
End Property
#End Region

....

Me.MainForm.ListBox1.Items.Add("Added from Child form")


Guess I would ask if there are any problems with this code.


Credit for above code can be found at
http://www.dotnetsmart.com/forums/993/ShowPost.aspx
 
C

Cor Ligthert

Kevin,

It is the same as the code I showed in a different way.

What you do is passing the reference of that listbox.

Now I know you use that showdialog, than it is easier (and better as well as
my first sample) to do it in this way.

\\\
f.ShowDialog(Me)
///
And in your showdialog form by instance
\\\
Dim str As String = DirectCast(Me.Owner, Form1).ListBox1.Items(0).ToString
///

I hope this helps ?

Cor
 
K

kevininstructor

Thanks again Cor for your help...sorry I didn't mention the "ShowDialog"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top