Javascript required
Skip to content Skip to sidebar Skip to footer

How to Add Background Image in Windows Form in C#

#1

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 3
  • Joined: 01-March 16

C# Windows Form Application Background Image: How to Save

Posted 01 March 2016 - 08:55 PM

Hello, I need help with a C# Windows form application. My application is mostly done and all the settings save through save.settings. But the short of it is the app will load and have a default background image. The user can select from a list of embedded images and change the background. The update works fine. The problem is when the user closes the app and re-opens it the default image loads and not the new image. So how do I save the background image with save.settings or another method?


Is This A Good Question/Topic? 0

  • +

#2 Skydiver User is offline

Reputation: 7895

  • View blog
  • Posts: 26,360
  • Joined: 05-May 12

Re: C# Windows Form Application Background Image: How to Save

Posted 01 March 2016 - 09:52 PM

Show us your code for how you are saving and loading the background settings.

#3 Isaiah 53 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 3
  • Joined: 01-March 16

Re: C# Windows Form Application Background Image: How to Save

Posted 01 March 2016 - 10:08 PM

See code below.

//Loads data from save settings on form load   private void lbl_Prestige_Load(object sender, EventArgs e)         {             lbl_MWlevel.Text = Properties.Settings.Default.MWlevel;             lbl_MWPrestige.Text = Properties.Settings.Default.MWPrestige;             lbl_MWKD.Text = Properties.Settings.Default.MWKD;             lbl_MW2KD.Text = Properties.Settings.Default.MW2KD;             lbl_MW2lvl.Text = Properties.Settings.Default.MW2lvl;             lbl_MW2Prestige.Text = Properties.Settings.Default.MW2Prestige;             lbl_MW3KD.Text = Properties.Settings.Default.MW3KD;             lbl_MW3lvl.Text = Properties.Settings.Default.MW3lvl;             lbl_MW3Prestige.Text = Properties.Settings.Default.MW3Prestige;             lbl_BOKD.Text = Properties.Settings.Default.BOKD;             lbl_BOlvl.Text = Properties.Settings.Default.BOlvl;             lbl_BOPrestige.Text = Properties.Settings.Default.BOPrestige;             lbl_BO2KD.Text = Properties.Settings.Default.BO2KD;             lbl_BO2lvl.Text = Properties.Settings.Default.BO2lvl;             lbl_BO2Prestige.Text = Properties.Settings.Default.BO2Prestige;             lbl_BO3KD.Text = Properties.Settings.Default.BO3KD;             lbl_BO3lvl.Text = Properties.Settings.Default.BO3lvl;             lbl_BO3Prestige.Text = Properties.Settings.Default.BO3Prestige;             lbl_AWKD.Text = Properties.Settings.Default.AWKD;             lbl_AWlvl.Text = Properties.Settings.Default.AWlvl;             lbl_AWPrestige.Text = Properties.Settings.Default.AWPrestige;             lbl_GhostsKD.Text = Properties.Settings.Default.GhostsKD;             lbl_Ghostslvl.Text = Properties.Settings.Default.Ghostslvl;             lbl_GhostsPre.Text = Properties.Settings.Default.GhostsPrestige;             lbl_WAWKD.Text = Properties.Settings.Default.WAWKD;             lbl_WAWlvl.Text = Properties.Settings.Default.WAWlvl;             lbl_WAWPrestige.Text = Properties.Settings.Default.WAWPrestige;             lbl_GamerTag.Text = Properties.Settings.Default.Gamertag;  // Saves data on form close   private void lbl_Prestige_FormClosed(object sender, FormClosedEventArgs e)         {             Properties.Settings.Default.MWlevel = lbl_MWlevel.Text;             Properties.Settings.Default.MWKD = lbl_MWKD.Text;             Properties.Settings.Default.MWPrestige = lbl_MWPrestige.Text;             Properties.Settings.Default.MW2KD = lbl_MW2KD.Text;             Properties.Settings.Default.MW2lvl = lbl_MW2lvl.Text;             Properties.Settings.Default.MW2Prestige = lbl_MW2Prestige.Text;             Properties.Settings.Default.MW3KD = lbl_MW3KD.Text;             Properties.Settings.Default.MW3lvl = lbl_MW3lvl.Text;             Properties.Settings.Default.MW3Prestige = lbl_MW3Prestige.Text;             Properties.Settings.Default.BOKD = lbl_BOKD.Text;             Properties.Settings.Default.BOlvl = lbl_BOlvl.Text;             Properties.Settings.Default.BOPrestige = lbl_BOPrestige.Text;             Properties.Settings.Default.BO2KD = lbl_BO2KD.Text;             Properties.Settings.Default.BO2lvl = lbl_BO2lvl.Text;             Properties.Settings.Default.BO2Prestige = lbl_BO2Prestige.Text;             Properties.Settings.Default.BO3KD = lbl_BO3KD.Text;             Properties.Settings.Default.BO3lvl = lbl_BO3lvl.Text;             Properties.Settings.Default.BO3Prestige = lbl_BO3Prestige.Text;             Properties.Settings.Default.AWKD = lbl_AWKD.Text;             Properties.Settings.Default.AWlvl = lbl_AWlvl.Text;             Properties.Settings.Default.AWPrestige = lbl_AWPrestige.Text;             Properties.Settings.Default.GhostsKD = lbl_GhostsKD.Text;             Properties.Settings.Default.Ghostslvl = lbl_Ghostslvl.Text;             Properties.Settings.Default.GhostsPrestige = lbl_GhostsPre.Text;             Properties.Settings.Default.WAWKD = lbl_WAWKD.Text;             Properties.Settings.Default.WAWlvl = lbl_WAWlvl.Text;             Properties.Settings.Default.WAWPrestige = lbl_WAWPrestige.Text;             Properties.Settings.Default.Gamertag = lbl_GamerTag.Text;             Properties.Settings.Default.Save();            }            

Actually the code I just listed is for all the settings on the form except the back ground. I do not know how to save the background image because in save settings I am not sure what type to use. As for loading the background settings I simply just put an image in the resources and then made a default image in the background image section in properties under appearance. Then I have this code below that will switch the image when the user selects it from a combo box.

              private void button1_Click(object sender, EventArgs e)         {             switch (comboBox1.SelectedIndex)             {                 case 0:                     form1.BackgroundImage = Properties.Resources.call_of_duty_modern_warfare_2;                     break;                 case 1:                     form1.BackgroundImage = Properties.Resources.zombies_new;                     break;                 case 2:                     form1.BackgroundImage = Properties.Resources.Onslaught_Michael_Myers;                     break;                 case 3:                     form1.BackgroundImage = Properties.Resources.ghost_modern_warfare_two_x_call_of_duty_253888_1024x575;                     break;                 case 4:                     form1.BackgroundImage = base.BackgroundImage;                     break;             }                     this.Close();            

This post has been edited by Skydiver: 01 March 2016 - 10:15 PM
Reason for edit:: Put code in code tags, learn to do this yourself.

#4 Skydiver User is offline

Reputation: 7895

  • View blog
  • Posts: 26,360
  • Joined: 05-May 12

Re: C# Windows Form Application Background Image: How to Save

Posted 01 March 2016 - 10:14 PM

Move your background setting code out of your button click handler and into a method that takes an integer. In your button click handler, call the method passing in the selected combobox index. Also save the selected combobox index into your settings. In your form load handler, call the method passing in the index that you saved in your settings.

#5 Isaiah 53 User is offline

  • New D.I.C Head

Reputation: 0

  • View blog
  • Posts: 3
  • Joined: 01-March 16

Re: C# Windows Form Application Background Image: How to Save

Posted 01 March 2016 - 10:21 PM

Thanks for the reply but not sure that I follow. I'm not an expert at C# and still learning. Could you please provide an example?

Also, how do I save the selected combobox index into my settings?

#6 Skydiver User is offline

Reputation: 7895

  • View blog
  • Posts: 26,360
  • Joined: 05-May 12

Re: C# Windows Form Application Background Image: How to Save

Posted 02 March 2016 - 06:45 AM

So instead of:

void DetonateButton_Click(object sender, EventArgs e) {     int time = 0;      if (int.TryParse(TimeLimitTextBox.Text, out time))         CountDownTimer.TimeLimit = time; }            

You would have:

void SetTimer(string text) {     int time = 0;      if (int.TryParse(text, out time))         CountDownTimer.TimeLimit = time; }  void DetonateButton_Click(object sender, EventArgs e) {     SetTimer(TimeLimitTextBox.Value); }            

And you are kidding about needing an example about saving and loading to settings, right? You already have code that loads and saves settings. It's exactly the same principle.

How to Add Background Image in Windows Form in C#

Source: https://www.dreamincode.net/forums/topic/389727-c%23-windows-form-application-background-image-how-to-save/