Use ToolStripMenuItem to set font size : ToolStripMenuItem « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ToolStripMenuItemScreenshots 
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)5050);
    }

    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(00);
        this.mainMenuStrip.Name = "mainMenuStrip";
        this.mainMenuStrip.Size = new System.Drawing.Size(30024);
        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(10021);

        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(2590);
        this.fontSizeContextStrip.Name = "contextMenuStrip1";
        this.fontSizeContextStrip.RightToLeft = System.Windows.Forms.RightToLeft.No;
        this.fontSizeContextStrip.Size = new System.Drawing.Size(9770);

        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(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(300145);
        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());
    }
}

 
Related examples in the same category
1.ToolStripMenuItem in actionToolStripMenuItem in action
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.