Solved

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

Posted on 2011-02-21
11
415 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now