简体   繁体   中英

Edit controls in custom control - c# winforms

在此处输入图片说明

I am trying to change the background color of the text box of a custom control (Custom control name is 'tc1' and text box name is 'txtb') by clicking the 'Change' button that is not in the custom control. How should I tackle this problem?

Thanks, Wayne Scicluna

Option 1:

You can make your TextBox public. to do so, go to designer of your user control, then select Modifier property and the set the value to public.

Now you can access it outside of your control. For example if you have an instance of your user control with name of userControl1 and name of your text box is txtb :

userControl1.txtb.BackColor = Color.Red;

Option 2:

As another option you can create a public property and in that property get or set your textbox color.

public Color TextBoxBackColor
{
    get
    {
        return txtb.BackColor;
    }
    set
    {
        txtb.BackColor = value;
    }
}

Now you can access this property outside of your control. For example if you have an instance of your user control with name of userControl1 :

userControl1.TextBoxBackColor = Color.Red;

Since you are building a user control, changing the color of the textbox can be treated as a feature of the control and can be provided to clients by adding a property.

[Browsable(true)]  // allows you to change it in the designer
public Color TextBoxBackColor
{
    get { return txtb.BackColor; }
    set { txtb.BackColor = value; }
}

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