Click to See Complete Forum and Search --> : Text Property for user control


hansipet
February 28th, 2006, 02:59 AM
Hello,
at the moment I create a usercontrol. I wan't to have for that a text property. But I don't have any idea, why my solution doesn't work...


[
Category("Appearance"),
Description("The text associated with the control.")
]
public string Text
{
get { return text; }
set { text = value; }
}


Regards
Hansjörg

hspc
February 28th, 2006, 03:15 AM
The control class (the base class of all user controls) already has the Text property..Why do you want to add your own?
You can override OnTextChanged method to add your special logic or override the Text property itself.

hansipet
February 28th, 2006, 03:19 AM
I don't want a new Text propertie. I want to see and edit the text property in the property editor (at the moment I don't see it there), because I want to display the text in a label inside the usercontrol.
What is the best way for that?

Regards
hansjörg

hspc
February 28th, 2006, 03:24 AM
try to add the Browsable attribute:

[Browsable(true)]
public override string Text
{
get
{
return text;
}
set
{
text = value;
}
}

hansipet
February 28th, 2006, 03:57 AM
Great it works. Is there also a possibility to bind the text property of a label to this text property? It would be great if I can see the text also in the designer

Regards and many thanks
Hansjörg

hansipet
February 28th, 2006, 04:33 AM
Solved. I can set the property of the label only inside the Property Text

Thanks
Hansjörg

hansipet
March 1st, 2006, 01:54 AM
Hello,

during the test I have found out that the text-Property is not saved if I close the form.
What I have to do to save it?

Regards
Hansjörg

hspc
March 1st, 2006, 03:55 PM
Hello,

during the test I have found out that the text-Property is not saved if I close the form.
What I have to do to save it?

Regards
Hansjörg
strange
Please send the code of the property..or upload the code file.

hansipet
March 2nd, 2006, 12:33 AM
public partial class ConnectionDeviceSettingsCtrl : UserControl{
private string text;
[Browsable(true)]
public override string Text
{
get { return text; }
set
{
this.text = value;
connectionDevSettings.Text = value;
}
}

private System.Windows.Forms.GroupBox connectionDevSettings;
}


Regards and many thanks for your help!

hspc
March 2nd, 2006, 01:30 AM
So..When you close the form designer and reopen it ,, you lose the last Text value...
This is so strange..I did not face this problem before..

hansipet
March 2nd, 2006, 02:02 AM
Yes I loose this property. I have another strange problem in this workspace.

If the control where the usercontrol is placed is open during closing my visualstudio 2005 express edition.

- control where the above usercontrol is used is open in designer
- close VS 2005 express edition
- open VS 2005 express edition
- open this project -->
Receive warnings:

Warning 1 Exception has been thrown by the target of an invocation. D:\daten\projekte\c#\NetWorkManagerDev\NetworkManager\View\ConfigCtrl.Designer.cs 531 0
Warning 2 Could not load file or assembly 'Ethernet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Das System kann die angegebene Datei nicht finden. D:\daten\projekte\c#\NetWorkManagerDev\NetworkManager\View\ConfigCtrl.Designer.cs 538 0
Warning 3 Could not load file or assembly 'Ethernet, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Das System kann die angegebene Datei nicht finden. D:\daten\projekte\c#\NetWorkManagerDev\NetworkManager\View\ConfigCtrl.Designer.cs 537 0
Warning 4 Could not find type 'Ethernet.NetworkManager.Data.DeviceConnectionList'. Please make sure that the assembly that contains this type is referenced. If this type is a part of your development project, make sure that the project has been successfully built. D:\daten\projekte\c#\NetWorkManagerDev\NetworkManager\View\ConfigCtrl.Designer.cs 514 0


I don't know why. May be the two things are associated..
Regards
Hansjörg

hansipet
March 2nd, 2006, 02:04 AM
sorry I saw that some parts of the warnings are in german.

Das System kann die angegebene Datei nicht finden.

means

The system can't find the file

hspc
March 2nd, 2006, 12:14 PM
Try to rebuild the control separately..then add it again to the form designer..

hansipet
March 3rd, 2006, 12:10 AM
What do you mean with build seperatly and add it to the form designer. Do you mean build the project seperatly and add it than to the VS2005 workspace?

Regards
Hansjörg

hspc
March 3rd, 2006, 12:58 AM
No
delete the control from the form
then build the control project
then add it again to the form..

another last option:
remove the control project from the workspace..
build it
add it to the control box as an assemble..then drag and drop it on the form.

hansipet
March 3rd, 2006, 01:32 AM
I have made now the first step:

I think it is a bug of VS2005...
For some reason it seems that the building of the components not works properly...
if I have opened a form with the control -> close visual studio -> open project -> an error on the form appears. I have first to close the form, close the control -> rebuild project. open one time the control and after that it works..


Regards
Hansjörg

jkristia
April 10th, 2006, 09:58 AM
I have the exact same problem now.
I needed the .Text field exposed in a user derived class of UserControl, so I added the Browsable(true), so now it shows up, but when changed, nothing get written to the file.

Any solution for this problem?

My woraround for now was to add a new field like this

[Category("Appearance"), Description("wrapper for .Text field")]
public string Title
{
get { return base.Text; }
set { base.Text = value; }
}


which of course is really ugly.

Any better way of exposing the Text field in the designer so it works?

Jesper

jhammer
April 10th, 2006, 10:55 AM
How about:

[Browsable(true)]
public new string Text
{
get { return base.Text; }
set { base.Text = value; }
}

jkristia
April 10th, 2006, 11:25 AM
That does not work.

I first tried override then new, neither works, the property is not written the file.

hansipet
April 11th, 2006, 12:12 AM
Til now I work also only with the workaround (other property). I had the problem also for a few other controls. I don't have any idea, what is the problem.

Regards
Hansjörg

jkristia
April 11th, 2006, 09:55 AM
yeah I guess we will just have to live with that bug.

jkristia
April 11th, 2006, 10:45 AM
I found the answer on the microsoft.public.dotnet.languages.csharp group.




[EditorBrowsable(EditorBrowsableState.Always), Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Bindable(true)]
public override string Text
{
get { return base.Text; }
set { base.Text = value; }
}


The important attribute is the DesignerSerializationVisibility one

Jesper

hansipet
April 12th, 2006, 01:02 AM
It doesn't work too...I think this is a bug of the designer or something else...

jkristia
April 12th, 2006, 09:47 AM
hm... it works for me. The field shows up in the designed (as before) and when changed and saved, the changes are written correct in the InitializeComponent method, so for me it works correct now.