Solved

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

Posted on 2011-02-21
11
427 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

735 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