i have three forms, mainform(which is the "MAIN") log-in and home form. when accessing buttons in the main form to enable it i did this. its property is set to public.

(this.Owner as Main).homebtn.Enabled = true;

after logging in homebtn is anabled, when clicked, the home form will show and it has a label. now what i wanna do is set the label.text="something" when homebutton is click.

what i did is this.

(this.Owner as homeform).Label1.text="something"

then error...

error is: Object reference not set to an instance of an object.

how do you control the label from mainform to homeform???:sweat:
thanks

Recommended Answers

All 3 Replies

You have no control with the name 'Label1' is what that means.

i got it.. thanks

thread.close();

A quick suggestion? It's not a good idea to make controls public as it makes these controls accessible outside of the program if you insist on having a Control be accessible out side of that Form i would suggest using Internal over public as internal makes the controls accessible outside of that form but not outside of your application. If you need to use the control because it is not in the same application Ex. from a Dll, I would suggest using a public method like

private Label label1 = new Label();
        public string LabelText
        { 
            get
            {
                return label1.Text;
            }
            set
            {
                label1.Text = value; 
            }
        }

Doing it like this ensures that only the text value of the label can be changed outside of the program and no other properties of the label can be changed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.