problem reading value from another Form

R

Rich P

In frmMain I added a public property which returns a label contained in
frmMain:

public Label LblCD
{
get
{
return lblCurDir;
}
}


I open the 2nd form, frm2, by clicking on a button on frmMain, and in
frm2 I have this code:

frmMain fMain;
private string GetLblText()
{
return fMain.LblCD.Text;
}

the error I get when I try to invoke this proc on frm2 is:

"Object reference not set to an instance of an object."

I was thinking of trying frmMain fMain = new frmMain();

but I then I would not be able to read the current text contained in
frmMain.lblCurDir.Text.

What is the correct way to read data from another form?

Thanks,


Rich
 
M

mick

Rich P said:
In frmMain I added a public property which returns a label contained in
frmMain:

public Label LblCD
{
get
{
return lblCurDir;
}
}


I open the 2nd form, frm2, by clicking on a button on frmMain, and in
frm2 I have this code:

frmMain fMain;
private string GetLblText()
{
return fMain.LblCD.Text;
}

the error I get when I try to invoke this proc on frm2 is:

"Object reference not set to an instance of an object."

I was thinking of trying frmMain fMain = new frmMain();

but I then I would not be able to read the current text contained in
frmMain.lblCurDir.Text.

What is the correct way to read data from another form?


By making it public. How are you trying to read it from frm2?

mick
 
A

Alberto Poblacion

Rich P said:
[...] in frm2 I have this code:

frmMain fMain;
private string GetLblText()
{
return fMain.LblCD.Text;
}

This won't work because fMain is null;
[...]
I was thinking of trying frmMain fMain = new frmMain();

This won't work, either, because now fMain pints to a NEW instance of
frmMain, so you can't recover the original text from lblCurDir.
What is the correct way to read data from another form?

You need to add some mechanismo to pass a reference to the first form
into the second form. One way to do it is to use the constructor of form2.
When you open form2 from frmMain, you can pass a reference in this way:

Form2 frm = new Form2(this); //Note the "this".
frm.Show();

In Form2, you add a constructor that accepts the parameter:

frmMain fMain;
public Form2(frmMain f) : this()
{
fMain = f;
}

In this way, the variable fMain is correctly initialied, and you can now
access your property as you intended:

private string GetLblText()
{
return fMain.LblCD.Text;
}
 
M

mick

mick said:
By making it public. How are you trying to read it from frm2?

Just to add, your frm2 will have to be able to reference your first form.
You can either do that from a public property or by passing it in when
you create frm2

Form frm2 = new Form(fMain)

and you would need to overload the frm2 contructor to take this ref.

mick
 
R

Rich P

Thanks. That is what I did end up doing.

frmMain fMain

public Form2(frmMain frm)
{
InitializeComponent();
fMain = frm;
}

Now it works. but now I am thinking that I don't really need the public
property I added in frmMain. Is there a purpose for adding custom user
defined properties to a Form object? How can I access the value of a
custom property on a Form - besides going through the constructor? If
the constructor on the 2nd form is the only way -- it seems redundant to
add custom properties to a Form. I only ask for my edification. Am I
missing something?

Rich
 
M

Mr. Arnold

Rich said:
In frmMain I added a public property which returns a label contained in
frmMain:

public Label LblCD
{
get
{
return lblCurDir;
}
}


I open the 2nd form, frm2, by clicking on a button on frmMain, and in
frm2 I have this code:

frmMain fMain;
private string GetLblText()
{
return fMain.LblCD.Text;
}

the error I get when I try to invoke this proc on frm2 is:

"Object reference not set to an instance of an object."

I was thinking of trying frmMain fMain = new frmMain();

but I then I would not be able to read the current text contained in
frmMain.lblCurDir.Text.

What is the correct way to read data from another form?

You pass frmmain, it's an object, in the constructor of form2 and
address the form an object.

You will have to put a constructor in form2 look it up use Google.

var form2 = new frmForm2(frmMain).

the constructor can use the object as Form or as Object.

If object then while in form2

var frmmain = (Form)object.

frmmain.someproperty

It's something like but close, as I have not used Windows forms for
awhile. It's been all Web the last couple of years.
 
M

Mr. Arnold

Rich said:
Thanks. That is what I did end up doing.

frmMain fMain

public Form2(frmMain frm)
{
InitializeComponent();
fMain = frm;
}

Now it works. but now I am thinking that I don't really need the public
property I added in frmMain. Is there a purpose for adding custom user
defined properties to a Form object? How can I access the value of a
custom property on a Form - besides going through the constructor? If
the constructor on the 2nd form is the only way -- it seems redundant to
add custom properties to a Form. I only ask for my edification. Am I
missing something?

The custom property would be for something like a List<T> that was
populated in form1, and you wanted to use the List<T> a public property
on form1 and use it in form2 without populating a new List<T> same data
in form2, as an example.

You can Linq query the List<T> that's setting in form1 from form2.

public List<Group> Groups {get; set;} // in form1.
 
P

Peter Duniho

mick said:
[...]
What is the correct way to read data from another form?

By making it public. How are you trying to read it from frm2?

Actually, no. Making internal class fields public is a poor way to
expose data from those fields. Much better would be to declare a public
property that retrieves the specific data of interest, without showing
the control instance to the client code.

Pete
 
M

mick

Peter Duniho said:
mick said:
[...]
What is the correct way to read data from another form?

By making it public. How are you trying to read it from frm2?

Actually, no. Making internal class fields public is a poor way to expose
data from those fields. Much better would be to declare a public property
that retrieves the specific data of interest, without showing the control
instance to the client code.

Yes it was a sloppy post. I actually meant through a property.

mick
 

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