tags:

views:

115

answers:

3

Any idea how i can do that ?

Now days it is done in Resources.Designer.vb we have there following lines:

Friend ReadOnly Property ResourceManager() As Global.System.Resources.ResourceManager
            Get
                   If Object.ReferenceEquals(resourceMan, Nothing) Then
                        #If WizardVersion Then
                            Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Wizard.Resources", GetType(Resources).Assembly)
                        #ElseIf CalculatorVersion Then
                            Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Calculator.Resources", GetType(Resources).Assembly)
                        #ElseIf ViewerVersion Then
                            Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Viewer.Resources", GetType(Resources).Assembly)
                        #End If
                    resourceMan = temp
                End If
                Return resourceMan
            End Get
        End Property 

That file is regenerated each time via the compiler and so each time i have to add there those lines by hand.

Can you suggest some another way to do so ?

Thanks.

+1  A: 

You could run a batch file that copies (and overrides the old file) the files into the proper place on a pre-build action. You could take a flag in as a parameter of the batch.

Daniel A. White
+2  A: 

You could use reflection to overwrite the ResourceManager created by the Resources class :

Sub InitResources()
    #If WizardVersion Then
        Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Wizard.Resources", GetType(Resources).Assembly)
    #ElseIf CalculatorVersion Then
        Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Calculator.Resources", GetType(Resources).Assembly)
    #ElseIf ViewerVersion Then
        Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Viewer.Resources", GetType(Resources).Assembly)
    #End If

    Dim resManField As System.Reflection.FieldInfo = GetType(My.Resources.Resources).GetField("resourceMan", Reflection.BindingFlags.Static Or Reflection.BindingFlags.NonPublic)
    resManField.SetValue(Nothing, temp)

End Sub

That's not very elegant, but it should work...

Thomas Levesque
Where from i should call that function ?What is the earliest point I can put it there ?
Night Walker
Call it when your program starts, before you need any resources. In the Sub Main for instance
Thomas Levesque
It doesn't compile i get on GetType(Resources).Assembly -> Resources type expected
Night Walker
I copied/pasted this part from your code... replace `GetType(Resources)` with `GetType(My.Resources.Resources)`
Thomas Levesque
Ok this compiled but i got a lot of exceptions , when i run it.I have put it this code as early as i could .
Night Walker
What exceptions ? I can't guess if you don't tell...
Thomas Levesque
Exception.InnerException The error is: Could not find any resources appropriate for the specified culture or the neutral culture.
Night Walker
On which line ? and where do you execute this method ? in the Main sub, in your form constructor, somewhere else ?
Thomas Levesque
I execute it on the first lines of form1.vb. And I am getting the exception when some resource is called .
Night Walker
"the first lines" doesn't mean anything, since the order of the methods can change... is it in the constructor (Sub New) ? in the Load event ? And by the way, did you just paste the method in your code, or do you actually call it ? put a breakpoint in the method to check that it is called
Thomas Levesque
I have pasted the code at the first lines of Sub New.I get my error before the constructor of my main file is called.The error happens in file called aplication.designer.vb . I think this file is generated by the compiler.You can think about better place were i can put this code in .
Night Walker
Yes, put it in Sub Main. If it doesn't exist, create it, and declare it as your startup object (in project properties)
Thomas Levesque
Can't it be done without changing the current enter point ?Now it's Form1 i tried to do that there but with no succeed
Night Walker
No, because if you don't create the Main method yourself, VS creates it for you and you have no control over it. This code has to be executed before anything accesses the resources
Thomas Levesque
One more question, what code I should call from main that my application will work as before i added the main ?I should call the contractor of form1 class ?
Night Walker
Yes, you need to call the constructor : Dim f As New Form1() then run the app : Application.Run(f)
Thomas Levesque
My project is of type windows application. Can i add there main ? I added there main like you said but i couldn't see it in the start from menu.
Night Walker
Just create a new module and add a method with the following signature : `Sub Main(ByVal args() As String)`
Thomas Levesque
Still problem . Look on the new message i put in the post
Night Walker
A: 
Module Main
    Sub Main(ByVal args() As String)
        Dim f As New Form1() ' Creating our mainform

        Application.EnableVisualStyles() 'set windows style 
        InitResources()
        Application.Run(f)

    End Sub


    Sub InitResources()
#If WizardVersion Then
        Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Wizard.Resources", GetType(My.Resources.Resources).Assembly)
#ElseIf CalculatorVersion Then
        Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Calculator.Resources", GetType(My.Resources.Resources).Assembly)
#ElseIf ViewerVersion Then
        Dim temp As Global.System.Resources.ResourceManager = New Global.System.Resources.ResourceManager("Viewer.Resources", GetType(My.Resources.Resources).Assembly)
#End If

        Dim resManField As System.Reflection.FieldInfo = GetType(My.Resources.Resources).GetField("resourceMan", Reflection.BindingFlags.Static Or Reflection.BindingFlags.NonPublic)
        resManField.SetValue(Nothing, temp)

    End Sub
End Module

I get missing manifest when i am trying to use resources .

Night Walker
You need to call InitResources *before* you create an instance of Form1, because Form1 needs the resources to initialize
Thomas Levesque
Done. Got following error Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "Wizard.Resources.resources" was correctly embedded or linked into assembly "LP_Wizard" at compile time, or that all the satellite assemblies required are loadable and fully signed.
Night Walker