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();
}
}
}
Imports
System.Drawing.Text
Public
Class Form1
Inherits System.Windows.Forms.Form
#Region
" Windows Form Designer generated code "
Public Sub
New()
MyBase.New()
'This
call is required by the Windows Form Designer.
InitializeComponent()
'Add
any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component
list.
Protected Overloads
Overrides Sub
Dispose(ByVal disposing
As Boolean)
If
disposing Then
If
Not (components Is
Nothing) Then
components.Dispose()
End
If
End
If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As
System.ComponentModel.IContainer
'NOTE: The following procedure is required by the
Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents
textBox1 As System.Windows.Forms.TextBox
Friend WithEvents
textBox2 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.textBox1
= New System.Windows.Forms.TextBox
Me.textBox2
= New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'textBox1
'
Me.textBox1.Location
= New System.Drawing.Point(8, 8)
Me.textBox1.Multiline
= True
Me.textBox1.Name
= "textBox1"
Me.textBox1.Size
= New System.Drawing.Size(392, 152)
Me.textBox1.TabIndex
= 2
Me.textBox1.Text
= ""
'
'textBox2
'
Me.textBox2.Location
= New System.Drawing.Point(8, 168)
Me.textBox2.Multiline
= True
Me.textBox2.Name
= "textBox2"
Me.textBox2.ScrollBars
= System.Windows.Forms.ScrollBars.Vertical
Me.textBox2.Size
= New System.Drawing.Size(392, 160)
Me.textBox2.TabIndex
= 1
Me.textBox2.Text
= ""
'
'Form1
'
Me.AutoScaleBaseSize
= New System.Drawing.Size(5, 13)
Me.ClientSize
= New System.Drawing.Size(408, 334)
Me.Controls.Add(Me.textBox1)
Me.Controls.Add(Me.textBox2)
Me.FormBorderStyle
= System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Name
= "Form1"
Me.Text
= "Form1"
Me.ResumeLayout(False)
End Sub
#End
Region
Private Sub
textBox1_TextChanged(ByVal sender
As System.Object, ByVal
e As System.EventArgs)
Handles textBox1.TextChanged
Me.MeasureAll(Me.textBox1.Text)
End Sub
Private Sub
MeasureAll(ByVal sentence
As String)
Dim
g As Graphics = Me.CreateGraphics()
g.TextRenderingHint
= System.Drawing.Text.TextRenderingHint.AntiAlias
Dim
words() As String
= sentence.Split(New
Char() {" "c})
Dim
sb As System.Text.StringBuilder =
New System.Text.StringBuilder
Dim
s As String
For
Each s In words
Dim
sf As SizeF = g.MeasureString(s,
Me.textBox1.Font, 1024, StringFormat.GenericTypographic)
sb.Append(String.Format("{0}"
& vbTab & "width={1} height={2}" & vbCrLf, s, sf.Width, sf.Height))
Next
Me.textBox2.Text
= sb.ToString()
g.Dispose()
End Sub
End
Class