tags:

views:

198

answers:

3

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)
A: 

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";
}
Ash
@Ash I create UserControls at run-time often; imho : not uncommon or "abnormal" practice. Of course you are right to point out that any run-time created control is probably going to be "useless" until it's added to some "container." But, there are exceptions : I use a run-time created 3rd. party TreeView instance : never added to any container : it enables saving selected nodes in the UI TreeView as XML (said TreeView enables saving an entire TreeView as XML, only). I clone selected nodes in the UI's TreeView into the "ghost" TreeView, save the "ghost" TreeView as XML, then clear the "ghost."
BillW
A: 
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.

stakx
Thanks ..i referring to new instance..sorry!!
Kishore
A: 

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);
David Hall
@David, minor observation : since the variable is desclared in the scope of a (surely Private) Button click handler, it's not going to be accessible outside that scope, anyway : of course you are correct that declaring the variable in a one-off usage like this is a waste of time.
BillW