i just added......

S

Supra

i just added "is nothing" to get rid or error that i posted below. but
it is not displaying in richtextbox. all i seeing is debug window.

Dim nrtb As New RichTextBox
Sub DisplayMessage(ByVal rtb As RichTextBox, ByVal sText As String,
Optional ByVal Colour As String = "")
If rtb.SelectedText Is Nothing Then
rtb.SelectionStart = rtb.Text.Length
' rtb.doColor(rtb, DirectCast(sText, String))
' rtb.doColor(rtb, CType(sText, String))
rtb.SelectedText = nrtb.doColor(sText)
rtb.SelectionStart = rtb.Text.Length
rtb.ScrollToCaret()
sText = String.Empty
End If
End Sub

ne hints?
regards
 
K

Ken Tucker [MVP]

Hi,

The richtextbox does not have a doColor method kind of confused as
to what you are trying to do. If you are trying to change the color of the
selected text try some like this.

RichTextBox1.HideSelection = False ' Text stays selected if richtextbox
doesnt have focus

RichTextBox1.Focus()

RichTextBox1.SelectAll()

RichTextBox1.SelectionColor = Color.Green ' will make all the text green.



Ken

----------------------



i just added "is nothing" to get rid or error that i posted below. but it is
not displaying in richtextbox. all i seeing is debug window.

Dim nrtb As New RichTextBox
Sub DisplayMessage(ByVal rtb As RichTextBox, ByVal sText As String, Optional
ByVal Colour As String = "")
If rtb.SelectedText Is Nothing Then
rtb.SelectionStart = rtb.Text.Length
' rtb.doColor(rtb, DirectCast(sText, String))
' rtb.doColor(rtb, CType(sText, String))
rtb.SelectedText = nrtb.doColor(sText)
rtb.SelectionStart = rtb.Text.Length
rtb.ScrollToCaret()
sText = String.Empty
End If
End Sub

ne hints?
regards
 
S

Supra

in userconrtrol i named doColour... but also i added method called
doColor....please have a look .....
in usercontrol......:
Option Explicit On
Option Strict Off

Imports System.Runtime.InteropServices
Imports System.Drawing

Namespace PirateChat
<System.ComponentModel.DesignerCategoryAttribute("UserControl")> _
Public Class RichTextBox
Inherits System.Windows.Forms.RichTextBox

Private Declare Function GetScrollRange Lib "User32" (ByVal hWnd
As IntPtr, ByVal nBar As Integer, ByRef lpMinPos As Integer, ByRef
lpMaxPos As Integer) As Boolean

Function doColor(ByVal a As String)
Dim b As String, n, fgcolor, bgcolor As Integer
Dim colour() As Color = {Color.White, Color.Black,
Color.DarkBlue, Color.DarkGreen, Color.Red, Color.Brown, _
Color.Purple, Color.Orange,
Color.Yellow, Color.LightGreen, Color.DarkMagenta, Color.SkyBlue, _
Color.Blue, Color.Magenta,
Color.Gray, Color.LightSlateGray}
Dim rtb As RichTextBox
rtb = New RichTextBox
fgcolor = 1
bgcolor = 0

For n = 1 To a.Length
b = Mid(a, n, 1)
If b = Chr(3) Then
'Parse Colours
If IsNumeric(Mid(a, n + 1, 1)) Then
If IsNumeric(Mid(a, n + 2, 1)) Then
If Mid(a, n + 3, 1) = "," Then
If IsNumeric(Mid(a, n + 4, 1)) Then
If IsNumeric(Mid(a, n + 5, 1)) Then
'@##,##
fgcolor = CInt(Mid(a, n + 1, 2))
bgcolor = CInt(Mid(a, n + 4, 2))
n += 5
Else
'@##,#
fgcolor = CInt(Mid(a, n + 1, 2))
bgcolor = CInt(Mid(a, n + 4, 1))
n += 4

End If
Else
'@##,
fgcolor = CInt(Mid(a, n + 1, 2))
n += 3

End If
Else
'@##
fgcolor = CInt(Mid(a, n + 1, 2))
n += 2
End If

ElseIf Mid(a, n + 2, 1) = "," Then
If IsNumeric(Mid(a, n + 3, 1)) Then
If IsNumeric(Mid(a, n + 4, 1)) Then
'@#,##
fgcolor = CInt(Mid(a, n + 1, 1))
bgcolor = CInt(Mid(a, n + 3, 2))
n += 4
Else
'@#,#
fgcolor = CInt(Mid(a, n + 1, 1))
bgcolor = CInt(Mid(a, n + 3, 1))
n += 3

End If
Else
'@#,
fgcolor = CInt(Mid(a, n + 1, 1))
n += 2
End If
Else
'@#
fgcolor = CInt(Mid(a, n + 1, 1))
n = n + 1
End If

If fgcolor > 15 Then
fgcolor = 1
End If

If bgcolor > 15 Then
bgcolor = 0
End If

rtb.SelectionColor = colour(fgcolor)
SelectionBackColor = colour(bgcolor)
Else
rtb.SelectionColor = colour(1)
SelectionBackColor = colour(0)
End If

ElseIf b = Chr(2) Then
Dim IsBold As Boolean = ((rtb.SelectionFont.Style
And FontStyle.Bold) = FontStyle.Bold)
rtb.SelectionFont = _
New Font(rtb.SelectionFont,
DirectCast(IIf(IsBold, rtb.SelectionFont.Style And Not FontStyle.Bold,
rtb.SelectionFont.Style Or FontStyle.Bold), FontStyle))

ElseIf b = Chr(31) Then
Dim IsUnderline As Boolean =
((rtb.SelectionFont.Style And FontStyle.Underline) = FontStyle.Underline)
rtb.SelectionFont = _
New Font( _
rtb.SelectionFont, _
DirectCast(IIf(IsUnderline,
rtb.SelectionFont.Style And Not FontStyle.Underline,
rtb.SelectionFont.Style Or FontStyle.Underline), FontStyle))
ElseIf b = Chr(22) Then
Dim IsItalic As Boolean = ((rtb.SelectionFont.Style
And FontStyle.Italic) = FontStyle.Italic)
rtb.SelectionFont = _
New Font( _
rtb.SelectionFont, _
DirectCast(IIf(IsItalic,
rtb.SelectionFont.Style And Not FontStyle.Italic,
rtb.SelectionFont.Style Or FontStyle.Italic), FontStyle))
ElseIf b = "" Then
b = Mid(a, n, 1)
Else
' rtb.SelectedText = DirectCast(b.ToString, String)
' rtb.SelectedText = CType(b, String) '.ToString
rtb.SelectedText = b.ToString
End If
Next n
End Function
ne hints u cann help me?
regsrds,
 
S

Supra

ths code in posted above will woked both fore and background colour
that embedded in richtextbox. it is similar to mirc chat colour. that i
done this in vb6.
refards
 
G

Guest

There is a difference between String is nothing and String.Length = 0. You
might want to try if rtb.SelectedText.Length = 0 then.
 
R

Robby

Generaly you should avoid giving your custom controls the same name as .Net
controls. The namespace hierarchy searches the referenced Framework
namespaces first, then it looks at other references, next the Imports
statements namespaces and finally the namespaces defined in the project.
Your declaration ...

Dim nrtb As New RichTextBox

will create a .Net RichTextBox instance because you have not specified the
namespace. Therefore of the namespace hierarchy finds
System.Windows.Forms.RichTextBox first since it is in a referenced Framework
namespace. Now to create an instance of your RichTextBox you need the
declaration ...

Dim nrtb As New PirateChat.RichTextBox

as this specifies what namespace to look in. Since there is no PirateChat
Framework namespace the searching for PirateChat.RichTextBox goes to your
PirateChat namespace where your custom RichTextBox is found.

Again, avoid using the same classnames for your classes as thoes in the
Framework. This is particularly true for classes you are extending through
inheritance as it will be very confusing. Just look at all the trouble you
have gone through to find the true problem with your code.

Robby
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top