Setting Text on Different Form

F

Fred

I have two forms, the first having a label and a button that opens the
second form. The second form has a button that I want to set text in
form1's label before closing.
I have no problem getting the 2nd form to show, but when I click the
2nd form's button to set the text in the 1st form's label, I get the
following message:
Object reference not set to an instance of an object.
What am I doing wrong????
 
Z

zacks

I have two forms, the first having a label and a button that opens the
second form. The second form has a button that I want to set text in
form1's label before closing.
I have no problem getting the 2nd form to show, but when I click the
2nd form's button to set the text in the 1st form's label, I get the
following message:
Object reference not set to an instance of an object.
What am I doing wrong????

If the 1st form the application's main form? If so how is it shown? Is
it being shown because it is the project's startup object? If so, then
the reference to the form will not be known to any other class in the
project. If the application's startup object is a Sub Main, and the
form is shown by invoking the Application.Run method in the Sub Main
and passing a reference to the main form to the Applicaiton.Run
method, then it becomes a global reference where any other class in
the project can access it, and any Public or Friend object in it.
Controls in VB are declared as Friend by default.
 
S

ShaneO

Fred said:
I have two forms, the first having a label and a button that opens the
second form. The second form has a button that I want to set text in
form1's label before closing.
I have no problem getting the 2nd form to show, but when I click the
2nd form's button to set the text in the 1st form's label, I get the
following message:
Object reference not set to an instance of an object.
What am I doing wrong????

In Form2, prefix the label with the Form Name -

Form1.Label1.Text = "Hello"


ShaneO

There are 10 kinds of people - Those who understand Binary and those who
don't.
 
F

Fred

If the 1st form the application's main form? If so how is it shown? Is
it being shown because it is the project's startup object? If so, then
the reference to the form will not be known to any other class in the
project. If the application's startup object is a Sub Main, and the
form is shown by invoking the Application.Run method in the Sub Main
and passing a reference to the main form to the Applicaiton.Run
method, then it becomes a global reference where any other class in
the project can access it, and any Public or Friend object in it.
Controls in VB are declared as Friend by default.
I have a Sub Main start-up w/ Application.Run(frmMain) as the means of
showing the first form. Once the frmMain form is shown, I am able to
show the second form with a button click event.
The second form has the button click event coded with:
frmMain.lblTitle.Text = "Form 2 Closed"
Me.Close

It is the frmMain.lblTitle.Text = "Form 2 Closed" that is identified
as the source of the error. This is driving me crazy!
 

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