Tech Off Thread

6 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Form Designer automatically replacing my code - resources.GetObject()

Back to Forum: Tech Off
  • User profile image
    StinkyPants

    Hi All

    I have an issue with the Designer code behind a form. I've setup my solution so that a large part of the code is in a Library DLL and the actual UI code in another. The aim is to allow me to make changes to the actual functionality separate from the UI.

    What it should also allow me to do is have all my images and icons etc in the library DLL, which are then referenced by code in the UI DLL. This means if we want to replace an image in the future we update the library DLL and simply drop that into the install folder.

    This all works but only up to a point. For example I have a MainUI form which has an image that stretches across the top of the form. This is loaded into a picture box and I do this by going into the MainUI.Designer.vb and adding the line;

    Me.pbxBanner.Image = MyLibrary.LibraryResources.MainUIBannerImage


    (For reference LibraryResources is a class of shared properties that return resources loaded into the MyLibrary DLL).

    This is fine. When I go back to designer view the image is there and when run it displays without issue.

    The problem is somewhere along the way Visual Studio replaces my line with something like this;

    Me.pbxBanner.Image = CType(resources.GetObject("pbxBanner.Image"), System.Drawing.Image)


    This means if I update the image in my Library DLL the UI ignores it because it instead reads it from its own resources where it appears to have saved the image.

    I simply want to know how if possible I can switch off this behaviour.

    Thanks in advance!

  • User profile image
    wkempf

    The *.Designer.vb file is a generated file and you shouldn't be touching it (this is clearly stated in the generated file). I've often thought that Microsoft should make these partial classes, so we could make modifications to the type safely, but alas, they've not done this.

  • User profile image
    StinkyPants

    @wkempf: Thanks for response, and I do see the warning but if I cannot add it in there, where can I add it? Doing it this way means I can see the MainUI as it will appear when run at design time. I know I could set this information at run time which would mean it will always grab the correct images, but is there no way I can have the best of both!?

  • User profile image
    wkempf

    Nope. You'll have to do this programatically.

  • User profile image
    StinkyPants

    @wkempf: Boo! Never nice to be told you can't have your cake and eat it. Okay, I'll switch to being good and take out my manual edits from the *.Designer.vb file and will set this stuff programatically.

  • User profile image
    spivonious

    @StinkyPants: Yes, that's the only way. The .Designer.vb file is updated without warning by the WinForms designer.

    If you make a default constructor for the form class, you can add your change there after the InitializeComponent call.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.