C # creating a property in a UserControl

2

I have a project in C # and I created a usercontrol with two components: a combobox and a button . I need to create a property to change the properties of the usercontrol's combobox.

I tried the following way:

 public ComboBox Combo
 {
     get
     {
        return cboLista;
     }
     set
     {
        value = cboLista;
     }
  }

The property appears, I can make changes to the control but it does not save and when I run the application the combobox settings do not work.

    
asked by anonymous 20.08.2014 / 15:26

1 answer

2

Syntax error I believe: Invert in Set cboLista and value and run the tests!

public ComboBox Combo
{
    get
    {
       return cboLista;
    }
    set
    {
       cboLista = value;
    }
}

Well a tip when doing this should be concerned with methods and actions and why your usercontrol should have these features programmed. When I used this in Web I would program some actions that I would use by activating usercontrol .

The event Resize , we can set the size of the combo according to the size of the UserControl dynamically in this way:

private void UCCombo_Resize(object sender, EventArgs e)
{
    Combo.Width = this.Size.Width - 11;
}

When placing the object (UserControl) it will automatically leave the size of the combo as follows:

So,justconcluding,ithastobedynamic,butprogrammingshouldbedoneintheparentobject

Anotherexample:

CreateinyourUserControlacodelikethis:

publicComboBoxStyleComboSytle{get{returncomboBox1.DropDownStyle;}set{comboBox1.DropDownStyle=value;}}

ByputtingUserControlintoyourFormitwillenablethissettingasbelow:

And by changing the style the control will change this new setting as well.

    
20.08.2014 / 18:01