我正在尝试编写一个快速程序,当我将Properties.Settings分配给变量s时出现错误。 我试图将它分配给这个变量,因为我的很多文本框需要分配一个设置值,还因为有很多设置需要保存。
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)
您需要使用Properties.Settings.Default,而不需要为其实例化变量。如果您想要一个别名来缩小代码,请删除以下代码:
public Properties.Settings s = new Properties.Settings();
在类声明之前添加:
using s = Properties.Settings.Default;
正如deathismyfriend评论的那样,你可以通过为重复代码创建方法来减少代码。
答案 1 :(得分:1)
您的Settings
已保存在Properties.Settings.Default
内,因此您应该使用它而不是声明自己的设置。此外,您可以为同一事件订阅多个控件。您应该做的是,订阅所有abuttons
到aButton_Click
个活动以及ibuttons
到iButton_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;
}
}
}
}
}
}
您还需要在设置中设置您从设置中引用的项目: