Solved

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

Posted on 2011-02-21
11
433 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 86

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 86

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
Independent Software Vendors: 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 86

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 86

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 86

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

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!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

623 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