0

I have below model class in c# window application. I have form controls which I want to assign values to model properties after form validation. I have many more controls and many controls are the same. So instead of validating each control separate I want to loop the same controls and validate then in for loop.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TShopLibrary
{
    public class CustomerModel
    {
        public int id { get; set; }
        public string Name { get; set; }
        public string Contact1 { get; set; }
        public string Contact2 { get; set; }
        public string RefContact { get; set; }
        public decimal ShirtLength { get; set; }
        public List<ShirtBottomTypeModel> ShirtBottomType { get; set; } = new List<ShirtBottomTypeModel>();
        public decimal Sleeve { get; set; }
        public decimal Shoulder { get; set; }
        public decimal Chest { get; set; }
        public decimal ShirtBottom { get; set; }
        public decimal ShalwarLength { get; set; }
        public decimal ShalwarWidth { get; set; }
        public decimal ShalwarBottom { get; set; }
        public decimal ShalwarBottomOpening { get; set; }
        public List<NeckTypeModel> NeckType { get; set; } = new List<NeckTypeModel>();
        public decimal ChestPlateLength { get; set; }
        public decimal ChestPlateWidth { get; set; }
        public decimal NeckWidth { get; set; }
        public decimal NeckHeight { get; set; }
        public decimal Pocket { get; set; }
        public List<SewingTypeModel> SewingType { get; set; } = new List<SewingTypeModel>();
        public decimal Comments { get; set; }
        public decimal CreatedDate { get; set; }
        public decimal UpdtedDate { get; set; }

        public CustomerModel()
        {

        }

    }
}

and below is my form code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TShopLibrary;

namespace TShopUI
{
    public partial class AddCustomerForm : Form
    {
        private CustomerModel customermodel = new CustomerModel();
        public AddCustomerForm()
        {
            InitializeComponent();
        }

       

        private bool ValidateForm()
        {
           
        }

        private void CustomerSaveButton_Click(object sender, EventArgs e)
        {

            if (ValidateForm())
            {

            }
        }

        private void CustomerNameTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

In my ValidateForm() I want some thing like below.

foreach (Control ctrl in Controls.OfType<TextBox>())
            {
if(ctrl.Text!="")
{
 customermodel[ctrl.Name] = ctrl.text;
}
                
            }

In the above all is good. The problem is at

customermodel[ctrl.Name] = ctrl.text;

2
  • 1
    Hi, "This piece is not working." such a sentence is usually the beginning of a question cascade to find out all the necessary information to get a clear picture of your problem. Please be so kind save us the time and elaborate on this fact. What did actually happen? Commented Sep 16, 2020 at 7:25
  • on the first glance: to be able to use the [..] on a custom object like in this line: customermodel[ctrl.Name] = ctrl.text; you would need to override the indexing operator. Since I don't see that piece of code in your CustomerModel class. This should lead to a compilation error. This compilation error can be researched in your favourite search engine. Commented Sep 16, 2020 at 7:27

1 Answer 1

1

You can do this:

var props = typeof(Control).GetProperties();
foreach (var p in props)
{
    var value = p.GetValue(control_instance);
    if (p.Name == "Text")
        Do stuff....
}
Sign up to request clarification or add additional context in comments.

1 Comment

It might be better to first check if it matches and then getting the value of the instance to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.