In Depth Banner
Skip Navigation Links

Select your preferred language

Listing 3. The TextWords demonstration program

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

 

namespace TextWords

{

  /// <summary>

  /// Summary description for Form1.

  /// </summary>

  public class Form1 : System.Windows.Forms.Form

  {

    private System.Windows.Forms.TextBox textBox1;

    private System.Windows.Forms.TextBox textBox2;

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.Container components = null;

 

    public Form1()

    {

      //

      // Required for Windows Form Designer support

      //

      InitializeComponent();

 

      //

      // TODO: Add any constructor code after InitializeComponent call

      //

    }

 

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    protected override void Dispose( bool disposing )

    {

      if( disposing )

      {

        if (components != null)

        {

          components.Dispose();

        }

      }

      base.Dispose( disposing );

    }

 

    #region Windows Form Designer generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

      this.textBox1 = new System.Windows.Forms.TextBox();

      this.textBox2 = new System.Windows.Forms.TextBox();

      this.SuspendLayout();

      //

      // textBox1

      //

      this.textBox1.Location = new System.Drawing.Point(8, 8);

      this.textBox1.Multiline = true;

      this.textBox1.Name = "textBox1";

      this.textBox1.Size = new System.Drawing.Size(392, 152);

      this.textBox1.TabIndex = 0;

      this.textBox1.Text = "";

      this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);

      //

      // textBox2

      //

      this.textBox2.Location = new System.Drawing.Point(8, 168);

      this.textBox2.Multiline = true;

      this.textBox2.Name = "textBox2";

      this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;

      this.textBox2.Size = new System.Drawing.Size(392, 160);

      this.textBox2.TabIndex = 0;

      this.textBox2.Text = "";

      //

      // Form1

      //

      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

      this.ClientSize = new System.Drawing.Size(408, 334);

      this.Controls.Add(this.textBox1);

      this.Controls.Add(this.textBox2);

      this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;

      this.Name = "Form1";

      this.Text = "Form1";

      this.ResumeLayout(false);

 

    }

    #endregion

 

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

      Application.Run(new Form1());

    }

 

    private void textBox1_TextChanged(object sender, System.EventArgs e)

    {

      MeasureAll(this.textBox1.Text);

    }

 

    private void MeasureAll(string sentence)

    {

      Graphics g=this.CreateGraphics();

      g.TextRenderingHint=System.Drawing.Text.TextRenderingHint.AntiAlias;

      string[] words=sentence.Split(new char[]{' '});

      System.Text.StringBuilder sb=new System.Text.StringBuilder();

      foreach(string s in words)

      {

        SizeF sf = g.MeasureString(s,this.textBox1.Font,1024,StringFormat.GenericTypographic);

        sb.Append(string.Format("\"{0}\"\t\twidth={1} height={2}\r\n",s, sf.Width,sf.Height));          

      }

      this.textBox2.Text=sb.ToString();

      g.Dispose();

    }

  }

}

 

Use your Back button to return to the article

Copyright © Bob Powell 2003-2009. All rights reserved