简体   繁体   English

从菜单项中选择图像时如何更改表格的背景色

[英]how to change background color of form when image selected from menuitems

In my form, I have application to change background color of form, when the menu item selection changed, on which event the code is needs to write? 在我的表单中,我有应用程序来更改表单的背景色,当菜单项选择更改时,需要在哪个事件上编写代码? Please help me. 请帮我。

I think you are talking of lightbox effect Something like show in URL below: 我认为您是在谈论灯箱效果,例如在以下网址中显示的内容:

http://www.huddletogether.com/projects/lightbox2/ http://www.huddletogether.com/projects/lightbox2/

http://en.wikipedia.org/wiki/Lightbox_%28JavaScript%29 http://en.wikipedia.org/wiki/Lightbox_%28JavaScript%29

If yes, I suggest you use the readymade lightbox js available. 如果是,建议您使用可用的现成灯箱js。 I can pitch in with more info. 我可以提供更多信息。 till then go thru this if it matches your requirement. 在此之前,如果符合您的要求,请遍历此内容。

On a WinForms Form I set up a ToolStripMenu with 1 ToolStripMenuItem called ToolsStripMenuItemColors. 在WinForms表单上,我设置了一个ToolStripMenuItem,其中包含1个名为ToolsStripMenuItemColors的ToolStripMenuItem。 Into its DropDownItems I added 3 more ToolStripMenuItems with the Text properties of "Red", "Green", "Blue". 在其DropDownItems中,我添加了另外3个具有Text属性“ Red”,“ Green”,“ Blue”的ToolStripMenuItems。

I hook into their .Click events. 我迷上了他们的.Click事件。 In the event handler I determine which item was clicked and set its Clicked property to true. 在事件处理程序中,我确定单击了哪个项目并将其Clicked属性设置为true。 On the others, I set it to false. 在其他情况下,我将其设置为false。 These two steps are just for display purposes, not totally necessary. 这两个步骤仅用于显示目的,并非完全必要。 I then use the .Text property of the selected item in a case statement to determine which color to set the Form's BackColor to. 然后,在case语句中使用所选项目的.Text属性来确定将窗体的BackColor设置为哪种颜色。 This is not the most elegant way to figure this out, but it should get you started. 这不是解决这个问题的最优雅的方法,但是它应该可以帮助您入门。 A possible better way would be to store the Color in the ToolStripMenuItem's Tag property and avoid a case statement based on strings. 一种更好的可能方法是将Color存储在ToolStripMenuItem的Tag属性中,并避免基于字符串的case语句。

There is also a CheckOnClick property and a CheckedChanged event available to use, but I thought handling the Click event would be better since you would only want one selection checked at a time. 还有一个CheckOnClick属性和一个CheckedChanged事件可以使用,但是我认为处理Click事件会更好,因为您一次只希望选择一个选项。

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            foreach (ToolStripMenuItem item in toolStripMenuItemColors.DropDownItems)
            {
                item.Click += ItemClick;
            }
        }

        private void ItemClick(object sender, EventArgs e)
        {
            foreach (ToolStripMenuItem item in toolStripMenuItemColors.DropDownItems)
            {
                if (item.Equals(sender))
                {
                    item.Checked = true;
                }
                else
                {
                    item.Checked = false;
                }
            }

            string color =((ToolStripMenuItem)sender).Text;
            Color newColor = this.BackColor;

            switch (color)
            {
                case "Red":
                    newColor = Color.Red;
                    break;
                case "Blue":
                    newColor = Color.Blue;
                    break;
                case "Green":
                    newColor = Color.Green;
                    break;
                default:
                    break;
            }
            BackColor = newColor;
        }
    }
}

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

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