Copy and paste between RichTextBox : RichTextBox « GUI « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » GUI » RichTextBox 
14.27.9.Copy and paste between RichTextBox
Copy and paste between RichTextBox
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.EventArgsHandles 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(00)
    End Sub

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgsHandles 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.EventArgsHandles btnPaste.Click
        Dim data_object As IDataObject = Clipboard.GetDataObject()

        If data_object.GetDataPresent(DataFormats.RtfThen
            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.TextThen
            lblTarget.Text = data_object.GetData(DataFormats.Text).ToString
        Else
            lblTarget.Text = ""
        End If

        If data_object.GetDataPresent(DataFormats.HtmlThen
            lblHtml.Text = data_object.GetData(DataFormats.Html).ToString
        Else
            lblHtml.Text = ""
        End If
    End Sub
End Class

<Global.Microsoft.VisualBasic.CompilerServices.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.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.
    <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(24040)
        Me.btnPaste.Name = "btnPaste"
        Me.btnPaste.Size = New System.Drawing.Size(4824)
        Me.btnPaste.TabIndex = 16
        Me.btnPaste.Text = "Paste"
        '
        'btnCopy
        '
        Me.btnCopy.Location = New System.Drawing.Point(840)
        Me.btnCopy.Name = "btnCopy"
        Me.btnCopy.Size = New System.Drawing.Size(4824)
        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(0104)
        Me.lblRtf.Margin = New System.Windows.Forms.Padding(3331)
        Me.lblRtf.Name = "lblRtf"
        Me.lblRtf.Size = New System.Drawing.Size(296110)
        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(0248)
        Me.lblHtml.Margin = New System.Windows.Forms.Padding(3233)
        Me.lblHtml.Name = "lblHtml"
        Me.lblHtml.Size = New System.Drawing.Size(29680)
        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(0216)
        Me.lblTarget.Margin = New System.Windows.Forms.Padding(3132)
        Me.lblTarget.Name = "lblTarget"
        Me.lblTarget.Size = New System.Drawing.Size(29628)
        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(072)
        Me.rchTarget.Name = "rchTarget"
        Me.rchTarget.Size = New System.Drawing.Size(29628)
        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(00)
        Me.rchSource.Name = "rchSource"
        Me.rchSource.Size = New System.Drawing.Size(29628)
        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(296329)
        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
14.27.RichTextBox
14.27.1.Load txt file to RichTextBoxLoad txt file to RichTextBox
14.27.2.Simple Editor based on RichTextBoxSimple Editor based on RichTextBox
14.27.3.Print view text in RichTextBoxPrint view text in RichTextBox
14.27.4.Save text in RichTextBox into a fileSave text in RichTextBox into a file
14.27.5.Change RichTextBox Selection colorChange RichTextBox Selection color
14.27.6.Change RichTextBox Selection FontChange RichTextBox Selection Font
14.27.7.Save Rtf file from RichTextBox and Read Rtf file to RichTextBoxSave Rtf file from RichTextBox and Read Rtf file to RichTextBox
14.27.8.Find text in RichTextBoxFind text in RichTextBox
14.27.9.Copy and paste between RichTextBoxCopy and paste between RichTextBox
14.27.10.RichTextBox Redo/UndoRichTextBox Redo/Undo
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.