2015-07-11 85 views
0

我想编写一个快速程序,并且当我将Properties.Settings分配给变量s时出现错误。 我想将它分配给这个变量,因为我的很多文本框需要分配一个设置的值,也因为有很多需要保存的设置。与Properties.Settings不兼容的可访问性?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
namespace SharpDock 
{ 
    public partial class SettingsWindow : Form 
    { 
     public Properties.Settings s = new Properties.Settings(); // This is the error. 
     public SettingsWindow() 
     { 
      InitializeComponent(); 
     } 

     private void Settings_Load(object sender, EventArgs e) 
     { 
      app1.Text = s.app1; 
      app2.Text = s.app2; 
      app3.Text = s.app3; 
      app4.Text = s.app4; 
      app5.Text = s.app5; 
      app6.Text = s.app6; 
      ico1.Text = s.ico1; 
      ico2.Text = s.ico2; 
      ico3.Text = s.ico3; 
      ico4.Text = s.ico4; 
      ico5.Text = s.ico5; 
      ico6.Text = s.ico6; 
     } 

     private void abutton1_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Executable Files (*.exe) | *.exe"; 
      ofd.Title = "Which executable would you like to launch?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       app1.Text = ofd.FileName; 
       s.app1 = ofd.FileName; 
      } 
     } 

     private void Accept_Click(object sender, EventArgs e) 
     { 
      s.Save(); 
      MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 

     private void abutton2_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Executable Files (*.exe) | *.exe"; 
      ofd.Title = "Which executable would you like to launch?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       app2.Text = ofd.FileName; 
       s.app2 = ofd.FileName; 
      } 
     } 

     private void abutton3_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Executable Files (*.exe) | *.exe"; 
      ofd.Title = "Which executable would you like to launch?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       app3.Text = ofd.FileName; 
       s.app3 = ofd.FileName; 
      } 
     } 

     private void abutton4_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Executable Files (*.exe) | *.exe"; 
      ofd.Title = "Which executable would you like to launch?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       app4.Text = ofd.FileName; 
       s.app4 = ofd.FileName; 
      } 
     } 

     private void abutton5_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Executable Files (*.exe) | *.exe"; 
      ofd.Title = "Which executable would you like to launch?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       app5.Text = ofd.FileName; 
       s.app5 = ofd.FileName; 
      } 
     } 

     private void abutton6_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Executable Files (*.exe) | *.exe"; 
      ofd.Title = "Which executable would you like to launch?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       app6.Text = ofd.FileName; 
       s.app6 = ofd.FileName; 
      } 
     } 

     private void ibutton1_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Image Files (*.png) | *.png"; 
      ofd.Title = "Which icon would you like?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       ico1.Text = ofd.FileName; 
       s.ico1 = ofd.FileName; 
      } 
     } 

     private void ibutton2_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Image Files (*.png) | *.png"; 
      ofd.Title = "Which icon would you like?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       ico2.Text = ofd.FileName; 
       s.ico2 = ofd.FileName; 
      } 
     } 

     private void ibutton3_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Image Files (*.png) | *.png"; 
      ofd.Title = "Which icon would you like?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       ico3.Text = ofd.FileName; 
       s.ico3 = ofd.FileName; 
      } 
     } 

     private void ibutton4_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Image Files (*.png) | *.png"; 
      ofd.Title = "Which icon would you like?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       ico4.Text = ofd.FileName; 
       s.ico4 = ofd.FileName; 
      } 
     } 

     private void ibutton5_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Image Files (*.png) | *.png"; 
      ofd.Title = "Which icon would you like?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       ico5.Text = ofd.FileName; 
       s.ico5 = ofd.FileName; 
      } 
     } 

     private void ibutton6_Click(object sender, EventArgs e) 
     { 
      OpenFileDialog ofd = new OpenFileDialog(); 
      ofd.Filter = "Image Files (*.png) | *.png"; 
      ofd.Title = "Which icon would you like?"; 
      DialogResult dr = ofd.ShowDialog(); 
      if (dr == DialogResult.OK) 
      { 
       ico6.Text = ofd.FileName; 
       s.ico6 = ofd.FileName; 
      } 
     } 
    } 
} 

和错误是:

Error 1 Inconsistent accessibility: field type 'SharpDock.Properties.Settings' is less accessible than field 'SharpDock.SettingsWindow.s' C:\Users\Lewis\Documents\Visual Studio 2013\Projects\SharpDock\SharpDock\SettingsWindow.cs 14 36 SharpDock 

请帮帮忙!我被这个错误困住了。

〜刘易斯

+0

请勿张贴所有的代码只有导致错误的部分。你也可以将很多这些方法压缩成1或2种方法。 – deathismyfriend

+0

尝试将's'声明为'private'或'internal' – MrPaulch

回答

1

您需要使用Properties.Settings.Default,你不需要实例化一个变量它。如果你想要一个别名以使代码更小,删除此代码:

public Properties.Settings s = new Properties.Settings(); 

和类声明之前补充一点:

using s = Properties.Settings.Default; 

而作为deathismyfriend评论,你可以通过减少很多代码为重复的代码创建方法。

1

你的Settings保存在Properties.Settings.Default里面,所以你应该使用它而不是声明你自己的设置。另外,您可以将多个控件订阅到同一事件。你应该做的是,订阅你所有的abuttonsaButton_Click事件和ibuttonsiButton_Click事件。这里是你的代码固定:

using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace SharpDock 
{ 
    public partial class SettingsWindow : Form 
    { 
     public SettingsWindow() 
     { 
      InitializeComponent(); 
      LoadSettings(); 
     } 

     private void LoadSettings() 
     { 
      app1.Text = Properties.Settings.Default.app1; 
      app2.Text = Properties.Settings.Default.app2; 
      app3.Text = Properties.Settings.Default.app3; 
      app4.Text = Properties.Settings.Default.app4; 
      app5.Text = Properties.Settings.Default.app5; 
      app6.Text = Properties.Settings.Default.app6; 
      ico1.Text = Properties.Settings.Default.ico1; 
      ico2.Text = Properties.Settings.Default.ico2; 
      ico3.Text = Properties.Settings.Default.ico3; 
      ico4.Text = Properties.Settings.Default.ico4; 
      ico5.Text = Properties.Settings.Default.ico5; 
      ico6.Text = Properties.Settings.Default.ico6; 
     } 

     private void Accept_Click(object sender, EventArgs e) 
     { 
      Properties.Settings.Default.Save(); 
      MessageBox.Show("SharpDock", "You must restart the program for the changes to take effect.", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 

     private void aButton_Click(object sender, EventArgs e) 
     { 
      using (var ofd = new OpenFileDialog()) 
      { 
       ofd.Filter = "Executable Files (*.exe) | *.exe"; 
       ofd.Title = "Which executable would you like to launch?"; 

       if (ofd.ShowDialog() == DialogResult.OK) 
       { 
        if (sender == abutton1) 
        { 
         app1.Text = ofd.FileName; 
         Properties.Settings.Default.app1 = ofd.FileName; 
        } 
        else if (sender == abutton2) 
        { 
         app2.Text = ofd.FileName; 
         Properties.Settings.Default.app2 = ofd.FileName; 
        } 
        else if (sender == abutton3) 
        { 
         app3.Text = ofd.FileName; 
         Properties.Settings.Default.app3 = ofd.FileName; 
        } 
        else if (sender == abutton4) 
        { 
         app4.Text = ofd.FileName; 
         Properties.Settings.Default.app4 = ofd.FileName; 
        } 
        else if (sender == abutton5) 
        { 
         app5.Text = ofd.FileName; 
         Properties.Settings.Default.app5 = ofd.FileName; 
        } 
        else if (sender == abutton6) 
        { 
         app6.Text = ofd.FileName; 
         Properties.Settings.Default.app6 = ofd.FileName; 
        } 
       } 
      } 
     } 

     private void iButton_Click(object sender, EventArgs e) 
     { 
      using (var ofd = new OpenFileDialog()) 
      { 
       ofd.Filter = "Image Files (*.png) | *.png"; 
       ofd.Title = "Which icon would you like?"; 

       if (ofd.ShowDialog() == DialogResult.OK) 
       { 
        if (sender == ibutton1) 
        { 
         ico1.Text = ofd.FileName; 
         Properties.Settings.Default.ico1 = ofd.FileName; 
        } 
        else if (sender == ibutton2) 
        { 
         ico2.Text = ofd.FileName; 
         Properties.Settings.Default.ico2 = ofd.FileName; 
        } 
        else if (sender == ibutton3) 
        { 
         ico3.Text = ofd.FileName; 
         Properties.Settings.Default.ico3 = ofd.FileName; 
        } 
        else if (sender == ibutton4) 
        { 
         ico4.Text = ofd.FileName; 
         Properties.Settings.Default.ico4 = ofd.FileName; 
        } 
        else if (sender == ibutton5) 
        { 
         ico5.Text = ofd.FileName; 
         Properties.Settings.Default.ico5 = ofd.FileName; 
        } 
        else if (sender == ibutton6) 
        { 
         ico6.Text = ofd.FileName; 
         Properties.Settings.Default.ico6 = ofd.FileName; 
        } 
       } 
      } 
     } 
    } 
} 

您还需要你是从你的设置参考的项目设置里面进行设置:

Settings picture