HighTechTalks DotNet Forums  

RichTextBox.selection

VB.net microsoft.public.dotnet.languages.vb


Discuss RichTextBox.selection in the VB.net forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Pascal
 
Posts: n/a

Default RichTextBox.selection - 01-20-2010 , 01:19 PM






IHello
I would like to change the font size, color, name, when I click on a word
and reurn it back to its previous style if i click it again. I try this but
it works only if I make a double click on the word... When i want to select
a piece of word without an apostrophe for example it crashes: What's wrong?
Private Sub RTB_txt_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RTB_txt.SelectionChanged
If RTB_txt.SelectionFont.Name = ("Tahoma") And _
RTB_txt.SelectionFont.Size = "12" And _
RTB_txt.SelectionFont.Style = FontStyle.Bold And _
RTB_txt.SelectionColor = System.Drawing.Color.Red Then

RTB_txt.SelectionFont = New Font("Arial", 11, FontStyle.Regular)
RTB_txt.SelectionColor = System.Drawing.Color.Black
Else

RTB_txt.SelectionFont = New Font("Tahoma", 12, FontStyle.Bold)
RTB_txt.SelectionColor = System.Drawing.Color.Red
RTB_txt.AutoWordSelection = True
End If
End Sub

thanks
pascal

Reply With Quote
  #2  
Old   
Captain Jack
 
Posts: n/a

Default Re: RichTextBox.selection - 01-20-2010 , 01:46 PM






"Pascal" <scalpaSansSpam (AT) scalpa (DOT) info> wrote

Quote:
IHello
I would like to change the font size, color, name, when I click on a word
and reurn it back to its previous style if i click it again. I try this
but it works only if I make a double click on the word... When i want to
select a piece of word without an apostrophe for example it crashes:
What's wrong?
Private Sub RTB_txt_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RTB_txt.SelectionChanged
If RTB_txt.SelectionFont.Name = ("Tahoma") And _
RTB_txt.SelectionFont.Size = "12" And _
RTB_txt.SelectionFont.Style = FontStyle.Bold And _
RTB_txt.SelectionColor = System.Drawing.Color.Red Then

RTB_txt.SelectionFont = New Font("Arial", 11,
FontStyle.Regular)
RTB_txt.SelectionColor = System.Drawing.Color.Black
Else

RTB_txt.SelectionFont = New Font("Tahoma", 12, FontStyle.Bold)
RTB_txt.SelectionColor = System.Drawing.Color.Red
RTB_txt.AutoWordSelection = True
End If
End Sub

thanks
pascal
I'm not sure, but I think you're asking for trouble by changing selection
related properties in the SelectionChanged event handler. Have you tried
putting this code in a Click event handler instead?

--
Jack

Reply With Quote
  #3  
Old   
Pascal
 
Posts: n/a

Default Re: RichTextBox.selection - 01-22-2010 , 12:01 PM



Hello
I tried this but nothing happens:

Private Sub RTB_txt_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RTB_txt.Click
If RTB_txt.SelectionFont.Name = ("Tahoma") And _
RTB_txt.SelectionFont.Size = "12" And _
RTB_txt.SelectionFont.Style = FontStyle.Bold And _
RTB_txt.SelectionColor = System.Drawing.Color.Red Then

RTB_txt.SelectionFont = New Font("Arial", 11, FontStyle.Regular)
RTB_txt.SelectionColor = System.Drawing.Color.Black
Else

RTB_txt.SelectionFont = New Font("Tahoma", 12, FontStyle.Bold)
RTB_txt.SelectionColor = System.Drawing.Color.Red
RTB_txt.AutoWordSelection = True
End If

End Sub

--
http://www.scalpa.info
http://scalpa98.blogspot.com/
http://scalpa-production.blogspot.com/

"Captain Jack" <CaptainJack1024 (AT) comcast (DOT) net> a écrit dans le message de
news:1-ednTOtKMtpwcrWnZ2dnUVZ_hednZ2d (AT) giganews (DOT) com...
Quote:
"Pascal" <scalpaSansSpam (AT) scalpa (DOT) info> wrote in message
news:3961F5EE-8ADE-45D7-91F9-2503C36B8084 (AT) microsoft (DOT) com...
IHello
I would like to change the font size, color, name, when I click on a word
and reurn it back to its previous style if i click it again. I try this
but it works only if I make a double click on the word... When i want to
select a piece of word without an apostrophe for example it crashes:
What's wrong?
Private Sub RTB_txt_SelectionChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles RTB_txt.SelectionChanged
If RTB_txt.SelectionFont.Name = ("Tahoma") And _
RTB_txt.SelectionFont.Size = "12" And _
RTB_txt.SelectionFont.Style = FontStyle.Bold And _
RTB_txt.SelectionColor = System.Drawing.Color.Red Then

RTB_txt.SelectionFont = New Font("Arial", 11,
FontStyle.Regular)
RTB_txt.SelectionColor = System.Drawing.Color.Black
Else

RTB_txt.SelectionFont = New Font("Tahoma", 12, FontStyle.Bold)
RTB_txt.SelectionColor = System.Drawing.Color.Red
RTB_txt.AutoWordSelection = True
End If
End Sub

thanks
pascal

I'm not sure, but I think you're asking for trouble by changing selection
related properties in the SelectionChanged event handler. Have you tried
putting this code in a Click event handler instead?

--
Jack

Reply With Quote
  #4  
Old   
Captain Jack
 
Posts: n/a

Default Re: RichTextBox.selection - 01-22-2010 , 12:19 PM



"Pascal" <scalpaSansSpam (AT) scalpa (DOT) info> wrote

Quote:
Hello
I tried this but nothing happens:

Private Sub RTB_txt_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RTB_txt.Click
If RTB_txt.SelectionFont.Name = ("Tahoma") And _
RTB_txt.SelectionFont.Size = "12" And _
RTB_txt.SelectionFont.Style = FontStyle.Bold And _
RTB_txt.SelectionColor = System.Drawing.Color.Red Then

RTB_txt.SelectionFont = New Font("Arial", 11,
FontStyle.Regular)
RTB_txt.SelectionColor = System.Drawing.Color.Black
Else

RTB_txt.SelectionFont = New Font("Tahoma", 12, FontStyle.Bold)
RTB_txt.SelectionColor = System.Drawing.Color.Red
RTB_txt.AutoWordSelection = True
End If

End Sub

There may be a timing problem. The default behavior of the RTB when you
click on it is to reduce the selection length to zero and set the selection
start to where you clicked. You may not have anything selected at the moment
when this code is running.

One option would be to manually find the word where the selection marker is.
Read through the Text property backwards until you get to a non-word
character, then forwards, then manually set your selection to that, then
make your changes.

Another possibility (although it may yield the same results) is to try this
code in the MouseDown event handler, or maybe even record the selection to a
global variable during a MouseMove event handler, then use the most recent
recorded value during the Click event handler.

--
Jack

Reply With Quote
  #5  
Old   
Pascal
 
Posts: n/a

Default Re: RichTextBox.selection - 01-22-2010 , 12:45 PM



Yes it's more complex than I thought before.
I figured than by clicking one time all the word will be selected.... stupid
me!
I understood that I have to drag and move the mouse to see something
happens....... and i discovered if a part of the word is already in arial
etc. and another part is in Tahoma etc. it crashes too arrgh.
I will digg your ideas to see if there is a better solution. thanks
pascal

Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.