Text wrap: vertical, horizontal and trim : String Format « 2D Graphics « 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » 2D Graphics » String FormatScreenshots 
Text wrap: vertical, horizontal and trim
Text wrap: vertical, horizontal and trim


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.ComboBox lstAlignmentH;
    private System.Windows.Forms.ComboBox lstTrimming;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.ComboBox lstAlignmentV;
        public Form1() {
            InitializeComponent();
      ResizeRedraw = true;
      lstAlignmentH.DataSource = Enum.GetNames(typeof(StringAlignment));
      lstAlignmentV.DataSource = Enum.GetNames(typeof(StringAlignment));
      lstTrimming.DataSource = Enum.GetNames(typeof(StringTrimming));
        }

    private void TextWrap_Paint(object sender, PaintEventArgs e)
    {
      string text = "Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Alignment Center each line of text.Center each line of textCenter each line of textCenter each line of textCenter each line of textCenter each line of text";
          
      StringFormat stringFormat = new StringFormat();
      stringFormat.Alignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), lstAlignmentH.Text);
      stringFormat.LineAlignment = (StringAlignment)Enum.Parse(typeof(StringAlignment), lstAlignmentV.Text);
      
      stringFormat.Trimming = (StringTrimming)Enum.Parse(typeof(StringTrimming), lstTrimming.Text);

      Font font = new Font("Verdana"12);
      Rectangle rect = new Rectangle(30110, Width - 60, Height - 160);
      e.Graphics.DrawString(text, font, Brushes.Black, rect, stringFormat);

      Pen pen = Pens.Black;
      e.Graphics.DrawRectangle(pen, rect);
    }

    private void lst_Changed(object sender, EventArgs e)
    {
      Invalidate();
    }
    private void InitializeComponent()
    {
      this.label1 = new System.Windows.Forms.Label();
      this.label2 = new System.Windows.Forms.Label();
      this.lstAlignmentH = new System.Windows.Forms.ComboBox();
      this.lstTrimming = new System.Windows.Forms.ComboBox();
      this.label3 = new System.Windows.Forms.Label();
      this.lstAlignmentV = new System.Windows.Forms.ComboBox();
      this.SuspendLayout();
      // 
      // label1
      // 
      this.label1.AutoSize = true;
      this.label1.Location = new System.Drawing.Point(1219);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(11313);
      this.label1.TabIndex = 0;
      this.label1.Text = "Alignment (Horizontal):";
      // 
      // label2
      // 
      this.label2.AutoSize = true;
      this.label2.Location = new System.Drawing.Point(1274);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(4913);
      this.label2.TabIndex = 1;
      this.label2.Text = "Trimming:";
      // 
      // lstAlignmentH
      // 
      this.lstAlignmentH.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstAlignmentH.FormattingEnabled = true;
      this.lstAlignmentH.Location = new System.Drawing.Point(13116);
      this.lstAlignmentH.Name = "lstAlignmentH";
      this.lstAlignmentH.Size = new System.Drawing.Size(12121);
      this.lstAlignmentH.TabIndex = 2;
      this.lstAlignmentH.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);
      // 
      // lstTrimming
      // 
      this.lstTrimming.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstTrimming.FormattingEnabled = true;
      this.lstTrimming.Location = new System.Drawing.Point(13169);
      this.lstTrimming.Name = "lstTrimming";
      this.lstTrimming.Size = new System.Drawing.Size(12121);
      this.lstTrimming.TabIndex = 3;
      this.lstTrimming.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);
      // 
      // label3
      // 
      this.label3.AutoSize = true;
      this.label3.Location = new System.Drawing.Point(1247);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(10013);
      this.label3.TabIndex = 4;
      this.label3.Text = "Alignment (Vertical):";
      // 
      // lstAlignmentV
      // 
      this.lstAlignmentV.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
      this.lstAlignmentV.FormattingEnabled = true;
      this.lstAlignmentV.Location = new System.Drawing.Point(13142);
      this.lstAlignmentV.Name = "lstAlignmentV";
      this.lstAlignmentV.Size = new System.Drawing.Size(12121);
      this.lstAlignmentV.TabIndex = 5;
      this.lstAlignmentV.SelectedIndexChanged += new System.EventHandler(this.lst_Changed);
      // 
      // TextWrap
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(319296);
      this.Controls.Add(this.lstAlignmentV);
      this.Controls.Add(this.label3);
      this.Controls.Add(this.lstTrimming);
      this.Controls.Add(this.lstAlignmentH);
      this.Controls.Add(this.label2);
      this.Controls.Add(this.label1);
      this.Font = new System.Drawing.Font("Tahoma"8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.Name = "TextWrap";
      this.Text = "Text Wrap";
      this.Paint += new System.Windows.Forms.PaintEventHandler(this.TextWrap_Paint);
      this.ResumeLayout(false);
      this.PerformLayout();

    }
      [STAThread]
      static void Main()
      {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
      }

}


           
       
Related examples in the same category
1.Text Columns
2.String format flag: No WrapString format flag: No Wrap
3.String Format Flag: Direction VerticalString Format Flag: Direction Vertical
4.Create a StringFormat object, and set the tab stops, in pixelsCreate a StringFormat object, and set the tab stops, in pixels
5.StringFormat: Alignment, LineAlignment
6.StringFormat.GenericTypographic
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.