Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将值传递给用户控件?_C#_Winforms_User Controls - Fatal编程技术网

C# 如何将值传递给用户控件?

C# 如何将值传递给用户控件?,c#,winforms,user-controls,C#,Winforms,User Controls,我有一个windows窗体,我想向用户控件传递一个值。我以编程方式在winform中创建用户控件并设置一个值,但未设置该值。下面是我创建用户控件的代码: namespace AddPanel { public partial class Form1 : Form { public Form1() { int a = db.CamTable1s.Count(); InitializeCompon

我有一个windows窗体,我想向用户控件传递一个值。我以编程方式在winform中创建用户控件并设置一个值,但未设置该值。下面是我创建用户控件的代码:

namespace AddPanel
{
    public partial class Form1 : Form
    {

        public Form1()
        {

            int a = db.CamTable1s.Count();


            InitializeComponent();    

            DisplayImage(a);
        }


        private void DisplayImage(int rowNum)
        {    

            test nt = new test();
            nt.Location = new System.Drawing.Point(33, h);
            nt.Name = "test1";
            nt.usrID = "username";
            nt.Size = new System.Drawing.Size(408, 266);
            this.Controls.Add(nt);

        }


    }
}
namespace AddPanel
{
    public partial class test : UserControl
    {
        public string usrID { get; set; }

        public test()
        {
            InitializeComponent();
            //textBox1.Text = usrID;

        }

        public test(string Id)
        {
            InitializeComponent();

            usrID = Id;

            UCtextBox.Text = usrID;

        }


    }
}
我在测试用户控件中设置了一个名为nt.Name的变量,然后我只想在用户控件的文本框中显示它。以下是用户控件的代码:

namespace AddPanel
{
    public partial class Form1 : Form
    {

        public Form1()
        {

            int a = db.CamTable1s.Count();


            InitializeComponent();    

            DisplayImage(a);
        }


        private void DisplayImage(int rowNum)
        {    

            test nt = new test();
            nt.Location = new System.Drawing.Point(33, h);
            nt.Name = "test1";
            nt.usrID = "username";
            nt.Size = new System.Drawing.Size(408, 266);
            this.Controls.Add(nt);

        }


    }
}
namespace AddPanel
{
    public partial class test : UserControl
    {
        public string usrID { get; set; }

        public test()
        {
            InitializeComponent();
            //textBox1.Text = usrID;

        }

        public test(string Id)
        {
            InitializeComponent();

            usrID = Id;

            UCtextBox.Text = usrID;

        }


    }
}

显然,我不知道为什么这不起作用。有人能帮我吗?

即使在有绑定的WPF中,UI也不会自动获取对属性的更改(而不会引发
PropertyChanged
)。在WinForms的硬编码领域,它肯定不会出现

所以你有两个问题:

  • 您在调用中调用了默认构造函数,因此没有代码设置textbox的
    Text
    属性

  • 即使设置了文本,对属性的后续更改也不会传播到UI

  • 最简单的解决方案是在setter中运行UI更新:

    private string usrID;
    public string UserID //Correct style!
    {
       get { return usrID; }
       set
       {
          usrID = value;
          RCtextBox.Text = usrID;
       }
    }
    
    您还可以从setter调用一个方法,监听
    INotifyPropertyChanged
    和其他一些内容

    另一种方法是公开方法而不是属性:

    public string UpdateUserID(string newId)
    {
        usrID = newId;
        RCtextBox.Text = usrID;
    }
    

    您应该将传入usrId属性的值放入文本框

        public partial class test : UserControl
    {
        public string usrID 
        { 
            get{return _usrId;} 
            set
            {
                _usrId = value;
                  UCtextBox.Text = value;
            }
        }
    

    您没有使用构造函数
    测试(字符串Id)
    。您也从未在DisplayImage调用中使用变量
    rowNum
    。这是我在代码中保留的,如果我想将UCtextBox.Text=usrID;在代码的下面,我怎么能做到呢?@user3634308我不知道你的意思。你可以把那一行的副本放在任何你想放的地方。您只需要在setter中使用它,就可以将属性更改传播到UI中。我需要在代码的更深处使用usrID,但它不起作用。类似stream.login=usrID;我必须先创建流,然后设置用户ID。但这与设置文本框文本无关!当然,您必须先创建流,然后才能设置属性。没有看到问题中的代码(请只问一个新问题,但如果是NRE,不要麻烦,读一下:)我不明白你在问什么,或者出了什么问题。我知道这与我原来的问题没有任何关系,我只是在问