Hi
I've looked online and played around allot but can't seem to find a solution
I made a custom user control that has a textbox & Lable inside of it. All I wanted to do was to override the Text property of the user control to point to the text property of the inner textbox… seems simple enough but it just doesn’t work. I found allot of topics online from people with the same problem but no solution. When I create a new property named “Value” with exactly the same code it works fine. Any assistance would be appreciated.
[Browsable(true)]
[Bindable(true)]
public override string Text
{
get
{
return txtInner.Text;
}
set
{
txtInner.Text = value;
}
}
{
return txtInner.Text;
}
set
{
txtInner.Text = value;
}
}
VulpesPosted Mar 19, 2014, 7:41 AM
We do need to add a couple more attributes:
Rousseau SennettPosted Mar 20, 2014, 3:51 AM
1. The container basically looks at what control it contains and adjusts the control & itself to look the way I want it to.
2. This makes design work allot faster for me. And should I ever decide for example I want all the TextBoxes in my applications to look different I only have to recode the 1 container.
This is not my final "Control Wrapper" but I attached my solution. I think this might be a better idea to start with.
VulpesPosted Mar 19, 2014, 5:16 PM
I wondered whether placing the following attribute on the UserControl itself might have solved the problem:
{
// blah
}
but it didn't seem to make any difference :(
Rousseau SennettPosted Mar 19, 2014, 10:33 AM
I added a test db to my project with a dataset to test the databinding. I Dropped a Datagridview & TextBox (Text Databound to same column) on the form
1. When I browse through data in the datagridview UserControl1 updates fine.
2. When I change the value in the DataGridView it persists and both controls change
3. When I change the value in the TextBox it persists and the datagridview & usercontrol changes
4. When I change the value in the UserControl it doesn't seem to update my bindingsource.
I just can't seem to expose the innerTextbox.Text property to behave the same as it usually would in a simple TextBox.
VulpesPosted Mar 19, 2014, 7:32 AM
Although you can set the Text property of the UserControl from the Properties Box (and it then shows up immediately on the design surface) when you run it the value is not persisted.
As you say, this is simply because the value of the Text property isn't written to the InitializeComponent method.
Offhand, I don't know whether this is because of a bug or limitation in the VS designer or because we're missing some attribute.
I'll investigate later, when I have more time, and let you know if I come up with an answer.
Rousseau SennettPosted Mar 19, 2014, 7:13 AM
After your reply I decided to start fresh to make sure something wasn't going wrong between classes. I'm using VS2010 & Windows 8.
1. I created new Windows Forms Project
2. Added User Control "UserControl1"
3. Added the exact same code as above to override the Text Property.
It seems I found the problem, when I set the "Text" property in design view from the properties toolbox it doesn't add the following to Form1.Designer.cs
this.userControl11.Text = "test";
however if I add the above code to the Form1.Designer.cs manually it seems to work fine. Is this a bug in VS2010 or am I missing an attribute on my override ? I also found the same problem with binding the Text property... the code is not added to Form1Designer.cs
VulpesPosted Mar 18, 2014, 11:14 AM
The Text property of txtInner changes when you set the Text property of the UserControl either in the designer or programatically.
Similarly, getting the Text property also works as expected.
So when you say it doesn't work, do you mean that attempting to set the UserControl's Text property doesn't display anything in txtInner at all?