tags:

views:

593

answers:

4

How do I reference a constant string in my .designer.cs file?

A straight forward answer is to create a private string variable in my .cs file, then edit the designer.cs file to use this variable instead of hardcoding the string. But the designer doesn't like this throws an error. I understand why this can't work, but I'm not sure of what the best alternative is.

Should each of my UI controls just have text as a place holder, then I need to override all text properties only at runtime? This way I loose the benefit of seeing everything in the designer.

I'm just trying to figure how to cause the least disruption when things change in the future. Thanks.

+3  A: 

It's not entirely clear to me what you're trying to achieve, but normally if you want to avoid the strings all being hard-coded into the designer .cs file, you use a resource file which the strings will be loaded from on startup.

If you set the "localizable" property of the form to be "true" I believe everything will end up in resources automatically. On the other hand, I'm far from expert at Windows Forms, so take all this with a pinch of salt until you've got it working :)

Jon Skeet
lol @ "far from expert"
DrG
While I wouldn't say I'm quite a newbie in WinForms, I'm really not an expert by any sensible definition.
Jon Skeet
+1  A: 

What problem are you trying to solve? If you're trying to implement localization, look at the localization features that come with .NET and with sattelite assemblies.

If you're trying to solve customization, look at the customization options available in .NET, with the settings editor.

Why do you need to use constant strings?

Dave Van den Eynde
I have a form with a lot of buttons and menu items on it. Based on the version of our product, I need to enable/disable different sets of buttons. The way this was done in the past is keeping a list of strings (button names) and using that to check if a certain button should be enabled/disabled.the constant strings were embedded into the .designer.cs file, which works, but makes the designer unusable.
TheSean
That doesn't explain why you would use those constant strings as placeholders, if the strings contain the names of the buttons.
Dave Van den Eynde
+1  A: 

Your second method is the best bet. Because you can't assume the designer is always going to keep your changes around, and often times it just blows them away. You are going to want to do your control changes in Page_Init event method. This may 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 get to keep you constants in your actual code-behind class that you control and the controls will be initiated the way you want and you can just control the things you want to change.

Update: sorry didn't realize this was WinForms. It is actually even easier in WinForms, because you just register your controls in the constructor right after the control initialization is called.

Nick Berardi
Thanks, but I'm just using winforms. There must be a similar event handler though. I'm not super familiar with how user controls are rendered in the designer but I will give it a look.
TheSean
You actually just want to do it in the constructor after the controls have been initialized.
Nick Berardi
+3  A: 

The *.designer.* files are generated by Visual Studio. You don't want to modify them by hand at all unless you really have to, because Visual Studio may at times re-generate them and erase your changes.

What you really need to do is think of your constant string a resource that you want to be available to the forms designer, so that when it generates the code it uses that resource.

There are several ways to accomplish this in Visual Studio. The easiest is probably via databindings for the controls. To demonstrate, put a label control on a form and look at the properties for the control. If you sort the properties by name there will be two properties at the top of the list enclosed in parentheses: ApplicationSettings and DataBindings. If you pull those down, you will see they both have options to bind to the Text property of your label. Using these, you can put your "constant string" in either your app's settings file and bind it via the ApplicationSettings property or in any datasource and bind it via the DataBindings property. Then the forms designer will generate the appropriate code for you.

Joel Coehoorn
Joes is right, actually u should not modify the designer file manually!
Enyra