c# Change background in all forms with a button click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Keno995
    New Member
    • Jul 2013
    • 2

    c# Change background in all forms with a button click

    I made 2 forms and Form2 has buttons with images like planets and game wallpapers etc. When I write the code for button:

    Code:
    this.BackgroundImage = new Bitmap(Properties.Resources._8_8008_by_amplifier404); this.BackgroundImageLayout = ImageLayout.Stretch;
    // where _8_8008_by_amplifier404 is name of the image located in resources
    it only changes background in form which has those buttons as long the form is used. Once I go back to form1 it's changed to null. How to set background in all forms with a button click which will be there until another button with an image is clicked.
    Attached Files
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You could raise an event that is caught by all pages that need to change their background when the background is changed. Pass the value for the new background image using a custom EventArgs.

    Handle the event in the forms that need to change their backgrounds and in this method retrieve the new value from the EventArgs so that they can actually change the background.

    If the forms that need to change their backgrounds do not have an instance of the control that causes the background to change, than make sure that your event is a Shared/Static event so that they can still handle it.

    Be sure to remove handlers to shared events when objects are destroyed/disposed of.

    -Frinny

    Comment

    • Keno995
      New Member
      • Jul 2013
      • 2

      #3
      Can you write a code for that?!

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You can find information about Raising events here:
        Raising an Event
        You can find information about Handling events here:
        Handling and Raising Events

        Instead of just having a public delegate you can define it's scope as a public static delegate so that it is shared event that can be caught by all windows.

        Comment

        Working...