首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >与Properties.Settings不一致的可访问性?

与Properties.Settings不一致的可访问性?
EN

Stack Overflow用户
提问于 2015-07-11 14:52:28
回答 2查看 102关注 0票数 0

我试图编写一个快速的程序,当我将Properties.Settings赋给变量s时,错误就会出现。我试图将它赋值给这个变量,因为我的许多文本框都需要指定一个设置值,而且还有很多设置需要保存。

代码语言:javascript
复制
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;
            }
        }
    }
}

错误是:

代码语言:javascript
复制
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

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

刘易斯

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-11 15:07:37

您需要使用Properties.Settings.Default,并且不需要实例化它的变量。如果希望使用别名使代码更小,请删除以下代码:

代码语言:javascript
复制
public Properties.Settings s = new Properties.Settings();

并在类声明之前添加以下内容:

代码语言:javascript
复制
using s = Properties.Settings.Default;

正如deathismyfriend评论的那样,您可以通过为重复代码创建方法来减少代码。

票数 1
EN

Stack Overflow用户

发布于 2015-07-11 15:19:16

您的Settings保存在Properties.Settings.Default中,因此您应该使用它而不是声明您自己的设置。此外,还可以为同一事件订阅多个控件。您应该做的是,将所有abuttons订阅到aButton_Click事件,将ibuttons订阅到iButton_Click事件。这是您的代码已修复:

代码语言:javascript
复制
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;
                    }
                }
            }
        }
    }
}

您还需要从设置中引用的项在设置中设置:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31358409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档