【问题标题】:How to set values of user control from class如何从类中设置用户控件的值
【发布时间】:2014-05-26 12:21:00
【问题描述】:

我有一个用户控件,上面有很多文本框。我将此用户控件添加到不同的项目中,当我在 UserControl 上编写每个属性时,我可以使用它。我想使用一个类来设置这个用户控件的文本框字段。这些是我的代码:

类:

namespace IEUserControl
{
     public class IEValue
    {        
        public  string IsEmriNo { get; set; }
        public  string Nevi { get; set; }
        public  string BrutKg { get; set; }        
        public string NetKg { get; set; }        
    }
}

用户控制:

namespace IsEmriUserControl
{
    public partial class UC_IsEmri : UserControl
    {
        public UC_IsEmri()
        {
            InitializeComponent();
        }

        //private IsEmriValue _isEmri;


        //public IsEmriValue isEmri
        //{
        //    get
        //    {
        //        return _isEmri;
        //    }

        //    set
        //    {
        //        _isEmri = value;
        //    }

        //}


        public string IsEmriNo
        {
            get { return txtIsEmriNo.Text; }
            set { txtIsEmriNo.Text = value; }
        }
        public string Nevi
        {
            get { return txtNevi.Text; }
            set { txtNevi.Text = value; }
        }
        public string BrutKg
        {
            get { return txtBrutKg.Text; }
            set { txtBrutKg.Text = value; }
        }
        public string NetKg
        {
            get { return txtNetKg.Text; }
            set { txtNetKg.Text = value; }
        }

    }
}

当我使用属性时,我可以设置文本框的值。但是我想用我的类设置我的文本框值。谁能给我一个使用类设置文本框值的示例?谢谢。

【问题讨论】:

  • 你想把文本框的值赋给 IEValue 类属性吗?
  • 谢谢,我的问题是 Sinatr 的回答。

标签: c# class user-controls


【解决方案1】:

制作这样的方法/属性

public IEValue IE_Value
{
    get
    {
        return new IEValue() {
            IsEmrino = txtIsEmriNo.Text,
            Nevi = txtNevi.Text,
            BrutKg = txtBrutKg.Text,
            NetKg = txtNetKg.Text
        };
    }
    set
    {
        txtIsEmriNo.Text = value.IsEmrino;
        txtNevi.Text = value.Nevi;
        txtBrutKg.Text = value.BrutKg;
        txtNetKg.Text = value.NetKg;
    }
}

【讨论】:

  • 非常感谢,正是我需要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多