C# creating a property in a Usercontrol

Asked

Viewed 980 times

2

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

I tried it this way:

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

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

1 answer

2

Syntax error I believe: Inverts the Set cboLista and value and take the tests!

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

Good a hint when doing this should concern you with methods and actions and so your usercontrol should have these features programmed. When I used this in Web I programmed some actions that I was going to use usercontrol.

Reinforcing what I said the programming should go to the object and then you should use Usercontrol as reference for some settings, I’ll leave an example:

The event Resize, we can configure the size of the combo according to the size of the UserControl dynamically in that 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 combo size this way:

inserir a descrição da imagem aqui

So, just to conclude, it can be dynamic, but the programming must be done on the Father object

Another example:

Create in your UserControl a code like that:

public ComboBoxStyle ComboSytle
{
    get
      {
          return comboBox1.DropDownStyle;
      }
    set
      {
         comboBox1.DropDownStyle = value;
      }
}

By placing the UserControl in his Form it will enable this image equal configuration below:

inserir a descrição da imagem aqui

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

  • I realized the mistake, but it still didn’t work out the way I wanted it to. I can only change the properties of the combobox through the code, for example: cboTeste.Combo.DropDownStyle = ComboBoxStyle.DropDownList; In visual mode the property Combo appears in the properties tab, I make the property change Dropdownstyle I see on the screen the modification (still in visual mode), but when executing these modifications do not appear. But we can already continue the work using code, so identify what the problem for visual mode I put here.

  • 1

    "mas ao executar aplicação estas modificações não aparecem" This looks like property being set at some point in the code. The debugger is your friend.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.