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)
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
nullorString.Empty(check viaString.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
Clickevent 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.