简体   繁体   中英

Changing form control from user control

I'm trying to changing a user control informations (labels, pictures etc.) from auto added user control. But i cant do it.

Here's my code;

        private void KitapButton_Click(object sender, EventArgs e)
        {
            BıtıkForm BForm = new BıtıkForm();

            BForm.kitapGoruntuleme.Visible = true;
        }
public partial class BıtıkForm : Form
    {
        //create controls public instance
        public Label label;

        public BıtıkForm()
        {
            InitializeComponent();

            //initialize the control 
            label = new Label();
        }
    }

Now you can access it from other place like;

BıtıkForm BForm = new BıtıkForm();

BForm.label.Visible = true;

/////// But my Suggestion do not do it like that instead do it like below ///////

BıtıkForm BForm = new BıtıkForm(controlVisible);//Pass the bool value as parameter to the constructor of form
BForm.Show();

And then in form

public partial class BıtıkForm : Form
        {
            public BıtıkForm(bool controlVisible)
            {
                InitializeComponent();

                //Set Control Visibility
                someControl.Visible = controlVisible;
            }
        }

I didn't use C# too much but It's eventually object oriented. The mistake I made is; I was creating a new instance of 'BıtıkForm' everytime event fired. It could be solved by adding new property where event belongs, and property will carry 'BıtıkForm' object. So It can be managed trough all over the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM