Search for items in a list

0

Hello, I need to search for items from a list, I have a Client class

 public class Cliente 
{
    public int ClienteId { get; set; }
    public string RazonSocial { get; set; }
    public EnumDocumentoEdentidad DocumentoIdentidad { get; set; }
    public string NumeroDocumento { get; set; }
    public string Direccion { get; set; }
    public string Telefono { get; set; }

    public virtual  ICollection<Venta> Ventas  { get; set; }
}

I have controls in a Windows Forms form which are called as follows txtClienteId , txtRazonSocial , txtDocumentoIdentidad . So what I want to look for is the following:

As you will see the controls are called the same as the properties except they have txt, cbo, etc in front of them at the beginning. I need to do a search on behalf of the control if the property is called SocialResearch that searches all controls RazonSocial and if it finds it put the content of txtRazonSocial to the property RazonSocial , in other words assign the value to the attribute.

 public void Guardar(Form frm)
    {
        ObtenerValorControles(frm);
        List<ControlEntity> result = ObtenerNameControles(frm);

        Control obj = new Control();
        obj.GetAllControls();

        var controles = from b in frm.GetAllControls()
            where b is TextBox || b is ComboBox || b is CheckBox
            select b;

        if (nameForm == "Cliente")
        {
            var prop = new Cliente();

            foreach (var propertyInfo in prop.GetType().GetProperties())
            {
                if (propertyInfo.Name = result.Contains(prop))
                {

                }

            }
        }

I get the name of the controls

 public List<ControlEntity> ObtenerNameControles(Form frm)
    {
        nameForm = Convert.ToString(frm.Tag);

        Control obj = new Control();
        obj.GetAllControls();

        var controles = from b in frm.GetAllControls()
            where b is TextBox || b is ComboBox || b is CheckBox || b is Form
            select b;

        foreach (var control in controles)
        {
            if (control is CheckBox)
            {
                nameCheck = ((CheckBox) control).Name;
            }
            if (control is ComboBox)
            {
                nameCombo = ((ComboBox) control).Name;
            }
            if (control is TextBox)
            {
                nameTextBox = ((TextBox) control).Name;
            }


            if (!string.IsNullOrEmpty(nameCheck))
            {
                var n = new ControlEntity() {Name = nameCheck};
                listNombreControles.Add(n);
            }
            if (!string.IsNullOrEmpty(nameCombo))
            {
                var n = new ControlEntity() {Name = nameCombo};
                listNombreControles.Add(n);
            }
            if (!string.IsNullOrEmpty(nameTextBox))
            {
                var n = new ControlEntity() {Name = nameTextBox};
                listNombreControles.Add(n);
            }
        }
        return listNombreControles();

    }
    
asked by Pedro Ávila 16.10.2016 в 00:35
source

2 answers

1

Instead of programming a magic binding because you do not use the existing one, I think that in the time that it will take you to program this you could finish the binding of all the forms

Entity Framework in WinForms

If you analyze the title Creating a Form View (With an EntityBindingNavigator) you will see how it performs DataBinding of the properties of the model to control the form.

    
answered by 17.10.2016 в 19:31
1

The truth is that the code can be simplified quite a lot by using reflection, I tried to alter as little as possible your code to achieve it in the way you want.

public void Guardar(Form frm)
{
    ObtenerValorControles(frm);
    List<ControlEntity> result = ObtenerNameControles(frm);

    Control obj = new Control();
    obj.GetAllControls();

    var controles = from b in frm.GetAllControls()
        where b is TextBox || b is ComboBox || b is CheckBox
        select b;

    if (nameForm == "Cliente")
    {
        var prop = new Cliente();

        foreach (var propertyInfo in prop.GetType().GetProperties())
        {
            if (propertyInfo.Name == result.Contains(prop))
            {
                var prop2 = GetControlByName(propertyInfo.Name).GetType().GetProperty("Text");//TODO:Agregar una relación para el tipo de campo
                    prop2?.SetValue(GetControlByName(propertyInfo.Name), cliente.GetType().GetProperty(propertyInfo.Name).GetValue(cliente,null), null);
            }

        }
    }

}

In my example I have hardcodeado the Text property, if you want you can extend it and make it more flexible.

Greetings

    
answered by 17.10.2016 в 19:43