tags:

views:

83

answers:

2

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, pls help me

thanks in advance..

A: 

Praveen,
I think you are talking of lightbox effect Something like show in URL below:

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

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

If yes, I suggest you use the readymade lightbox js available. I can pitch in with more info. till then go thru this if it matches your requirement.

Regards,
Justin Samuel.

Justin
A: 

On a WinForms Form I set up a ToolStripMenu with 1 ToolStripMenuItem called ToolsStripMenuItemColors. Into its DropDownItems I added 3 more ToolStripMenuItems with the Text properties of "Red", "Green", "Blue".

I hook into their .Click events. In the event handler I determine which item was clicked and set its Clicked property to true. On the others, I set it to 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. 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.

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.

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;
        }
    }
}
jomtois