1
votes

I have a simple user control with a text box and label in it. I created public properties to access the text in the textbox when I use the user control in another form.

My problem is the property is returning null value when I call it in the form. Am i missing anything?

My property is as follows::

 public partial class UserControl1 : UserControl
 {
        public UserControl1()
        {
            InitializeComponent();
        }

        public string rtnTxtMake
        {  
            get 
            { 
                return txtMake.Text; 
            }
            set 
            { 
                txtMake.Text = value; 
            } 
        }
 }

and in the next forms button click event i call the property as follows

        UserControl1 Usc = new UserControl1();
        string Make = Usc.rtnTxtMake;

        MessageBox.Show(Make)
3
Do you ever set txtMake.Text to anything?Nifle
i enter values in the text box when i run the application.is n't that enough?Kishore
manually when i assigned txtMake.Text to some value.i'm able to get the required output.but how do i assign the value typed during runtime to txtMake.Text????????Kishore
Ewww, hungarian notationanonymous coward
There's nothing wrong with creating a UserControl at run-time in response to a Button Click ! The other comments/answers already remind you : you need to add it to a valid "container" (Form, Panel, etc.) to use it, and you'll have do that inside the Button Click event : since the UserControl you create will go "out of scope" when the Click event exits. In most cases, you'll want keep a reference to the newly created UserControl to access it later : whether that's a specific variable declared at Form-scope level, or an entry in a List<UserControl> depends on your design and intent.BillW

3 Answers

0
votes
UserControl1 Usc = new UserControl1();
string Make = Usc.rtnTxtMake;

If your user control has by default an empty textbox field, then it seems correct that the above two lines of code would return either null or String.Empty (check via String.IsNullOrEmpty), since you explicitly create a new instance of your user control.

I suppose what you really want is this:

  • You have inserted a user control into a form in the Designer. Let's call this user control instance ctlUser.

  • You have a button with a Click event handler. The last few lines of code in your question are from that handler method.

  • In the handler, you wouldn't create a new instance of your user control (Usc) but refer to the one that you previously inserted into your form, ctlUser. Then things should work as expected.

0
votes

Your UserControl must be added to the Controls collection of a parent Form/Control before it can be properly initialized. Normally you would not write the code yourself that creates and adds the UserControl.

Instead, first build your project, then go to the Deisgner view of your main form and look at the Toolbox.

Your UserControl name (and an icon) should appear towards the top of the toolbox, and you can simply drag it to the main form. The Windows Forms designer will automatically generate the needed initialization code for you.

You should not create a new instance of your control in your button click event handler. Using the Designer approach to create your control you can simply access the existing instance of your control as follows:

public void button_Click(object sender, EventArgs e)
{
    // myUserControl1 has already been created and initialized by the Deisgner generated code
    // Note the name 'myUserControl1' is just an example, yours may be different.
    string controlText=myUserControl1.rtnTxtMake;

    // Or to change the UserControl textbox value
    myUserControl1.rtnTxtMake="Testing";
}
0
votes

What exactly to you mean when you say that the property is returning a null value? Is it actually null, or is your MessageBox simple showing empty?

I quickly duplicated your code and it behaves exactly as expected - the MessageBox shows, but it is empty because the default value of the Text property of the TextBox control is an empty string.

Also, the way you are approaching this is a little unusual.

Firstly, the line:

UserControl1 Usc = new UserControl1(); 

You do not generally need to instantiate a user control like this. Instead you can drag the control from the toolbox onto the design surface of your form. This will then take care of instantiating and initialising your control for you.

I think that this is actually your problem - when you include the line of code above, you are creating a new instance of the user control, and this is is no way realted to the user control that you have dragged onto the designer.

If you go to the designer view of your form and click on the user control, you should see a properties window somehere. If you do no, then either select it from the View menu, or press F4. In the list of properties, there should be one "Name" this is the programatic name generated for your user control. You can change this here if you want, but when you refer to this control in the rest of the form, this is what you must use.

Secondly, the next two lines:

string Make = Usc.rtnTxtMake;  

MessageBox.Show(Make)  

You can access the property rtnTxtMake directly. Unless you later need to access the Make string in the rest of your code, then directly accessing the property would usually be considered better style.

MessageBox.Show(userControl.rtnTxtMake);