Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-02-21
11
Medium Priority
?
435 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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