VB.Net Tutorial/GUI/RichTextBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 19:40, 26 мая 2010
Содержание
- 1 Change RichTextBox Selection color
- 2 Change RichTextBox Selection Font
- 3 Copy and paste between RichTextBox
- 4 Find text in RichTextBox
- 5 Load txt file to RichTextBox
- 6 Print view text in RichTextBox
- 7 RichTextBox Redo/Undo
- 8 Save Rtf file from RichTextBox and Read Rtf file to RichTextBox
- 9 Save text in RichTextBox into a file
- 10 Simple Editor based on RichTextBox
Change RichTextBox Selection color
<source lang="vbnet">Option Strict On Imports System.IO Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RichTextBoxSelectionColor
public Shared Sub Main Application.Run(New Form1) End Sub
End class
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 Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents btnChangeColor As System.Windows.Forms.Button Friend WithEvents btnChangeFont As System.Windows.Forms.Button Friend WithEvents btnSaveRTF As System.Windows.Forms.Button Friend WithEvents btnOpenRTF As System.Windows.Forms.Button Friend WithEvents btnWriteControls As System.Windows.Forms.Button Friend WithEvents btnReadControls As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents TextBox3 As System.Windows.Forms.TextBox "Required by the Windows Form Designer Private components As System.ruponentModel.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. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Panel1 = New System.Windows.Forms.Panel() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox() Me.ColorDialog1 = New System.Windows.Forms.ColorDialog() Me.FontDialog1 = New System.Windows.Forms.FontDialog() Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog() Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.btnChangeColor = New System.Windows.Forms.Button() Me.btnChangeFont = New System.Windows.Forms.Button() Me.btnSaveRTF = New System.Windows.Forms.Button() Me.btnOpenRTF = New System.Windows.Forms.Button() Me.btnWriteControls = New System.Windows.Forms.Button() Me.btnReadControls = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.TextBox2 = New System.Windows.Forms.TextBox() Me.TextBox3 = New System.Windows.Forms.TextBox() Me.Panel1.SuspendLayout() Me.SuspendLayout() " "Panel1 " Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnReadControls, Me.btnWriteControls, Me.btnOpenRTF, Me.btnSaveRTF, Me.btnChangeFont, Me.btnChangeColor}) Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom Me.Panel1.Location = New System.Drawing.Point(0, 222) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(656, 64) Me.Panel1.TabIndex = 0 " "RichTextBox1 " Me.RichTextBox1.Location = New System.Drawing.Point(16, 8) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(336, 176) Me.RichTextBox1.TabIndex = 1 Me.RichTextBox1.Text = "Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they w" & _ "ere perfectly normal, thank you very much." " "btnChangeColor " Me.btnChangeColor.Location = New System.Drawing.Point(24, 24) Me.btnChangeColor.Name = "btnChangeColor" Me.btnChangeColor.Size = New System.Drawing.Size(88, 32) Me.btnChangeColor.TabIndex = 0 Me.btnChangeColor.Text = "Change Color" " "btnChangeFont " Me.btnChangeFont.Location = New System.Drawing.Point(128, 24) Me.btnChangeFont.Name = "btnChangeFont" Me.btnChangeFont.Size = New System.Drawing.Size(88, 32) Me.btnChangeFont.TabIndex = 1 Me.btnChangeFont.Text = "Change Font" " "btnSaveRTF " Me.btnSaveRTF.Location = New System.Drawing.Point(232, 24) Me.btnSaveRTF.Name = "btnSaveRTF" Me.btnSaveRTF.Size = New System.Drawing.Size(88, 32) Me.btnSaveRTF.TabIndex = 2 Me.btnSaveRTF.Text = "Save RTF" " "btnOpenRTF " Me.btnOpenRTF.Location = New System.Drawing.Point(336, 24) Me.btnOpenRTF.Name = "btnOpenRTF" Me.btnOpenRTF.Size = New System.Drawing.Size(88, 32) Me.btnOpenRTF.TabIndex = 3 Me.btnOpenRTF.Text = "Open RTF" " "btnWriteControls " Me.btnWriteControls.Location = New System.Drawing.Point(440, 24) Me.btnWriteControls.Name = "btnWriteControls" Me.btnWriteControls.Size = New System.Drawing.Size(88, 32) Me.btnWriteControls.TabIndex = 4 Me.btnWriteControls.Text = "Write Controls" " "btnReadControls " Me.btnReadControls.Location = New System.Drawing.Point(544, 24) Me.btnReadControls.Name = "btnReadControls" Me.btnReadControls.Size = New System.Drawing.Size(88, 32) Me.btnReadControls.TabIndex = 5 Me.btnReadControls.Text = "Read Controls" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(400, 24) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(232, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "TextBox1" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(400, 68) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(232, 20) Me.TextBox2.TabIndex = 3 Me.TextBox2.Text = "TextBox2" " "TextBox3 " Me.TextBox3.Location = New System.Drawing.Point(400, 112) Me.TextBox3.Name = "TextBox3" Me.TextBox3.Size = New System.Drawing.Size(232, 20) Me.TextBox3.TabIndex = 4 Me.TextBox3.Text = "TextBox3" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(656, 286) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox3, Me.TextBox2, Me.TextBox1, Me.RichTextBox1, Me.Panel1}) Me.Panel1.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Private Sub btnChangeColor_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnChangeColor.Click Dim theColor As System.Drawing.Color ColorDialog1.AllowFullOpen = True ColorDialog1.AnyColor = True ColorDialog1.ShowDialog() theColor = ColorDialog1.Color btnChangeColor.ForeColor = theColor RichTextBox1.SelectionColor = theColor End Sub
End Class</source>
Change RichTextBox Selection Font
<source lang="vbnet">Option Strict On Imports System.IO Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RichTextBoxSelectionFont
public Shared Sub Main Application.Run(New Form1) End Sub
End class
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 Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents btnChangeColor As System.Windows.Forms.Button Friend WithEvents btnChangeFont As System.Windows.Forms.Button Friend WithEvents btnSaveRTF As System.Windows.Forms.Button Friend WithEvents btnOpenRTF As System.Windows.Forms.Button Friend WithEvents btnWriteControls As System.Windows.Forms.Button Friend WithEvents btnReadControls As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents TextBox3 As System.Windows.Forms.TextBox "Required by the Windows Form Designer Private components As System.ruponentModel.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. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Panel1 = New System.Windows.Forms.Panel() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox() Me.ColorDialog1 = New System.Windows.Forms.ColorDialog() Me.FontDialog1 = New System.Windows.Forms.FontDialog() Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog() Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.btnChangeColor = New System.Windows.Forms.Button() Me.btnChangeFont = New System.Windows.Forms.Button() Me.btnSaveRTF = New System.Windows.Forms.Button() Me.btnOpenRTF = New System.Windows.Forms.Button() Me.btnWriteControls = New System.Windows.Forms.Button() Me.btnReadControls = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.TextBox2 = New System.Windows.Forms.TextBox() Me.TextBox3 = New System.Windows.Forms.TextBox() Me.Panel1.SuspendLayout() Me.SuspendLayout() " "Panel1 " Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnReadControls, Me.btnWriteControls, Me.btnOpenRTF, Me.btnSaveRTF, Me.btnChangeFont, Me.btnChangeColor}) Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom Me.Panel1.Location = New System.Drawing.Point(0, 222) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(656, 64) Me.Panel1.TabIndex = 0 " "RichTextBox1 " Me.RichTextBox1.Location = New System.Drawing.Point(16, 8) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(336, 176) Me.RichTextBox1.TabIndex = 1 Me.RichTextBox1.Text = "Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they w" & _ "ere perfectly normal, thank you very much." " "btnChangeColor " Me.btnChangeColor.Location = New System.Drawing.Point(24, 24) Me.btnChangeColor.Name = "btnChangeColor" Me.btnChangeColor.Size = New System.Drawing.Size(88, 32) Me.btnChangeColor.TabIndex = 0 Me.btnChangeColor.Text = "Change Color" " "btnChangeFont " Me.btnChangeFont.Location = New System.Drawing.Point(128, 24) Me.btnChangeFont.Name = "btnChangeFont" Me.btnChangeFont.Size = New System.Drawing.Size(88, 32) Me.btnChangeFont.TabIndex = 1 Me.btnChangeFont.Text = "Change Font" " "btnSaveRTF " Me.btnSaveRTF.Location = New System.Drawing.Point(232, 24) Me.btnSaveRTF.Name = "btnSaveRTF" Me.btnSaveRTF.Size = New System.Drawing.Size(88, 32) Me.btnSaveRTF.TabIndex = 2 Me.btnSaveRTF.Text = "Save RTF" " "btnOpenRTF " Me.btnOpenRTF.Location = New System.Drawing.Point(336, 24) Me.btnOpenRTF.Name = "btnOpenRTF" Me.btnOpenRTF.Size = New System.Drawing.Size(88, 32) Me.btnOpenRTF.TabIndex = 3 Me.btnOpenRTF.Text = "Open RTF" " "btnWriteControls " Me.btnWriteControls.Location = New System.Drawing.Point(440, 24) Me.btnWriteControls.Name = "btnWriteControls" Me.btnWriteControls.Size = New System.Drawing.Size(88, 32) Me.btnWriteControls.TabIndex = 4 Me.btnWriteControls.Text = "Write Controls" " "btnReadControls " Me.btnReadControls.Location = New System.Drawing.Point(544, 24) Me.btnReadControls.Name = "btnReadControls" Me.btnReadControls.Size = New System.Drawing.Size(88, 32) Me.btnReadControls.TabIndex = 5 Me.btnReadControls.Text = "Read Controls" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(400, 24) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(232, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "TextBox1" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(400, 68) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(232, 20) Me.TextBox2.TabIndex = 3 Me.TextBox2.Text = "TextBox2" " "TextBox3 " Me.TextBox3.Location = New System.Drawing.Point(400, 112) Me.TextBox3.Name = "TextBox3" Me.TextBox3.Size = New System.Drawing.Size(232, 20) Me.TextBox3.TabIndex = 4 Me.TextBox3.Text = "TextBox3" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(656, 286) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox3, Me.TextBox2, Me.TextBox1, Me.RichTextBox1, Me.Panel1}) Me.Panel1.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Private Sub btnChangeFont_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnChangeFont.Click Dim theFont As System.Drawing.Font FontDialog1.ShowDialog() theFont = FontDialog1.Font btnChangeFont.Font = theFont RichTextBox1.SelectionFont = theFont End Sub
End Class</source>
Copy and paste between RichTextBox
<source lang="vbnet">Imports System.Drawing Imports System.Windows.Forms public class CopyPasteRichText
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub frmDragRichText_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim txt As String = "The quick brown fox jumps over the lazy dog." rchSource.Text = txt rchSource.Select(txt.IndexOf("quick"), Len("quick")) rchSource.SelectionFont = New Font(rchSource.SelectionFont, FontStyle.Italic) rchSource.Select(txt.IndexOf("brown"), Len("brown")) rchSource.SelectionFont = New Font(rchSource.SelectionFont, FontStyle.Bold) rchSource.SelectionColor = Color.Brown rchSource.Select(txt.IndexOf("fox"), Len("fox")) rchSource.SelectionFont = New Font(rchSource.SelectionFont, FontStyle.Bold) rchSource.SelectionColor = Color.Red rchSource.Select(txt.IndexOf("jumps over"), Len("jumps over")) rchSource.SelectionFont = New Font(rchSource.SelectionFont, FontStyle.Underline) rchSource.Select(txt.IndexOf("lazy"), Len("lazy")) rchSource.SelectionFont = New Font(rchSource.SelectionFont, FontStyle.Bold) rchSource.Select(txt.IndexOf("dog"), Len("dog")) rchSource.SelectionFont = New Font(rchSource.SelectionFont, FontStyle.Bold) rchSource.SelectionColor = Color.Blue rchSource.Select(0, 0) End Sub Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click Dim data_object As New DataObject data_object.SetData(DataFormats.Rtf, rchSource.Rtf) data_object.SetData(DataFormats.Text, rchSource.Text) Dim html_text As String html_text = "<HTML>" & vbCrLf html_text &= " <HEAD>The Quick Brown Fox</HEAD>" & vbCrLf html_text &= " <BODY>" & vbCrLf html_text &= rchSource.Text & vbCrLf html_text &= " </BODY>" & vbCrLf & "</HTML>" data_object.SetData(DataFormats.Html, html_text) Clipboard.SetDataObject(data_object) End Sub Private Sub btnPaste_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPaste.Click Dim data_object As IDataObject = Clipboard.GetDataObject() If data_object.GetDataPresent(DataFormats.Rtf) Then rchTarget.Rtf = data_object.GetData(DataFormats.Rtf).ToString lblRtf.Text = data_object.GetData(DataFormats.Rtf).ToString Else rchTarget.Text = "" lblRtf.Text = "" End If If data_object.GetDataPresent(DataFormats.Text) Then lblTarget.Text = data_object.GetData(DataFormats.Text).ToString Else lblTarget.Text = "" End If If data_object.GetDataPresent(DataFormats.Html) Then lblHtml.Text = data_object.GetData(DataFormats.Html).ToString Else lblHtml.Text = "" End If End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.btnPaste = New System.Windows.Forms.Button Me.btnCopy = New System.Windows.Forms.Button Me.lblRtf = New System.Windows.Forms.Label Me.lblHtml = New System.Windows.Forms.Label Me.lblTarget = New System.Windows.Forms.Label Me.rchTarget = New System.Windows.Forms.RichTextBox Me.rchSource = New System.Windows.Forms.RichTextBox Me.SuspendLayout() " "btnPaste " Me.btnPaste.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.btnPaste.Location = New System.Drawing.Point(240, 40) Me.btnPaste.Name = "btnPaste" Me.btnPaste.Size = New System.Drawing.Size(48, 24) Me.btnPaste.TabIndex = 16 Me.btnPaste.Text = "Paste" " "btnCopy " Me.btnCopy.Location = New System.Drawing.Point(8, 40) Me.btnCopy.Name = "btnCopy" Me.btnCopy.Size = New System.Drawing.Size(48, 24) Me.btnCopy.TabIndex = 15 Me.btnCopy.Text = "Copy" " "lblRtf " Me.lblRtf.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblRtf.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblRtf.Location = New System.Drawing.Point(0, 104) Me.lblRtf.Margin = New System.Windows.Forms.Padding(3, 3, 3, 1) Me.lblRtf.Name = "lblRtf" Me.lblRtf.Size = New System.Drawing.Size(296, 110) Me.lblRtf.TabIndex = 14 " "lblHtml " Me.lblHtml.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblHtml.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblHtml.Location = New System.Drawing.Point(0, 248) Me.lblHtml.Margin = New System.Windows.Forms.Padding(3, 2, 3, 3) Me.lblHtml.Name = "lblHtml" Me.lblHtml.Size = New System.Drawing.Size(296, 80) Me.lblHtml.TabIndex = 13 " "lblTarget " Me.lblTarget.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.lblTarget.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTarget.Location = New System.Drawing.Point(0, 216) Me.lblTarget.Margin = New System.Windows.Forms.Padding(3, 1, 3, 2) Me.lblTarget.Name = "lblTarget" Me.lblTarget.Size = New System.Drawing.Size(296, 28) Me.lblTarget.TabIndex = 12 " "rchTarget " Me.rchTarget.AllowDrop = True Me.rchTarget.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.rchTarget.Location = New System.Drawing.Point(0, 72) Me.rchTarget.Name = "rchTarget" Me.rchTarget.Size = New System.Drawing.Size(296, 28) Me.rchTarget.TabIndex = 11 Me.rchTarget.Text = "" " "rchSource " Me.rchSource.AllowDrop = True Me.rchSource.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.rchSource.Location = New System.Drawing.Point(0, 0) Me.rchSource.Name = "rchSource" Me.rchSource.Size = New System.Drawing.Size(296, 28) Me.rchSource.TabIndex = 10 Me.rchSource.Text = "" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(296, 329) Me.Controls.Add(Me.btnPaste) Me.Controls.Add(Me.btnCopy) Me.Controls.Add(Me.lblRtf) Me.Controls.Add(Me.lblHtml) Me.Controls.Add(Me.lblTarget) Me.Controls.Add(Me.rchTarget) Me.Controls.Add(Me.rchSource) Me.Name = "Form1" Me.Text = "CopyPasteRichText" Me.ResumeLayout(False) End Sub Friend WithEvents btnPaste As System.Windows.Forms.Button Friend WithEvents btnCopy As System.Windows.Forms.Button Friend WithEvents lblRtf As System.Windows.Forms.Label Friend WithEvents lblHtml As System.Windows.Forms.Label Friend WithEvents lblTarget As System.Windows.Forms.Label Friend WithEvents rchTarget As System.Windows.Forms.RichTextBox Friend WithEvents rchSource As System.Windows.Forms.RichTextBox
End Class</source>
Find text in RichTextBox
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RichTextBoxChangeFont
public Shared Sub Main Application.Run(New Form1) End Sub
End class 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.ruponentModel.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 RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button2 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.Button1 = New System.Windows.Forms.Button Me.Label1 = New System.Windows.Forms.Label Me.Button2 = New System.Windows.Forms.Button Me.SuspendLayout() " "RichTextBox1 " Me.RichTextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.RichTextBox1.Location = New System.Drawing.Point(24, 72) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(240, 128) Me.RichTextBox1.TabIndex = 0 " "Button1 " Me.Button1.Location = New System.Drawing.Point(104, 216) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "Format Text" " "Button2 " Me.Button2.Location = New System.Drawing.Point(104, 256) Me.Button2.Name = "Button2" Me.Button2.TabIndex = 3 Me.Button2.Text = "Save Text" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(288, 285) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.RichTextBox1) Me.ResumeLayout(False) End Sub
- End Region
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click RichTextBox1.Text = "italic, bold, underlined, and strikeout" RichTextBox1.Find("bold") Dim fntBold As New Font(RichTextBox1.Font, FontStyle.Bold) RichTextBox1.SelectionFont = fntBold RichTextBox1.Find("italic") Dim fntItalic As New Font(RichTextBox1.Font, FontStyle.Italic) RichTextBox1.SelectionFont = fntItalic RichTextBox1.Find("strikeout") Dim fntStrikeout As New Font(RichTextBox1.Font, FontStyle.Strikeout) RichTextBox1.SelectionFont = fntStrikeout RichTextBox1.Find("underlined") Dim fntUnderline As New Font(RichTextBox1.Font, FontStyle.Underline) RichTextBox1.SelectionFont = fntUnderline End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click RichTextBox1.SaveFile("text.rtf") End Sub
End Class</source>
Load txt file to RichTextBox
<source lang="vbnet">Imports System.Windows.Forms public class RichTextBoxLoadTextFile
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents Button1 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog Me.Button1 = New System.Windows.Forms.Button Me.SuspendLayout() " "RichTextBox1 " Me.RichTextBox1.Location = New System.Drawing.Point(16, 16) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(264, 168) Me.RichTextBox1.TabIndex = 0 Me.RichTextBox1.Text = "" " "OpenFileDialog1 " Me.OpenFileDialog1.Filter = "Text Files(*.txt)|*.txt" " "Button1 " Me.Button1.Location = New System.Drawing.Point(104, 208) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(88, 32) Me.Button1.TabIndex = 1 Me.Button1.Text = "Open" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.RichTextBox1) Me.Name = "Form1" Me.Text = "RichTextBox" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click RichTextBox1.Text = "" OpenFileDialog1.ShowDialog() Dim textline As String FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input) While Not EOF(1) textline = LineInput(1) RichTextBox1.Text = RichTextBox1.Text + textline + Chr(13) + Chr(10) End While End Sub
End Class</source>
Print view text in RichTextBox
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PrintPreview
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.MenuItem7 = New System.Windows.Forms.MenuItem Me.MenuItem8 = New System.Windows.Forms.MenuItem Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.SuspendLayout() " Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0) Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0) Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300) Me.PrintPreviewDialog1.Document = Me.PrintDocument1 Me.PrintPreviewDialog1.Enabled = True Me.PrintPreviewDialog1.Location = New System.Drawing.Point(147, 19) Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250) Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1" Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty Me.PrintPreviewDialog1.Visible = False " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5, Me.MenuItem6, Me.MenuItem7, Me.MenuItem8}) Me.MenuItem1.Text = "File" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.Text = "Open" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "Save" " "MenuItem4 " Me.MenuItem4.Index = 2 Me.MenuItem4.Text = "-" " "MenuItem5 " Me.MenuItem5.Index = 3 Me.MenuItem5.Text = "Preview" " "MenuItem6 " Me.MenuItem6.Index = 4 Me.MenuItem6.Text = "Print" " "MenuItem7 " Me.MenuItem7.Index = 5 Me.MenuItem7.Text = "-" " "MenuItem8 " Me.MenuItem8.Index = 6 Me.MenuItem8.Text = "Exit" " "RichTextBox1 " Me.RichTextBox1.Dock = System.Windows.Forms.DockStyle.Fill Me.RichTextBox1.Location = New System.Drawing.Point(0, 0) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(292, 266) Me.RichTextBox1.TabIndex = 0 Me.RichTextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.RichTextBox1) Me.Menu = Me.MainMenu1 Me.ResumeLayout(False) End Sub Dim streamToPrint As System.IO.StringReader Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click RichTextBox1.Text = "asdfasdf" End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click SaveFileDialog1.ShowDialog() RichTextBox1.SaveFile(SaveFileDialog1.FileName) End Sub Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click streamToPrint = New System.IO.StringReader(RichTextBox1.Text) PrintPreviewDialog1.ShowDialog() End Sub Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click streamToPrint = New System.IO.StringReader(RichTextBox1.Text) PrintDocument1.Print() End Sub Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim linePerPage As Single = 0 Dim verticalPostion As Single = 0 Dim count As Integer = 0 Dim leftMargin As Single = e.MarginBounds.Left Dim topMargin As Single = e.MarginBounds.Top Dim strLine As String linePerPage = e.MarginBounds.Height / RichTextBox1.Font.GetHeight(e.Graphics) strLine = streamToPrint.ReadLine While ((count < linePerPage) And Not (strLine Is Nothing)) verticalPostion = topMargin + (count * RichTextBox1.Font.GetHeight(e.Graphics)) e.Graphics.DrawString(strLine, RichTextBox1.Font, Brushes.Black, leftMargin, verticalPostion, New StringFormat) count = count + 1 If (count < linePerPage) Then strLine = streamToPrint.ReadLine End If End While If (strLine <> Nothing) Then e.HasMorePages = True Else e.HasMorePages = False End If End Sub Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click End End Sub
End Class</source>
RichTextBox Redo/Undo
<source lang="vbnet">Imports System.Drawing Imports System.Windows.Forms public class RichTextBoxRedoUndo
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub rchNotes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rchNotes.TextChanged btnUndo.Enabled = rchNotes.CanUndo btnRedo.Enabled = rchNotes.CanRedo If btnUndo.Enabled Then btnUndo.Text = "Undo " & rchNotes.UndoActionName Else btnUndo.Text = "Undo" End If If btnRedo.Enabled Then If btnRedo.Enabled Then btnRedo.Text = "Redo " & rchNotes.RedoActionName Else If btnRedo.Enabled Then btnRedo.Text = "Redo" End If End Sub Private Sub btnUndo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUndo.Click rchNotes.Undo() End Sub Private Sub btnRedo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRedo.Click rchNotes.Redo() End Sub Private Sub btnRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRed.Click rchNotes.SelectionColor = Color.Red End Sub Private Sub btnBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBlue.Click rchNotes.SelectionColor = Color.Blue End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.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. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.MenuStrip1 = New System.Windows.Forms.MenuStrip Me.btnUndo = New System.Windows.Forms.ToolStripMenuItem Me.btnRedo = New System.Windows.Forms.ToolStripMenuItem Me.btnRed = New System.Windows.Forms.ToolStripMenuItem Me.btnBlue = New System.Windows.Forms.ToolStripMenuItem Me.rchNotes = New System.Windows.Forms.RichTextBox Me.MenuStrip1.SuspendLayout() Me.SuspendLayout() " "MenuStrip1 " Me.MenuStrip1.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.btnUndo, Me.btnRedo, Me.btnRed, Me.btnBlue}) Me.MenuStrip1.Location = New System.Drawing.Point(0, 0) Me.MenuStrip1.Name = "MenuStrip1" Me.MenuStrip1.Size = New System.Drawing.Size(374, 24) Me.MenuStrip1.TabIndex = 5 Me.MenuStrip1.Text = "MenuStrip1" " "btnUndo " Me.btnUndo.Enabled = False Me.btnUndo.Name = "btnUndo" Me.btnUndo.Text = "Undo" " "btnRedo " Me.btnRedo.Enabled = False Me.btnRedo.Name = "btnRedo" Me.btnRedo.Text = "Redo" " "btnRed " Me.btnRed.Name = "btnRed" Me.btnRed.Text = "Red" " "btnBlue " Me.btnBlue.Name = "btnBlue" Me.btnBlue.Text = "Blue" " "rchNotes " Me.rchNotes.Dock = System.Windows.Forms.DockStyle.Fill Me.rchNotes.Location = New System.Drawing.Point(0, 24) Me.rchNotes.Name = "rchNotes" Me.rchNotes.Size = New System.Drawing.Size(374, 249) Me.rchNotes.TabIndex = 6 Me.rchNotes.Text = "" " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(374, 273) Me.Controls.Add(Me.rchNotes) Me.Controls.Add(Me.MenuStrip1) Me.Name = "Form1" Me.Text = "UseRichTextBox" Me.MenuStrip1.ResumeLayout(False) Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents MenuStrip1 As System.Windows.Forms.MenuStrip Friend WithEvents btnUndo As System.Windows.Forms.ToolStripMenuItem Friend WithEvents btnRedo As System.Windows.Forms.ToolStripMenuItem Friend WithEvents btnRed As System.Windows.Forms.ToolStripMenuItem Friend WithEvents btnBlue As System.Windows.Forms.ToolStripMenuItem Friend WithEvents rchNotes As System.Windows.Forms.RichTextBox
End Class</source>
Save Rtf file from RichTextBox and Read Rtf file to RichTextBox
<source lang="vbnet">Option Strict On Imports System.IO Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class RichTextBoxSaveReadFile
public Shared Sub Main Application.Run(New Form1) End Sub
End class
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 Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents ColorDialog1 As System.Windows.Forms.ColorDialog Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents btnChangeColor As System.Windows.Forms.Button Friend WithEvents btnChangeFont As System.Windows.Forms.Button Friend WithEvents btnSaveRTF As System.Windows.Forms.Button Friend WithEvents btnOpenRTF As System.Windows.Forms.Button Friend WithEvents btnWriteControls As System.Windows.Forms.Button Friend WithEvents btnReadControls As System.Windows.Forms.Button Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox Friend WithEvents TextBox3 As System.Windows.Forms.TextBox "Required by the Windows Form Designer Private components As System.ruponentModel.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. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.Panel1 = New System.Windows.Forms.Panel() Me.RichTextBox1 = New System.Windows.Forms.RichTextBox() Me.ColorDialog1 = New System.Windows.Forms.ColorDialog() Me.FontDialog1 = New System.Windows.Forms.FontDialog() Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog() Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.btnChangeColor = New System.Windows.Forms.Button() Me.btnChangeFont = New System.Windows.Forms.Button() Me.btnSaveRTF = New System.Windows.Forms.Button() Me.btnOpenRTF = New System.Windows.Forms.Button() Me.btnWriteControls = New System.Windows.Forms.Button() Me.btnReadControls = New System.Windows.Forms.Button() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.TextBox2 = New System.Windows.Forms.TextBox() Me.TextBox3 = New System.Windows.Forms.TextBox() Me.Panel1.SuspendLayout() Me.SuspendLayout() " "Panel1 " Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnReadControls, Me.btnWriteControls, Me.btnOpenRTF, Me.btnSaveRTF, Me.btnChangeFont, Me.btnChangeColor}) Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom Me.Panel1.Location = New System.Drawing.Point(0, 222) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(656, 64) Me.Panel1.TabIndex = 0 " "RichTextBox1 " Me.RichTextBox1.Location = New System.Drawing.Point(16, 8) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(336, 176) Me.RichTextBox1.TabIndex = 1 Me.RichTextBox1.Text = "Mr. and Mrs. Dursley, of number four, Privet Drive, were proud to say that they w" & _ "ere perfectly normal, thank you very much." " "btnChangeColor " Me.btnChangeColor.Location = New System.Drawing.Point(24, 24) Me.btnChangeColor.Name = "btnChangeColor" Me.btnChangeColor.Size = New System.Drawing.Size(88, 32) Me.btnChangeColor.TabIndex = 0 Me.btnChangeColor.Text = "Change Color" " "btnChangeFont " Me.btnChangeFont.Location = New System.Drawing.Point(128, 24) Me.btnChangeFont.Name = "btnChangeFont" Me.btnChangeFont.Size = New System.Drawing.Size(88, 32) Me.btnChangeFont.TabIndex = 1 Me.btnChangeFont.Text = "Change Font" " "btnSaveRTF " Me.btnSaveRTF.Location = New System.Drawing.Point(232, 24) Me.btnSaveRTF.Name = "btnSaveRTF" Me.btnSaveRTF.Size = New System.Drawing.Size(88, 32) Me.btnSaveRTF.TabIndex = 2 Me.btnSaveRTF.Text = "Save RTF" " "btnOpenRTF " Me.btnOpenRTF.Location = New System.Drawing.Point(336, 24) Me.btnOpenRTF.Name = "btnOpenRTF" Me.btnOpenRTF.Size = New System.Drawing.Size(88, 32) Me.btnOpenRTF.TabIndex = 3 Me.btnOpenRTF.Text = "Open RTF" " "btnWriteControls " Me.btnWriteControls.Location = New System.Drawing.Point(440, 24) Me.btnWriteControls.Name = "btnWriteControls" Me.btnWriteControls.Size = New System.Drawing.Size(88, 32) Me.btnWriteControls.TabIndex = 4 Me.btnWriteControls.Text = "Write Controls" " "btnReadControls " Me.btnReadControls.Location = New System.Drawing.Point(544, 24) Me.btnReadControls.Name = "btnReadControls" Me.btnReadControls.Size = New System.Drawing.Size(88, 32) Me.btnReadControls.TabIndex = 5 Me.btnReadControls.Text = "Read Controls" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(400, 24) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(232, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "TextBox1" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(400, 68) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(232, 20) Me.TextBox2.TabIndex = 3 Me.TextBox2.Text = "TextBox2" " "TextBox3 " Me.TextBox3.Location = New System.Drawing.Point(400, 112) Me.TextBox3.Name = "TextBox3" Me.TextBox3.Size = New System.Drawing.Size(232, 20) Me.TextBox3.TabIndex = 4 Me.TextBox3.Text = "TextBox3" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(656, 286) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox3, Me.TextBox2, Me.TextBox1, Me.RichTextBox1, Me.Panel1}) Me.Panel1.ResumeLayout(False) Me.ResumeLayout(False) End Sub
- End Region
Private Sub btnSaveRTF_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnSaveRTF.Click SaveFileDialog1.InitialDirectory = Application.ExecutablePath SaveFileDialog1.DefaultExt = "rtf" SaveFileDialog1.FileName = "NewFile" SaveFileDialog1.Filter = "Rich Text Files (*.rtf)|*.rtf|All Files (*.*) | *.*" SaveFileDialog1.ShowDialog() RichTextBox1.SaveFile(SaveFileDialog1.FileName) End Sub Private Sub btnOpenRTF_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOpenRTF.Click OpenFileDialog1.InitialDirectory = Application.ExecutablePath OpenFileDialog1.DefaultExt = "rtf" OpenFileDialog1.FileName = "NewFile" OpenFileDialog1.Filter = "Rich Text Files (*.rtf)|*.rtf|All Files (*.*) | *.*" OpenFileDialog1.ShowDialog() RichTextBox1.LoadFile(OpenFileDialog1.FileName) End Sub
End Class</source>
Save text in RichTextBox into a file
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class PrintPreview
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.MenuItem7 = New System.Windows.Forms.MenuItem Me.MenuItem8 = New System.Windows.Forms.MenuItem Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.SuspendLayout() " Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0) Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0) Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300) Me.PrintPreviewDialog1.Document = Me.PrintDocument1 Me.PrintPreviewDialog1.Enabled = True Me.PrintPreviewDialog1.Location = New System.Drawing.Point(147, 19) Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250) Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1" Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty Me.PrintPreviewDialog1.Visible = False " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5, Me.MenuItem6, Me.MenuItem7, Me.MenuItem8}) Me.MenuItem1.Text = "File" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.Text = "Open" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "Save" " "MenuItem4 Me.MenuItem8.Index = 6 Me.MenuItem8.Text = "Exit" " "RichTextBox1 " Me.RichTextBox1.Dock = System.Windows.Forms.DockStyle.Fill Me.RichTextBox1.Location = New System.Drawing.Point(0, 0) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(292, 266) Me.RichTextBox1.TabIndex = 0 Me.RichTextBox1.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.RichTextBox1) Me.Menu = Me.MainMenu1 Me.ResumeLayout(False) End Sub Dim streamToPrint As System.IO.StringReader Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click RichTextBox1.Text = "asdfasdf" End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click SaveFileDialog1.ShowDialog() RichTextBox1.SaveFile(SaveFileDialog1.FileName) End Sub
Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click End End Sub
End Class</source>
Simple Editor based on RichTextBox
<source lang="vbnet">Imports System.Windows.Forms public class SimpleEditorBasedOnRichTextBox
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents RichTextBox1 As System.Windows.Forms.RichTextBox Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem10 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem11 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem12 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem13 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem14 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem15 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem16 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem17 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem18 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem19 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem20 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem21 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem22 As System.Windows.Forms.MenuItem Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.MenuItem7 = New System.Windows.Forms.MenuItem Me.MenuItem8 = New System.Windows.Forms.MenuItem Me.MenuItem9 = New System.Windows.Forms.MenuItem Me.MenuItem10 = New System.Windows.Forms.MenuItem Me.MenuItem11 = New System.Windows.Forms.MenuItem Me.MenuItem12 = New System.Windows.Forms.MenuItem Me.MenuItem14 = New System.Windows.Forms.MenuItem Me.MenuItem13 = New System.Windows.Forms.MenuItem Me.MenuItem15 = New System.Windows.Forms.MenuItem Me.MenuItem17 = New System.Windows.Forms.MenuItem Me.MenuItem18 = New System.Windows.Forms.MenuItem Me.MenuItem19 = New System.Windows.Forms.MenuItem Me.MenuItem16 = New System.Windows.Forms.MenuItem Me.MenuItem20 = New System.Windows.Forms.MenuItem Me.MenuItem21 = New System.Windows.Forms.MenuItem Me.MenuItem22 = New System.Windows.Forms.MenuItem Me.RichTextBox1 = New System.Windows.Forms.RichTextBox Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem6, Me.MenuItem10}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5}) Me.MenuItem1.Text = "File" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.Text = "New" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "Open" " "MenuItem4 " Me.MenuItem4.Index = 2 Me.MenuItem4.Text = "Save" " "MenuItem5 " Me.MenuItem5.Index = 3 Me.MenuItem5.Text = "Exit" " "MenuItem6 " Me.MenuItem6.Index = 1 Me.MenuItem6.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem7, Me.MenuItem8, Me.MenuItem9}) Me.MenuItem6.Text = "Edit" " "MenuItem7 " Me.MenuItem7.Index = 0 Me.MenuItem7.Text = "Cut" " "MenuItem8 " Me.MenuItem8.Index = 1 Me.MenuItem8.Text = "Copy" " "MenuItem9 " Me.MenuItem9.Index = 2 Me.MenuItem9.Text = "Paste" " "MenuItem10 " Me.MenuItem10.Index = 2 Me.MenuItem10.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem11, Me.MenuItem13}) Me.MenuItem10.Text = "Font Color" " "MenuItem11 " Me.MenuItem11.Index = 0 Me.MenuItem11.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem12, Me.MenuItem14}) Me.MenuItem11.Text = "Size" " "MenuItem12 " Me.MenuItem12.Index = 0 Me.MenuItem12.Text = "Smaller Font" " "MenuItem14 " Me.MenuItem14.Index = 1 Me.MenuItem14.Text = "Larger Font" " "MenuItem13 " Me.MenuItem13.Index = 1 Me.MenuItem13.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem15, Me.MenuItem16}) Me.MenuItem13.Text = "Color" " "MenuItem15 " Me.MenuItem15.Index = 0 Me.MenuItem15.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem17, Me.MenuItem18, Me.MenuItem19}) Me.MenuItem15.Text = "Foreground" " "MenuItem17 " Me.MenuItem17.Index = 0 Me.MenuItem17.Text = "Red" " "MenuItem18 " Me.MenuItem18.Index = 1 Me.MenuItem18.Text = "Yellow" " "MenuItem19 " Me.MenuItem19.Index = 2 Me.MenuItem19.Text = "Green" " "MenuItem16 " Me.MenuItem16.Index = 1 Me.MenuItem16.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem20, Me.MenuItem21, Me.MenuItem22}) Me.MenuItem16.Text = "Background" " "MenuItem20 " Me.MenuItem20.Index = 0 Me.MenuItem20.Text = "Red" " "MenuItem21 " Me.MenuItem21.Index = 1 Me.MenuItem21.Text = "Yellow" " "MenuItem22 " Me.MenuItem22.Index = 2 Me.MenuItem22.Text = "Green" " "RichTextBox1 " Me.RichTextBox1.Dock = System.Windows.Forms.DockStyle.Fill Me.RichTextBox1.Location = New System.Drawing.Point(0, 0) Me.RichTextBox1.Name = "RichTextBox1" Me.RichTextBox1.Size = New System.Drawing.Size(292, 266) Me.RichTextBox1.TabIndex = 0 Me.RichTextBox1.Text = "" " "SaveFileDialog1 " Me.SaveFileDialog1.Filter = "Text(*.txt)|*.txt" " "OpenFileDialog1 " Me.OpenFileDialog1.Filter = "Text(*.txt)|*.txt" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.RichTextBox1) Me.Menu = Me.MainMenu1 Me.ResumeLayout(False) End Sub Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click RichTextBox1.Text = "" Form1.ActiveForm.Text = "NoName" End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click Dim textline As String OpenFileDialog1.ShowDialog() FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input) While Not EOF(1) textline = LineInput(1) RichTextBox1.Text = RichTextBox1.Text + textline + Chr(13) + Chr(10) End While FileClose(1) Form1.ActiveForm.Text = OpenFileDialog1.FileName End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click SaveFileDialog1.ShowDialog() RichTextBox1.SaveFile(SaveFileDialog1.FileName) Form1.ActiveForm.Text = SaveFileDialog1.FileName End Sub Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem5.Click End End Sub Private Sub MenuItem7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem7.Click RichTextBox1.Cut() End Sub Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click RichTextBox1.Copy() End Sub Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click RichTextBox1.Paste() End Sub Private Sub MenuItem12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem12.Click RichTextBox1.Font = New System.Drawing.Font("", 10) End Sub Private Sub MenuItem14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem14.Click RichTextBox1.Font = New System.Drawing.Font("", 20) End Sub Private Sub MenuItem17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem17.Click RichTextBox1.ForeColor = System.Drawing.Color.Red End Sub Private Sub MenuItem18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem18.Click RichTextBox1.ForeColor = System.Drawing.Color.Yellow End Sub Private Sub MenuItem19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem19.Click RichTextBox1.ForeColor = System.Drawing.Color.Green End Sub Private Sub MenuItem20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem20.Click RichTextBox1.BackColor = System.Drawing.Color.Red End Sub Private Sub MenuItem21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem21.Click RichTextBox1.BackColor = System.Drawing.Color.Yellow End Sub Private Sub MenuItem22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem22.Click RichTextBox1.BackColor = System.Drawing.Color.Green End Sub
End Class</source>