简体   繁体   English

c#WinForms - 将主窗口的样式传递给对话框

[英]c# WinForms - Pass main window's style to dialog boxes

I was just wondering if there was a simpler way of passing style values from the main form to dialog boxes. 我只是想知道是否有一种更简单的方法将样式值从主窗体传递到对话框。

My application has user-settable style properties for all the different control types; 我的应用程序具有适用于所有不同控件类型的用户可设置样式属性; ForeColor , BackColor , Button ForeColor , Button BackColor , Button FlatStyle etc. I want the dialog boxes to share the same, consistent style. ForeColorBackColor ,Button ForeColor ,Button BackColor ,Button FlatStyle等。我希望对话框共享相同,一致的样式。

At the moment, I pass each of these properties to a dialog boxes like this; 目前,我将每个属性传递给这样的对话框;
(this example shows one of the more simple dialog forms, some have more control types and a lot more style properties) (此示例显示了一个更简单的对话框表单,其中一些具有更多控件类型和更多样式属性)

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MyApplication
{
    public partial class ConfirmDialog : Form
    {
        public Color ButtonBackColour { private get; set; }
        public Color ButtonForeColour { private get; set; }
        public FlatStyle ButtonFlatStyle { private get; set; }

        public string Message { private get; set; }
        public ConfirmDialog()
        {
            InitializeComponent();
        }

        private void ConfirmDialog_Load(object sender, EventArgs e)
        {
            buttonYES.ForeColor = ButtonForeColour;
            buttonCancel.ForeColor = ButtonForeColour;

            buttonYES.BackColor = ButtonBackColour;
            buttonCancel.BackColor = ButtonBackColour;

            buttonYES.FlatStyle = ButtonFlatStyle;
            buttonCancel.FlatStyle = ButtonFlatStyle;

            lblMsg.Text = Message;
        }
    }
}

Ok, now I have a static class which I use to hold the dialog Initializers; 好的,现在我有一个静态类,用于保存对话框Initializers;

using System.Drawing;
using System.Windows.Forms;

namespace MyApplication
{
    public static class ShowDialog
    {
        public static bool ConfirmDialog(string message, Color foreColour, Color backColour, Color buttonBackColour, Color buttonForeColour, FlatStyle buttonFlatStyle)// more style properties to come later on(flatstyle style props etc)
        {
            bool confirm = false;
            using (ConfirmDialog Dialog = new ConfirmDialog()
            {
                Message = message,
                ForeColor = foreColour,
                BackColor = backColour,
                ButtonBackColour = buttonBackColour,
                ButtonForeColour = buttonForeColour,
                ButtonFlatStyle = buttonFlatStyle
            })
            {
                DialogResult dr = Dialog.ShowDialog();
                if (dr == DialogResult.Yes)
                {
                    confirm = true;
                }
                else
                {
                    confirm = false;
                }
            }
            return confirm;
        }
    }
}

And finally, this is the mess of arguments I have to include... 最后,这是我必须包括的一堆争论......

private void buttonDelete_Click(object sender, EventArgs e)
{
    if (ShowDialog.ConfirmDialog("Are you sure you want to delete...?", this.ForeColour, this.BackColour, buttonDelete.BackColour, buttonDelete.ForeColour, buttonDelete.FlatStyle))// any button from main form will do
    {
        // Delete stuff...
    }
}

I am unsure if the static class is redundant, but it will make things a lot easier when the dialog boxes start to mount up. 我不确定静态类是否冗余,但是当对话框开始挂载时,它会使事情变得容易多了。

So instead of having to pass all of these individual style properties around, I was wondering if there is a cleaner way. 因此,我不想传递所有这些单独的样式属性,而是想知道是否有更清洁的方式。 I know there is no Form.Style property. 我知道没有Form.Style属性。 I suppose I could create one. 我想我可以创造一个。 But I wanted to check first with the Gurus to see if I am missing anything. 但我想先跟大师检查一下,看看我是否遗漏了什么。

确保,[formname] .designer.cs中主窗体的所有属性都是公共静态,在de showdialog中你可以用它们调用它们:如果你的窗体叫做Form1:this.ForceColor = Form1.ForceColor是那个那些属性确实是公共静态的

Since you already have a static class defined for this purpose, you can host your style properties there, and reference them from both your forms and your dialogs. 由于您已经为此目的定义了静态类,因此可以在那里托管样式属性,并从表单和对话框中引用它们。 To implement this, your static class becomes more like this: 要实现这一点,你的静态类变得更像这样:

using System.Drawing;
using System.Windows.Forms;

namespace MyApplication
{
    public static class ShowDialog
    {
        public static Color ForeColour { get; set; }
        public static Color BackColour { get; set; }
        public static Color ButtonBackColour { get; set; }
        public static Color ButtonForeColour { get; set; }
        public static FlatStyle ButtonFlatStyle { get; set; }

        public static bool ConfirmDialog(string message)
        {
            bool confirm = false;
            using (ConfirmDialog Dialog = new ConfirmDialog()
            {
                Message = message,
                ForeColor = this.ForeColour,
                BackColor = this.BackColour,
                ButtonBackColour = this.ButtonBackColour,
                ButtonForeColour = this.ButtonForeColour,
                ButtonFlatStyle = this.ButtonFlatStyle
            })
            {
                DialogResult dr = Dialog.ShowDialog();
                if (dr == DialogResult.Yes)
                {
                    confirm = true;
                }
                else
                {
                    confirm = false;
                }
            }
            return confirm;
        }
    }
}

You can set these properties when your app launches, and not only will you not have to pass all these extra parameters into your ConfirmDialog method, but you can also reference them from all of your forms, instead of having to define them individually in each form. 您可以在应用程序启动时设置这些属性,您不仅不必将所有这些额外参数传递到ConfirmDialog方法中,而且还可以从所有表单中引用它们,而不必在每个表单中单独定义它们。 This makes it easy to manage your styles in a single place as well if you want to apply skins or need to make changes later. 这样,如果您想要应用外观或需要稍后进行更改,也可以在单个位置轻松管理样式。

After reading Elemental Pete's answer, I considered creating static properties I could set as the application loads, but instead of adding them to the ShowDialog static class, creating a new public class, implementing INotifyPropertyChanged to update the main form style when the style properties are changed, then creating a public static instance on the main form. 在阅读了Elemental Pete的回答之后,我考虑创建我可以在应用程序加载时设置的静态属性,而不是将它们添加到ShowDialog静态类,创建一个新的公共类,实现INotifyPropertyChanged以在样式属性更改时更新主表单样式,然后在主窗体上创建一个公共静态实例。 This way, I can get or set the properties of [mainformname].Style from anywhere. 这样,我可以从任何地方获取或设置[mainformname] .Style的属性。 I know little of best practices in programming, but this seems like a good approach that does everything I need. 我对编程中的最佳实践知之甚少,但这似乎是一种很好的方法,可以完成我需要的一切。

public class DisplayStyle : INotifyPropertyChanged
    {
        private Color foreColor;
        private Color backColor;

        //Buttons
        private Color buttonForeColor;
        private Color buttonBackColor;
        private FlatStyle buttonFlatStyle;
        private FlatButtonAppearance buttonFlatAppearance;

        //ComboBoxes
        private Color comboForeColor;
        private Color comboBackColor;
        private FlatStyle comboBoxFlatStyle;

        //TextBoxes
        private Color textBoxForeColor;
        private Color textBoxBackColor;

        //CheckBoxes
        private Color checkBoxForeColor;
        private Color checkBoxBackColor;
        private FlatStyle checkBoxFlatStyle;
        private FlatButtonAppearance checkBoxFlatAppearance;

//...public properties, InotifyPropertyChanged Implementation etc...

Over on [mainformname]... 在[mainformname]上...

public static DisplayStyle Style = new DisplayStyle();

This is just another approach I've considered. 这只是我考虑过的另一种方法。

I have found yet another way to store and pass the mainform's style to other forms in the application. 我找到了另一种方法来存储并将mainform的样式传递给应用程序中的其他表单。 This time, I am utilizing the built-in User Properties . 这一次,我正在使用内置的用户属性

It is pretty much the same as my other approach , only instead of saving the values to a static instance of a public class, I save the values to Properties.Settings which creates a .config file in the user's Application Data folder. 它与我的其他方法几乎相同,只是将值保存到公共类的静态实例,而不是将值保存到Properties.Settings ,这会在用户的Application Data文件夹中创建.config文件。 It is really quite convenient, with methods for Reset() , Reload() and Save() as well as property updated events. 使用Reset()Reload()Save()以及属性更新事件的方法非常方便。

So I can create all the properties I need under the settings tab (or create them at run-time). 所以我可以在设置选项卡下创建我需要的所有属性(或者在运行时创建它们)。
So then I can edit property values: 那么我可以编辑属性值:

Properties.Settings.Default.FormForeColour = newColour;

and apply them to the form as the edits occur: 并在编辑发生时将它们应用于表单:

Properties.Settings.Default.PropertyChanged += new PropertyChangedEventHandler(UserSettings_PropChanged);

private void UserSettings_PropChanged(object sender, PropertyChangedEventArgs e)
{
    //update the form's display properties
    //e.g.
    this.ForeColor = Properties.Settings.Default.FormForeColour;

    labelStatus = e.PropertyName + " successfully updated";
}

So whenever I create a dialog box, I can simply set the style properties from the User Settings: 所以每当我创建一个对话框时,我只需从用户设置中设置样式属性:

using (ConfirmDialog Dialog = new ConfirmDialog()
{
    Message = message,
    this.ForeColor = Properties.Settings.Default.FormForeColour,
    this.BackColor = Properties.Settings.Default.FormBackColour,
    this.ButtonBackColour = Properties.Settings.Default.ButtonBackColour,
    this.ButtonForeColour = Properties.Settings.Default.ButtonForeColour,
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM