Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Multiple Line RichTextbox Color Background


Reply
 
Thread Tools Display Modes
  #1  
Old 11-23-2007, 12:05 AM
anitsircr anitsircr is offline
Newcomer
 
Join Date: Nov 2007
Posts: 3
Default Multiple Line RichTextbox Color Background


I have this project where list of data is displayed in a multiple richtextbox. I want to access each line and assign the corresponding color background. I have searched the net and tried many ways to do it, but it did not produce the right output. Anybody who can share to me what is the best way to do it?
Reply With Quote
  #2  
Old 11-23-2007, 12:17 AM
the master's Avatar
the master the master is offline
Tachikoma
 
Join Date: Mar 2003
Location: Mansfield, UK
Posts: 4,594
Default

What language are you using? VB6? VB.net?
__________________
"That which seems simple is often overlooked" ~ me
Halloween Special FX
Web Hosting from Spook Technologies

Last edited by MikeJ; 11-23-2007 at 01:05 AM.
Reply With Quote
  #3  
Old 11-23-2007, 01:06 AM
anitsircr anitsircr is offline
Newcomer
 
Join Date: Nov 2007
Posts: 3
Default

VB6.. Did I post it uncorrectly? Im so sorry..
Reply With Quote
  #4  
Old 11-23-2007, 01:13 AM
the master's Avatar
the master the master is offline
Tachikoma
 
Join Date: Mar 2003
Location: Mansfield, UK
Posts: 4,594
Default

Yes. The forum you posted it in was for questions and bugs related to this website. It appears one of the moderators has moved it to the correct forum now.

As for your problem this link shows a few examples of how it works.

You simply use selstart and sellength to select a bit of text in the textbox then use more or less the same properties you would normally use but with "sel" before them.

Code:
'Set the first 3 characters to blue
rtb.selstart=1
rtb.sellength=3
rtb.SelColor = RGB(0,0,255)

'Set the next 3 to green
rtb.selstart=4
rtb.sellength=3
rtb.selcolor=RGB(0,255,0)
__________________
"That which seems simple is often overlooked" ~ me
Halloween Special FX
Web Hosting from Spook Technologies
Reply With Quote
  #5  
Old 11-23-2007, 02:32 AM
anitsircr anitsircr is offline
Newcomer
 
Join Date: Nov 2007
Posts: 3
Default

Sorry for posting to a different forum. Ive only used it for the first time...
Thank you for the one who moved it to the right place. Thanks for the reply.

About the project, it is not the textcolor that I want to change but the background of the text. Just like it is being highlighted. Also I want to change the color of each line during runtime. How can I access the data in each line and change its text background. Each line will have a different background or highlight color.
Reply With Quote
  #6  
Old 11-23-2007, 09:03 AM
the master's Avatar
the master the master is offline
Tachikoma
 
Join Date: Mar 2003
Location: Mansfield, UK
Posts: 4,594
Default

It appears to be possible in .net but i cant find any properties for the VB6 control. There is a way though. Rich textboxes allow you to insert rich text code. Try this for example

Code:
rtb.SelRTF = "{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang2057\deflangfe1041
{\colortbl ;\red200\green200\blue255;}
\viewkind4\uc1\pard\ltrpar\highlight1\f0\fs24 random test text\highlight0\par }"
I dont fully understand what that means but Micro$oft have a page about RTF that you might find usefull.

One way you could find RTF code is to make what you want in M$ word with all your text and colors etc. Save as a .RTF file and open with notepad
__________________
"That which seems simple is often overlooked" ~ me
Halloween Special FX
Web Hosting from Spook Technologies

Last edited by the master; 11-23-2007 at 09:05 AM. Reason: Made code look nicer
Reply With Quote
  #7  
Old 11-23-2007, 12:12 PM
JPB JPB is offline
Contributor
 
Join Date: Oct 2004
Posts: 629
Default

Here's some code I've adapted from the vbAccelerator RichEdit control. It works with the standard VB RichTextBox control as long as the user has the riched20.dll installed on their system (at least on my system with Windows XP Home).

First, add a RichTextBox (RichTextBox1) and a CommandButton (Command1) to a Form for testing. The paste this code into the form:

Code:
Option Explicit

Private Declare Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor As Long, ByVal lHPalette As Long, lColorRef As Long) As Long
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, wParam As Long, lParam As Long) As Long

Private Const WM_USER As Long = &H400 
Private Const EM_SETCHARFORMAT = (WM_USER + 68)
Private Const CFM_BACKCOLOR = &H4000000
Private Const LF_FACESIZE = 32

Private Type CHARFORMAT2
    cbSize As Integer    '2
    wPad1 As Integer  '4
    dwMask As Long    '8
    dwEffects As Long    '12
    yHeight As Long   '16
    yOffset As Long   '20
    crTextColor As Long    '24
    bCharSet As Byte    '25
    bPitchAndFamily As Byte    '26
    szFaceName(0 To LF_FACESIZE - 1) As Byte    ' 58
    wPad2 As Integer    ' 60

    ' Additional stuff supported by RICHEDIT20
    wWeight As Integer            ' /* Font weight (LOGFONT value)      */
    sSpacing As Integer           ' /* Amount to space between letters  */
    crBackColor As Long        ' /* Background color                 */
    lLCID As Long               ' /* Locale ID                        */
    dwReserved As Long         ' /* Reserved. Must be 0              */
    sStyle As Integer            ' /* Style handle                     */
    wKerning As Integer            ' /* Twip size above which to kern char pair*/
    bUnderlineType As Byte     ' /* Underline type                   */
    bAnimation As Byte         ' /* Animated text like marching ants */
    bRevAuthor As Byte         ' /* Revision author index            */
    bReserved1 As Byte
End Type

Private Sub Command1_Click()
    Me.RichTextBox1.Text = "The background colour is now red."
    Me.RichTextBox1.SelStart = 0
    Me.RichTextBox1.SelLength = Len(Me.RichTextBox1.Text)
    SelBackcolor Me.RichTextBox1, vbRed
    Me.RichTextBox1.SelStart = 0
End Sub

Private Sub SelBackcolor(RTB As RichTextBox, Color As OLE_COLOR)
    Dim lngColor As Long
    Dim tCF2 As CHARFORMAT2
    
    If Color = -1 Then
        ' Revert to auto background color
        tCF2.dwMask = CFM_BACKCOLOR
        tCF2.dwEffects = CFM_BACKCOLOR
        tCF2.crBackColor = -1
    Else
        ' Attempt to convert special colours (e.g. vbWindowBackground) to RGB
        If Not OleTranslateColor(Color, 0, lngColor) Then
            ' Untranslatable, so just use the passed colour
            lngColor = Color
        End If
        tCF2.dwMask = CFM_BACKCOLOR
        tCF2.crBackColor = lngColor
    End If
    tCF2.cbSize = Len(tCF2)
    ' Set the Background colour
    Call SendMessage(RTB.hWnd, EM_SETCHARFORMAT, ByVal &H1, ByVal VarPtr(tCF2))
End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->