2

I have a windows form and I want to pass a value to a user control. I programmatically create the user control in the winform and set a value, but it doesn't get set. Here is the code where I create the user control:

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);

        }


    }
}

I set a variable I made in test user control called nt.Name, then I just want to display it in a text box on the user control. Here is the code for the user control:

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;

        }


    }
}

Obviously, I don't know why this isn't working. Could someone help me out?

Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
user3634308
  • 119
  • 1
  • 3
  • 14

2 Answers2

2

Even in WPF, where you have bindings, the UI will not automatically pick up a change to a property (without raising PropertyChanged). It definitely won't in the hard-coded land of WinForms.

So you have two problems:

  1. You invoked the default constructor in your call, so no code ever sets the Text property of the textbox

  2. Even if you had set the text, the subsequent change to the property would not propagate to the UI.

The simplest solution would be to just run the UI update in the setter:

private string usrID;
public string UserID //Correct style!
{
   get { return usrID; }
   set
   {
      usrID = value;
      RCtextBox.Text = usrID;
   }
}

You could also call a method from the setter, listen on INotifyPropertyChanged and a number of other things.

Another way would be to expose a method instead of a property:

public string UpdateUserID(string newId)
{
    usrID = newId;
    RCtextBox.Text = usrID;
}
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • if I wanted to put UCtextBox.Text = usrID; further down in the code, how could I do that? – user3634308 Dec 01 '14 at 19:25
  • @user3634308 I'm not sure what you mean. You can put a copy of that line anywhere you want. You just need it in the setter in order to make the property change propagate to the UI. – BradleyDotNET Dec 01 '14 at 19:26
  • I need to use the usrID somewhere further down in the code, but it isn't working. something like stream.login = usrID; I have to create the stream first and then set the userID. – user3634308 Dec 01 '14 at 19:36
  • But that has nothing to do with setting the textbox text! Of course you have to create the stream before you can set a property. Without seeing the code in question (please just ask a new question, but if its an NRE, don't bother, just read: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it ) I don't understand what you are asking, or what is going wrong. – BradleyDotNET Dec 01 '14 at 19:38
  • I know it doesn't have anything to do with my original question, I was just asking – user3634308 Dec 01 '14 at 19:44
  • @user3634308 Thats fine (though we encourage only asking one question per question), but your question wasn't even clear. I'm happy to help, but you need to explain what you are doing (and if its totally unrelated, just ask a new question). – BradleyDotNET Dec 01 '14 at 19:46
  • I figured out my problem; I had it backwards. I should have put usrID in Form1 then set it equal to "username" in form1. Then I could access it in "test" and use it. I'm terrible at explaining, but the point is that I had it backwards – user3634308 Dec 03 '14 at 16:35
2

You should put value passed into usrId property to the textbox.

    public partial class test : UserControl
{
    public string usrID 
    { 
        get{return _usrId;} 
        set
        {
            _usrId = value;
              UCtextBox.Text = value;
        }
    }
Pavel Pykhtin
  • 353
  • 3
  • 11