在主窗体中单击按钮时更改按钮颜色,即使在继承主窗体的窗体上也保留该按钮颜色

问题描述 投票:0回答:1

Output image- button color change on main but not on derived form我有两种形式。一种是Main form,另一种是继承main form的form2。 我在主窗体上的按钮单击事件上更改按钮颜色,并希望在 form2 上更改按钮的颜色。 这里的问题是更改主窗体上的按钮颜色,但不更改窗体2上的按钮颜色。请发送帮助。

这是我的代码。 //Form1主窗体

namespace BtnColor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Button btn = null;
        protected void BtnSetting(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            button.BackColor = Color.FromArgb(151, 242, 0);
            if(btn != null)
            {
                btn.BackColor = Color.LightSkyBlue;
            }
            btn = button;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            BtnSetting(button1, null);
            Form2 f2 = new Form2();
            f2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            BtnSetting(button2, null);
            Form3 f3 = new Form3();
            f3.Show();
            
        }
        private void Form1_Load(object sender, EventArgs e)
        {
           // button1.BackColor = Color.Red;
        }
    }
}

//Form 2
namespace BtnColor
{
    public partial class Form2 : Form1
    {
        public Form2()
        {
            InitializeComponent();
        }
    }
}
c# winforms button colors
1个回答
0
投票

为了详细说明 Ňɏssa Pøngjǣrdenlarp 的精彩评论,表单继承更多的是一种设计时行为,其中子类继承您指定的 layout 属性。但在运行时,当您创建

InheritedClass
的实例时,它的(例如)
buttonSetting
的实例也是不同的。

因此,为了“镜像”颜色变化行为,我们必须在

buttonSetting.BackColor
发生变化时添加一个处理程序,并遍历应用程序中的所有表单来同步更改。

buttonSetting.BackColorChanged += (sender, e) =>
{
    if (
        sender is Button srce
        &&
        string.Equals(srce.Name, "buttonSetting"))
    {
        foreach (var form in Application.OpenForms.OfType<MainForm>())
        {
            // Find the button on each form and update its color.
            if (form.Controls["buttonSetting"] is Button dest)
            {
                dest.BackColor = srce.BackColor;
            }
        }
    }
};

示例

这是带有一些上下文的片段:

继承表单类
public class InheritedForm : MainForm 
{
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        if(e.CloseReason.Equals(CloseReason.UserClosing))
        {
            e.Cancel = true;
            Hide();
            Application.OpenForms["MainForm"]?.BringToFront(); // Workaroud 'last visible child' issue
        }
        base.OnFormClosing(e);
    }
}

MainForm 运行时
public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        buttonSetting.BackColorChanged += (sender, e) =>
        {
            if (
                sender is Button srce
                &&
                string.Equals(srce.Name, "buttonSetting"))
            {
                foreach (var form in Application.OpenForms.OfType<MainForm>())
                {
                    // Find the button on each form and update its color.
                    if (form.Controls["buttonSetting"] is Button dest)
                    {
                        dest.BackColor = srce.BackColor;
                    }
                }
            }
        };

        // Toggle color
        buttonSetting.Click += (sender, e) =>
            buttonSetting.BackColor = buttonSetting.BackColor.Equals(Color.LightSkyBlue) ?
            Color.FromArgb(151, 242, 0) : Color.LightSkyBlue;

        buttonShowForm1.Click += (sender, e) =>
        {
            var mainForm = Application.OpenForms["MainForm"];
            if (Application.OpenForms["f1"] is Form form)                   
            {
                if(!form.Visible) form.Show(mainForm);
            }
            else
            {
                new InheritedForm
                {
                    Text = "Inherited Form 1",
                    Name = "f1",
                    StartPosition = FormStartPosition.Manual,
                    Size = mainForm.Size,
                    Location = new Point(mainForm.Left + mainForm.Width + 10, mainForm.Top),
                }.Show(mainForm);
            }
        };
        buttonShowForm2.Click += (sender, e) =>
        {
            var mainForm = Application.OpenForms["MainForm"];
            if (Application.OpenForms["f2"] is Form form)
            {
                if (!form.Visible) form.Show(mainForm);
            }
            else
            {
                new InheritedForm
                { 
                    Text = "Inherited Form 2",
                    Name = "f2",
                    StartPosition = FormStartPosition.Manual,
                    Size = mainForm.Size,
                    Location = new Point(
                        mainForm.Left + mainForm.Width + 10, 
                        mainForm.Top + mainForm.Height + 10),
                }.Show(mainForm);
            }
        };
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.