I have a form named "form1" in vb.net. This form has many controls. I opened the form1.designer.vb file and put in an if else expression such as:
If getLanguage() = "en" then label1.text = "Good Morning" Else label1.Text = "Bonjour"
This works perfectly fine in runtime.
If I open the Form1.vb [Design] page in design, and make any changes, the code above disappears.
Is there a way I can keep any code I put in the designer page? I don't want to put them in the load event in the form1.vb file.
For your example maybe better will be using property of the form
Visual Studio create two files of one class(which is your form) - Partial Class
So it is doesn't matter in which file you write your code.
Only visual studio use
designer.vb
file for generating form changes you made by designerCode from
designer.vb
contain methodInitializeComponent
which executed in the constructor.Create your own method and call it right after
InitializeComponent
in constructor.Or create third file for your code if you don't want put in the
yourform.vb
File
yourForm.MyDesigner.vb
designer.vb files are created automatically by Visual Studio. Every time you edit the design of the form, the file is re-written. Now, why you don't want to have that code on Load? The load event or the constructor (as already stated) are the right places where to put any initialization you need.
Locate this code :
Then add a line :
Then create the method in form1.vb or another Partial Class copy of your creation :
Don't touch the form1.designer.vb (.cs)
And as stated in other answers, better use
.Localizable
Property in the IDE and change it fromFalse
toTrue
. Then you'll gain access to several default languages. You don't have to bother writing code.English
for example.French
.Form1.en.resx
andForm1.fr.resx
(or so) along withForm1.vb
andForm1.Designer.vb
. Don't edit them !If you open the en.resx or fr.resx, you'll see that the edits you've made are in there. Those files are used to store inbuilt Lang-related ressources for your form. That's flatly called Globalization.
Then locate again the constructor of your Form.
Don't forget to add on top of your Class declaration those two namespaces :
And voilà ! I know I said you don't have to bother writing code, but the bits above in the constructor are enough to handle the selection of a language. Plus besoin de taper du code superflu après ça.