Mega Code Archive

 
Categories / C# / GUI Windows Form
 

Use ToolStripMenuItem to set font size

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; enum TextFontSize {     FontSizeHuge = 30,     FontSizeNormal = 20,     FontSizeTiny = 8 } public class MainWindow : Form {     private TextFontSize currFontSize = TextFontSize.FontSizeNormal;     private ToolStripMenuItem currentCheckedItem;     public MainWindow() {         InitializeComponent();         currentCheckedItem = normalToolStripMenuItem;         currentCheckedItem.Checked = true;         this.toolStripTextBoxColor.LostFocus += new EventHandler(toolStripTextBoxColor_LostFocus);     }     private void exitToolStripMenuItem_Click(object sender, EventArgs e) {         Application.Exit();     }     void toolStripTextBoxColor_LostFocus(object sender, EventArgs e) {         BackColor = Color.FromName(toolStripTextBoxColor.Text);     }     private void ContextMenuItemSelection_Clicked(object sender, EventArgs e) {         currentCheckedItem.Checked = false;         ToolStripMenuItem miClicked = miClicked = (ToolStripMenuItem)sender;         if (miClicked.Name == "hugeToolStripMenuItem") {             currFontSize = TextFontSize.FontSizeHuge;             currentCheckedItem = hugeToolStripMenuItem;         }         if (miClicked.Name == "normalToolStripMenuItem") {             currFontSize = TextFontSize.FontSizeNormal;             currentCheckedItem = normalToolStripMenuItem;         }         if (miClicked.Name == "tinyToolStripMenuItem") {             currFontSize = TextFontSize.FontSizeTiny;             currentCheckedItem = tinyToolStripMenuItem;         }         currentCheckedItem.Checked = true;         Invalidate();     }     private void MainWindow_Paint(object sender, PaintEventArgs e) {         Graphics g = e.Graphics;         g.DrawString("Right click on me...", new Font("Times New Roman", (float)currFontSize), new SolidBrush(Color.Black), 50, 50);     }     private void InitializeComponent() {         this.mainMenuStrip = new System.Windows.Forms.MenuStrip();         this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();         this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();         this.changeBackgroundColorToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();         this.toolStripTextBoxColor = new System.Windows.Forms.ToolStripTextBox();         this.hugeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();         this.normalToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();         this.tinyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();         this.mainMenuStrip.SuspendLayout();         this.fontSizeContextStrip.SuspendLayout();         this.SuspendLayout();         //          this.mainMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {             this.fileToolStripMenuItem,             this.changeBackgroundColorToolStripMenuItem});         this.mainMenuStrip.Location = new System.Drawing.Point(0, 0);         this.mainMenuStrip.Name = "mainMenuStrip";         this.mainMenuStrip.Size = new System.Drawing.Size(300, 24);         this.mainMenuStrip.TabIndex = 0;         this.mainMenuStrip.Text = "menuStrip1";         this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {             this.exitToolStripMenuItem});         this.fileToolStripMenuItem.Text = "&File";         this.exitToolStripMenuItem.Text = "E&xit";         this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);         //          // changeBackgroundColorToolStripMenuItem         //          this.changeBackgroundColorToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {             this.toolStripTextBoxColor});         this.changeBackgroundColorToolStripMenuItem.Name = "changeBackgroundColorToolStripMenuItem";         this.changeBackgroundColorToolStripMenuItem.Text = "Change Background Color";         //          // toolStripTextBoxColor         //          this.toolStripTextBoxColor.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.ImageAndText;         this.toolStripTextBoxColor.Name = "toolStripTextBoxColor";         this.toolStripTextBoxColor.Size = new System.Drawing.Size(100, 21);         this.fontSizeContextStrip.Enabled = true;         this.fontSizeContextStrip.GripMargin = new System.Windows.Forms.Padding(2);         this.fontSizeContextStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {             this.hugeToolStripMenuItem,             this.normalToolStripMenuItem,             this.tinyToolStripMenuItem});         this.fontSizeContextStrip.Location = new System.Drawing.Point(25, 90);         this.fontSizeContextStrip.Name = "contextMenuStrip1";         this.fontSizeContextStrip.RightToLeft = System.Windows.Forms.RightToLeft.No;         this.fontSizeContextStrip.Size = new System.Drawing.Size(97, 70);         this.hugeToolStripMenuItem.Text = "Huge";         this.hugeToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);         this.normalToolStripMenuItem.Text = "Normal";         this.normalToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);         this.tinyToolStripMenuItem.Text = "Tiny";         this.tinyToolStripMenuItem.Click += new System.EventHandler(this.ContextMenuItemSelection_Clicked);         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;         this.ClientSize = new System.Drawing.Size(300, 145);         this.ContextMenuStrip = this.fontSizeContextStrip;         this.Controls.Add(this.mainMenuStrip);         this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainWindow_Paint);         this.mainMenuStrip.ResumeLayout(false);         this.fontSizeContextStrip.ResumeLayout(false);         this.ResumeLayout(false);         this.PerformLayout();     }     private System.Windows.Forms.MenuStrip mainMenuStrip;     private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;     private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem;     private System.Windows.Forms.ToolStripMenuItem changeBackgroundColorToolStripMenuItem;     private System.Windows.Forms.ToolStripTextBox toolStripTextBoxColor;     private System.Windows.Forms.ContextMenuStrip fontSizeContextStrip;     private System.Windows.Forms.ToolStripMenuItem hugeToolStripMenuItem;     private System.Windows.Forms.ToolStripMenuItem normalToolStripMenuItem;     private System.Windows.Forms.ToolStripMenuItem tinyToolStripMenuItem;     [STAThread]     static void Main() {         Application.EnableVisualStyles();         Application.Run(new MainWindow());     } }