How to access and edit the properties of the controls contained in a UserControl? C #

0

I did a userControl that contains a Devexpress grid (DevExpress.XtraGrid.GridControl). What I want to do is access the properties of that grid in a Windows Form.

Is there any way to do this?

Greetings

    
asked by Nahuel Picca 22.06.2017 в 14:38
source

2 answers

2

To access the properties of the user control controls you should expose these values through properties of the user control.

For example, imagine that you define a user control ( MiUserControl ) with a button ( botonEnUserControl ) inside it. You could make it possible to read and modify the button text from outside the user control through a TextoDelBoton property. Or that you could check the width of the button through a property AnchoDelBoton .

public partial class MiUserControl : UserControl
{
    public MiUserControl()
    {
        InitializeComponent();
    }

    public string TextoDelBoton
    {
        get
        {
            return botonEnUserControl.Text;
        }
        set
        {
            botonEnUserControl.Text = value;
        }
    }

    public int AnchoDelBoton => botonEnUserControl.Width;

}

This way if you add an instance of your user control to a form (for example MiUserControl1 ) you could access these values through

 MiUserControl1.TextoDelBoton

or

 MiUserControl1.AnchoDelBoton
    
answered by 22.06.2017 / 15:41
source
2

There is no directly, it is assumed that the purpose of a User Control is to abstract the functionality it implements, that is why it does not allow access to the properties of the controls contained in

If you need to configure the Grid you will have to expose one by one those properties that you need

public class UserControl1 : UserControl
{
   //resto codigo

   public GridColumn Columns 
   {
      get { return XtraGrid1.Columns; }
      set { XtraGrid1.Columns = value; }
   }

}

The same applies to all the properties of internal controls that you want to expose outside.

If the user control contains only the grid, you may have to evaluate creating a Custom Control

    
answered by 22.06.2017 в 16:34