Solved

copy 'color' text only from RTF to clipboard VB.NET 2003

Posted on 2011-02-21
11
423 Views
Last Modified: 2012-05-11
Hi Experts,

I want to copy COLOR TEXT (e.g. GREEN) text from a RichTextBox and store on Clipboard until pasted.

[Winform; RichTextBox and Button]

If button is clicked; it copies all 'Green' color text to clipboard. The document will have a variation of colors, but I want to be selective.

I don't no where to start, so any code would be great.

Thanks,
Roberto
0
Comment
Question by:RobertoFreemano
  • 6
  • 5
11 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34946090
You have to select each character individually and then check if its color matches what you want.

Something like:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To RichTextBox1.TextLength - 1
            RichTextBox1.Select(i, 1)
            If RichTextBox1.SelectionColor.Equals(Color.Green) Then
                sb.Append(RichTextBox1.SelectedText)
            End If
        Next
        Clipboard.SetText(sb.ToString)
    End Sub

Open in new window

0
 

Author Comment

by:RobertoFreemano
ID: 34953233
Hi Idle_Mind,

There seems to be some discrepancy with "Clipboard.SetText(sb.ToString)"

VB.NET 2003 doesn't seem to know 'SetText'

I changed it to "Clipboard.SetDataObject(sb.ToString)"

and this canceled out the Error ; however, it doesn't work either :(
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34953274
Sorry...I'm using VS2010 and don't always remember which methods have changed since 2003.  =)

Is it even grabbing the green text correctly?

Try adding a MessageBox in there:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To RichTextBox1.TextLength - 1
            RichTextBox1.Select(i, 1)
            If RichTextBox1.SelectionColor.Equals(Color.Green) Then
                sb.Append(RichTextBox1.SelectedText)
            End If
        Next
        MessageBox.Show("Green Text?: " & sb.ToString)
        Clipboard.SetDataObject(sb.ToString)
    End Sub

Open in new window

0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:RobertoFreemano
ID: 34953366
That's got it ;)

It would help if I actually set the color to GREEN instead of LIME :)

ONE MORE THING - IF I MAY PLEASE:
When Paste from Clipboard, it writes to one single line...

Is there a may of adding VBCRLF into it? (to make it more readable)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34953647
I'm sure it's possible...where are the vbCrLf's at with respect to the text?  Not sure if it that character takes on the color of the text around it.
0
 

Author Comment

by:RobertoFreemano
ID: 34954315
A typical RTF file reads:

Text (red)
Text (green)
Space
Text (red)
Text (red)
Text (green)
Text (green)
Te
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 100 total points
ID: 34954592
Try this one out:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendColorToClipBoard(RichTextBox1, Color.Green)
    End Sub

    Private Sub SendColorToClipBoard(ByVal RTB As RichTextBox, ByVal Clr As Color)
        Dim PrevStart As Integer = RTB.SelectionStart
        Dim PrevLength As Integer = RTB.SelectionLength

        Dim PrevCharWasGreen As Boolean = False
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To RichTextBox1.TextLength - 1
            RTB.Select(i, 1)
            If RTB.SelectionColor.Equals(Clr) Then
                sb.Append(RTB.SelectedText)
                PrevCharWasGreen = True
            ElseIf PrevCharWasGreen Then
                PrevCharWasGreen = False
                sb.Append(vbCrLf)
            End If
        Next
        Clipboard.SetDataObject(sb.ToString)

        RTB.Select(PrevStart, PrevLength)
    End Sub

End Class

Open in new window

0
 

Author Comment

by:RobertoFreemano
ID: 34958564
Hi Idle_Mind,

SendColorToClipBoard is not declared :(
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34960594
I moved the code to a generic function...it's at line #7 in my last post.
0
 

Author Comment

by:RobertoFreemano
ID: 34967676
Ah ok, I'll get back to you later on this afternoon ;)
0
 

Author Closing Comment

by:RobertoFreemano
ID: 34967705
Amazing - Thanks Idle_Mind
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question