Custom code in designer.vb file goes away when making edits in design mode

1.8k views Asked by At

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.

3

There are 3 answers

1
Karl Stephen On BEST ANSWER

Locate this code :

    Public Sub New()
        Me.InitializeComponent()
    End Sub

Then add a line :

    Public Sub New()
        Me.InitializeComponent()
        Me.MakeComponentsChanges()
    End Sub

Then create the method in form1.vb or another Partial Class copy of your creation :

    Private Sub MakeComponentsChanges()
        If getLanguage() = "en" then
            label1.text = "Good Morning"
        Else
            label1.Text = "Bonjour"
        End If
    End Sub

Don't touch the form1.designer.vb (.cs)


And as stated in other answers, better use .Localizable Property in the IDE and change it from False to True. Then you'll gain access to several default languages. You don't have to bother writing code.

  • Select ONE language to start with : English for example.
  • Then edit each one of your controls Text : write "Hello" in a button, "Good Morning" in a Label, "Because I'm Happy" in a MenuItem etc.
  • Then change the language again, select French.
  • Then edit again each control and write "Bonjour", "Je vous souhaite un bon matin", "Parceque je suis de bonne humeur".... YES you've lost the previous text but have faith !
  • Compile your project without launching it, and you'll see the IDE has created two new files : Form1.en.resx and Form1.fr.resx (or so) along with Form1.vb and Form1.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.

    Public Sub New()
        Me.InitializeComponent()
        'Me.MakeComponentsChanges()
        ' Now you know about some Globalization, 
        ' you may get rid of that Method.

        ' Add two variables :
        Dim OriginalCulture As CultureInfo
        Dim CurrentOSCulture As CultureInfo

        ' Initialize them
        OriginalCulture = Thread.CurrentThread.CurrentCulture
        CurrentOSCulture = CultureInfo.CurrentCulture

        ' Do this test :
        Try
            Thread.CurrentThread.CurrentCulture = CurrentOSCulture ' may fail
            Thread.CurrentThread.CurrentUICulture = CurrentOSCulture ' may fail
            ' Attempt to match the current Thread culture to the Operating System one.
        Catch CurrentException As Exception
            Thread.CurrentThread.CurrentCulture = OriginalCulture
            Thread.CurrentThread.CurrentUICulture = OriginalCulture
            ' If it fails, revert back to default as defined in your IDE
        End Try
    End Sub

Don't forget to add on top of your Class declaration those two namespaces :

Imports System.Globalization
Imports System.Threading

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.

1
Fabio On

For your example maybe better will be using property of the form

.Localizable = true
.Language = Default(French)/English/or others

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 designer

Code from designer.vb contain method InitializeComponent 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

'Using Keyword Partial not necessary anymore,  
'because it is used in the `yourForm.Designer.vb` 
Public Class yourForm

    Public Sub Changelabel()
        If getLanguage() = "en" Then 
            label1.text = "Good Morning" 
        Else 
            label1.Text = "Bonjour"
        End If
    End Sub

End Class
1
Josh Part On

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.