How to use const strings in C # designer.cs code?
How can I refer to a constant string in the .designer.cs file?
The direct answer is to create a private string variable in my .cs file and then edit the designer.cs file to use that variable instead of hardcoding the string. But the constructor doesn't like this, it throws an error. I understand why this may not work, but I'm not sure which is the best alternative.
If each of my UI controls only have text as the place owner, do I need to override all text properties only at runtime? Thus, I lose the advantage of seeing everything in the designer.
I'm just trying to figure out how to cause the smallest disturbance when things change in the future. Thanks.
a source to share
The files *.designer.*
are generated by Visual Studio. You don't want to manually change them if you really need to, because Visual Studio can sometimes generate them and erase your changes.
What you really need to do is think about the constant constant of the resource that you want to use for the form designer so that it will use that resource when generating code.
There are several ways to do this in Visual Studio. The simplest is probably using data binding for controls. To demonstrate, place a label control on a form and look at the properties of the control. If you sort properties by name, there are two properties at the top of the list, enclosed in parentheses, ApplicationSettings
and DataBindings
. If you pull them, you can see that they both have options to bind to a property of Text
your shortcut. Using them, you can put your "constant string" in an application settings file and bind it using a property ApplicationSettings
or in any data source and bind it using a property DataBindings
. The form designer will then generate the appropriate code for you.
a source to share
It's not entirely clear to me what you are trying to achieve, but usually if you want to avoid the strings being hardcoded in the .cs file, you use a resource file that the strings from will be loaded into at startup.
If you set the form's "localizable" property to "true" I believe that everything will be automatically loaded into the resources. On the other hand, I'm far from an expert on Windows Forms, so take it all with a pinch of salt until you get it working :)
a source to share
What problem are you trying to solve? If you're trying to implement localization, take a look at the localization features that come with .NET and siteolithic assemblies.
If you are trying to decide a customization, review the customization options available in .NET using the Settings Editor.
Why do you need constant strings?
a source to share
Your second method is the best choice. Because you can't assume the designer will always keep your changes going, and often they just dump them. You will want to change your control changes in the event method Page_Init
. This can allow your changes to show up in the designer.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.init.aspx
This way you can store the constants in your actual class of code that you control and the controls will be triggered the way you want and you can just control the things you want to change.
Update: Sorry, but didn't understand that this is WinForms. It's actually even easier in WinForms because you just register your controls in the constructor right after the call to initialize the control.
a source to share