Example withContextMenuStrip:-



Add a new Form i.e.,FormCMenu to the Windows Application WABasics

Create one label,one textbox and one contextMenuStrip ,one ColorDialog,one fontDialog& set the following properties to them

[contextMenuStrip1,colorDialog1,fontDialog1 will only appear in Component tray only]

label1:

     Text                 :         Enter Any Data
     Name     :         lblSample

textBox1:

     Name     :         txtSample

contextMenuStrip1:-

Name          :         contextMenuStrip1
         
ToolStripMenuItem s Name s

1.     Font            :         smnuFont
2.     ForeColor   :         smnuForeColor
BackColor  :         smnuBackColor



Design :-



Generate the Event Handlers for smnuFont ,smnuForeColor,smnuBackColor Click
 Events

& write the following code in Source[FormCMenu.cs] 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WABasics
{
    public partial class FormCMenu : Form
    {
        public FormCMenu()
        {
            InitializeComponent();
        }

        private void smnuFont_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowDialog();
            txtSample.Font = fontDialog1.Font;
        }

        private void smnuForeColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            txtSample.ForeColor = colorDialog1.Color;
        }

        private void smnuBackColor_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            txtSample.BackColor = colorDialog1.Color;
        }
    }
}





Result :-
Run the application & enter any data in textbox
Right click in textbox select font & set any font style/font size


Right Click in the TextBoox

No comments:

Post a Comment