471,624 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,624 software developers and data experts.

Read data between parent and child form

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(iFil e))
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub
Thanks for any help you can provide,
Kevin
Nov 21 '05 #1
5 1662
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
Nov 21 '05 #2
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

"Cor Ligthert" <no************@planet.nl> wrote in message
news:O6**************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #3
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
Nov 21 '05 #4
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

Nov 21 '05 #5
Thanks again Cor for your help...sorry I didn't mention the "ShowDialog"
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by news.microsoft.com | last post: by
1 post views Thread by KentG | last post: by
reply views Thread by Jake Wiley | last post: by
reply views Thread by Hardik Shah | last post: by
3 posts views Thread by Sheikko | last post: by
7 posts views Thread by Darin | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.