Results 1 to 3 of 3

Thread: how to copy color text from richtextbox in vb.net

  1. #1
    Join Date
    Mar 2009
    Posts
    56

    how to copy color text from richtextbox in vb.net

    I am new to software development field and many times i try copy paste many things from various place & but i have to edit it every time means if i copy a paragraph having Rich enabled text then it wipes out all the bold, italic, underline, etc in it... I forgot to say i am using vb.net.... I just wanted to know weather how to copy color text from richtextbox in vb.net is possible?????

  2. #2
    Join Date
    Apr 2008
    Posts
    3,267

    Re: how to copy color text from richtextbox in vb.net

    Here is the Code for it....!

    Code:
    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

  3. #3
    Join Date
    Apr 2008
    Posts
    1,948

    Re: how to copy color text from richtextbox in vb.net

    Code:
    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

Similar Threads

  1. It looks like Amazon is trying to copy nook color
    By Amy_i3 in forum Portable Devices
    Replies: 6
    Last Post: 24-07-2011, 10:45 PM
  2. How to put html tag to color text
    By garfield1 in forum Software Development
    Replies: 3
    Last Post: 28-07-2009, 11:38 AM
  3. Change text color in Console
    By Zool in forum Software Development
    Replies: 4
    Last Post: 08-05-2009, 11:19 PM
  4. How to Change CSS Text Color
    By advsnhal in forum Software Development
    Replies: 4
    Last Post: 14-04-2009, 02:50 PM
  5. Replies: 0
    Last Post: 18-03-2009, 10:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,556,787.59498 seconds with 16 queries