Map windows Forms controls

Asked

Viewed 64 times

1

I am developing an application descktop in c# The framework 4.5

I did not imagine that the application could grow and have many fields, with this came the need to make an automatic mapping (from / to) of the controls to entities...

Example.

public Pessoa GetPessoaForm()
{
    //Obtém todos os controles de um formulário
    IEnumerable<Control> controles = frm.GetAll();

    Pessoa pessoa = new Pessoa();

    var prop = typeof(pessoa).GetProperties();

    foreach ( Controle control in controles)
    {  
        foreach (var p in prop)
        { 
            If(!p.Name.ToLower().Equals(control.Tag.ToString().ToLower())
                continue;

            If (typeof(control).Equals(typeof(TextBox))
                Control.Text = p.GetValue(prop, null); //erro também
        }
    }
}

This way I would like to map to the form and vice versa. From the Entity Form.

  • What error does it give you when you try to assign the value?

  • Null object, maybe you are trying to rescue the property wrong, there is some framework to do this kind of automatic mapping?

  • Search MVVM or MVP frameworks for Winforms. I don’t know if you have anything good available...

  • The problem should be the time and place at which this method is called. Usually the controls are created only when the form is initialized. In addition, if the form contains panels or other types of 'containers', you will have to create a recursion in this loop that iterates the form controls in order to check all your child controls.

  • @Linq good dirty, if demanded in the web world, maybe there is in the world desk

1 answer

1


Put the class Pessoa to notify changes in ownership (through interface INotifyPropertyChanged), as follows:

class Pessoa : INotifyPropertyChanged
{
    private string nome;
    public string Nome
    {
        get { return nome; }
        set
        {
            nome = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("Nome"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }
}

And then you can use the Binding straightforward:

foreach (Controle control in controles)
{  
    foreach (var p in prop)
    { 
        if(!p.Name.ToLower().Equals(control.Tag.ToString().ToLower())
            continue;

        if (typeof(control).Equals(typeof(TextBox))
            // supondo que a classe Pessoa tem uma propriedade "Nome"
            Control.DataBindings.Add("Text", pessoa, "Nome");
    }
}

Thus the value of the property Nome class Pessoa will be directly linked to the text of TextBox.

  • Seems to me a great option, I see any way to leave the code to the dynamic ones. I will check.

Browser other questions tagged

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