0

I have a string stored in database as 'GridColumn1.Caption = "XYZ"'

Is there a way to execute this code in runtime (on windows form load) to set GridColumn1's Caption property to "XYZ" in vb.net or in c#?

I want to read it from db on form load and set the captions and texts.

Thanks in advance.

user1645334
  • 89
  • 1
  • 7
  • 1
    Why don't you just store the minimum information required in the database, for example `XYZ` in a column `Caption`? Then your code is much better: `GridColumn1.Caption = dbRec.Caption`. – Tim Schmelter Jun 09 '15 at 08:12
  • Hi Tim, My application is multi language so i have to change millions of objects' caption, text or tooltip property. I tried localization but not work good enough with third party elements. – user1645334 Jun 09 '15 at 10:42
  • What if you or others are going to change the variable name of `GridView1` to something more meningful like `GridUsers` in future? That will break your code silently (at runtime). Apart from the fact that reflection is always inefficient. Maybe this helps: http://stackoverflow.com/questions/1251225/proper-localization-of-a-winforms-application – Tim Schmelter Jun 09 '15 at 10:45

1 Answers1

0

Yes you can! You can use the Microsoft.CSharp.CSharpCodeProvider to compile and execute code during runtime.

More information can be found in the answers to this question.

Community
  • 1
  • 1
Melvin
  • 344
  • 1
  • 8
  • Hi Melvin, I tried the link you posted but i guess i cannot pass my form by reference (ByRef) to the code to be compiled. – user1645334 Jun 09 '15 at 10:45
  • Wouldn't another option be to just store "XYZ" in the database and have your form set it's own caption by retrieving this column's content? – Melvin Jun 09 '15 at 12:42
  • Form and all the control on it have to be changed. A multi language application. Translators are entering controls' captions to the database and then the application finds the right caption by user's selected language. – user1645334 Jun 09 '15 at 13:43
  • I suggest to either make a different table for each language (with a column for the control id, and one with the translation), or one table with different columns for each language. This will be a lot easier then having to execute code from each column. One mistake, and it won't run, crash, or the control won't be set. When everything are just `string`'s, the worst that could happen is a spelling mistake. Aside from that, Visual Studio has excellent tools for [multilingual applications](https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj572370.aspx). – Melvin Jun 09 '15 at 14:00