-
Jun 13th, 2014, 08:55 PM
#1
Thread Starter
Junior Member
[RESOLVED] Bolding and Changing Font Type and Size in Richtextbox
Hello World!
Before I start I have spent some time looking at examples in both thread and the Code Bank. There are a lot of great examples and I have tried a number of them and modified them in an attempt to tweek it to what I want.
So here is what I am Doing.....
Bold, Change Font Type and Change Font Size of a specific text within a RichTextBox.
Here is the RichTextBox Output -
Code:
rtb1.Text = Name & vbCrLf _
& "Type: " & CStr(Type) & CStr(Energy) & CStr(Effect) & vbCrLf _
& "Power Rank: " & CStr(PowerRank) & vbCrLf _
& "Range: " & CStr(Range) & vbCrLf _
& "Duration: " & CStr(Duration) & vbCrLf _
& "Area of Effect: " & CStr(AoE) & vbCrLf _
& "Activiation Time: " & CStr(APC) & vbCrLf _
& "Defense: " & CStr(Defense) & vbCrLf _
& Description
What I am attempting to do is effect "Name" which is called from:
Code:
Name = tbSpellName.Text
I have tried the following:
Code:
With Me.rtb1
rtb1.SelectionStart = 0
rtb1.SelectionLength = 25
rtb1.SelectionFont = New Font("Visitation", 11, FontStyle.Bold)
rtb1.SelectedText = tbSpellName.Text '+ ControlChars.Cr
End With
and this
Code:
Dim Spellname As String = Name
With Me.rtb1
.SelectedText = Spellname
.SelectionFont = New Font("Visitation", 11, FontStyle.Bold)
End With
and this
Code:
Dim myfont As New Font("Visitation", 11, FontStyle.Bold)
With Me.rtb1
.SelectionStart = 0
.SelectionLength = 50
.SelectionFont = myfont
End With
All of which to no avail. I am stumped. Could anyone please help me out here?
TIA
Ldavis
-
Jun 14th, 2014, 11:31 AM
#2
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
Can no one help me out? I guess it might not be possible.
-
Jun 14th, 2014, 02:13 PM
#3
Re: Bolding and Changing Font Type and Size in Richtextbox
looks like this thread may have what you're looking for. I know little about RichTextBox but the code here seems to change the font of a part of the text as you describe.
-
Jun 14th, 2014, 07:38 PM
#4
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
Well that thread s about changing the cont style of the RTB which I can do. But I want to only change One Word only and that word Only. So thanks for the link, but that is not what I need.
-
Jun 14th, 2014, 08:19 PM
#5
Re: Bolding and Changing Font Type and Size in Richtextbox
You need to select the word you want to change, then set the selectionfont, which will just change the font for the word you selected
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 14th, 2014, 08:23 PM
#6
Re: Bolding and Changing Font Type and Size in Richtextbox
SelectionStart is the index of the first character of the word you want to select.
SelectionLength is the length of the word you want to select.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 14th, 2014, 08:59 PM
#7
Re: Bolding and Changing Font Type and Size in Richtextbox
Here's the way you can change the color and fontstyle,
Code:
Dim ndx As Integer, currentFont As System.Drawing.Font = Me.RichTextBox1.SelectionFont
If Not redText1 Is Nothing Then
ndx = Me.RichTextBox1.Find(redText1)
If ndx >= 0 Then
Me.RichTextBox1.SelectionColor = Color.Red
Me.RichTextBox1.SelectionFont = New Font( _
currentFont.FontFamily, _
currentFont.Size, _
FontStyle.Bold)
End If
End If
"redText1" is a string type parameter I send to this function.
-
Jun 14th, 2014, 10:21 PM
#8
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
Originally Posted by .paul.
SelectionStart is the index of the first character of the word you want to select.
SelectionLength is the length of the word you want to select.
I have tried that as I had shown in one of my examples:
That is not working.
-
Jun 14th, 2014, 10:24 PM
#9
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
Originally Posted by wes4dbt
Here's the way you can change the color and fontstyle,
Code:
Dim ndx As Integer, currentFont As System.Drawing.Font = Me.RichTextBox1.SelectionFont
If Not redText1 Is Nothing Then
ndx = Me.RichTextBox1.Find(redText1)
If ndx >= 0 Then
Me.RichTextBox1.SelectionColor = Color.Red
Me.RichTextBox1.SelectionFont = New Font( _
currentFont.FontFamily, _
currentFont.Size, _
FontStyle.Bold)
End If
End If
"redText1" is a string type parameter I send to this function.
I get that. I can even do SelectionFind if the text i wanted was a specific tet as you have listed but the Name, as listed in the code above, is a user entered piece of text that is gained from the Textbox : Name = tbSpellName.Text
-
Jun 14th, 2014, 10:31 PM
#10
Re: Bolding and Changing Font Type and Size in Richtextbox
So change redText1 to tbSpellName.Text in wes4dbt's example code
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 15th, 2014, 07:44 AM
#11
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
.paul -
I tried it, couldn't hurt could it.... here is the code with replacement items.... but it still didn't work....
Code:
Dim ndx As Integer, currentFont As System.Drawing.Font = Me.rtb1.SelectionFont
If Not tbSpellName.Text Is Nothing Then
ndx = Me.rtb1.Find(tbSpellName.Text)
If ndx >= 0 Then
Me.rtb1.SelectionColor = Color.Red
Me.rtb1.SelectionFont = New Font( "Visitation", 11, FontStyle.Bold)
End If
End If
-
Jun 15th, 2014, 07:57 AM
#12
Re: Bolding and Changing Font Type and Size in Richtextbox
I created a simple form with an RTB (RichTextBox1), a Textbox (Textbox1) and a button (Button1)
I entered a string in the textbox to test then ran it. I typed a word in the textbox and clicked the button. It found the first instance of the word and changed it. I thought it may have been your font family name so I checked again with that and it worked fine as you can see from the final code below. That means the problem lies elsewhere. You should set a breakpoint and step through checking the values of all the variables to see if they are as you expect.
vb.net Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Colour(TextBox1.Text)
End Sub
Private Sub Colour(ByVal ref)
Dim ndx As Integer, currentFont As System.Drawing.Font = Me.RichTextBox1.SelectionFont
If Not ref Is Nothing Then
ndx = Me.RichTextBox1.Find(ref)
If ndx >= 0 Then
Me.RichTextBox1.SelectionColor = Color.Red
Me.RichTextBox1.SelectionFont = New Font( _
"visitation", _
14, _
FontStyle.Bold)
End If
End If
End Sub
End Class
-
Jun 15th, 2014, 09:54 AM
#13
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
Well then...perhaps seein my whole code would be of much better help. Here it is in its less than glamorous form.....
Code:
Public Class Form1
Public Type, Range, AoE, APC, Defense, Description, Duration, Energy, Effect, RangeSelect, Name1 As String
Public Air As String
Public PowerRank, Difference, RangeInt, AoEInt As Integer
Private Sub Generate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Generate.Click
Type = " "
Energy = " "
Effect = " "
Defense = " "
RangeSelect = " "
Range = " "
Difference = 0
Try
PowerRank = CInt(tbPowerRank.Text)
Catch ex As Exception
If tbPowerRank.Text = "" Then
MessageBox.Show("You need to enter a Power Rank. We have entered 1 as your Power Rank for this Spell or Power.", "Power Rank Required", MessageBoxButtons.OK, MessageBoxIcon.Stop)
tbPowerRank.Text = "1"
End If
End Try
Try
Name = tbSpellName.Text
Catch ex As Exception
If tbSpellName.Text = "" Then
MessageBox.Show("Please enter a name for the spell or power.", "Name Required", MessageBoxButtons.OK, MessageBoxIcon.Stop)
tbSpellName.Text = "No Name"
End If
End Try
For i As Integer = 0 To lbType.SelectedItems.Count - 1
Type = Type & lbType.SelectedItems(i).ToString & "; "
Next
For i As Integer = 0 To lbEnergy.SelectedItems.Count - 1
Energy = Energy & "[" & lbEnergy.SelectedItems(i).ToString & "]"
Next
For i As Integer = 0 To lbEffect.SelectedItems.Count - 1
Effect = Effect & "[" & lbEffect.SelectedItems(i).ToString & "]"
Next
For i As Integer = 0 To lbDefense.SelectedItems.Count - 1
Defense = Defense & lbDefense.SelectedItems(i).ToString
Next
Name = tbSpellName.Text
PowerRank = CInt(tbPowerRank.Text)
Description = tbDescription.Text
Duration = cbxDuration.SelectedItem
Range = cbxRange.SelectedItem
AoE = cbxAoE.SelectedItem
APC = cbxActivation.SelectedItem
'Check Area of Effect
Select Case AoE
Case "30' +5'/success (Max Bonus +50')"
Select Case PowerRank
Case Is <> "6", "7"
MessageBox.Show("The AoE of 30' +5'/success (Max Bonus +50'), should be used with Power Rank 6 or 7 spells/powers.", "Area of Effect Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Select
Case "40' +5'/success (Max Bonus +50')"
Select Case PowerRank
Case Is <> "8", "9"
MessageBox.Show("The AoE of 40' +5'/success (Max Bonus +50'), should be used with Power Rank 8 or 9 spells/powers.", "Area of Effect Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Select
Case "50' +5'/success (Max Bonus +50')"
Select Case PowerRank
Case Is <> "10", "11"
MessageBox.Show("The AoE of 50' +5'/success (Max Bonus +50'), should be used with Power Rank 10 or 11 spells/powers.", "Area of Effect Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Select
Case "60' +5'/success (Max Bonus +50')"
Select Case PowerRank
Case Is <> "12"
MessageBox.Show("The AoE of 60' +5'/success (Max Bonus +50'), should be used with Power Rank 12 spells/powers.", "Area of Effect Incorrect", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End Select
End Select
'Cross Check that AoE is not bigger than Range; First Give Range and AoE Integer Numbers
Select Case AoE
Case "Self"
AoEInt = 0
Case "Target"
AoEInt = 1
Case "10'"
AoEInt = 10
Case "15'"
AoEInt = 15
Case "20'"
AoEInt = 20
Case "20' +5'/success (Max Bonus +50')"
AoEInt = 20
Case "25' +5'/success (Max Bonus +50')"
AoEInt = 25
Case "30' +5'/success (Max Bonus +50')"
AoEInt = 30
Case "40' +5'/success (Max Bonus +50')"
AoEInt = 40
Case "50' +5'/success (Max Bonus +50')"
AoEInt = 50
Case "60' +5'/success (Max Bonus +50')"
AoEInt = 60
End Select
Select Case Range
Case "Self"
RangeInt = 0
Case "Target"
RangeInt = 1
Case "10'"
RangeInt = 10
Case "20'"
RangeInt = 20
Case "30'"
RangeInt = 30
Case "40' +5/success (Max Bonus +50')"
RangeInt = 40
Case "45' +5/success (Max Bonus +50')"
RangeInt = 45
Case "50' +5/success (Max Bonus +50')"
RangeInt = 50
Case "55' +5/success (Max Bonus +50')"
RangeInt = 55
Case "60' +5/success (Max Bonus +50')"
RangeInt = 60
Case "65' +5/success (Max Bonus +50')"
RangeInt = 65
Case "70' +5/success (Max Bonus +50')"
RangeInt = 40
Case "75' +5/success (Max Bonus +50')"
RangeInt = 45
Case "80' +5/success (Max Bonus +50')"
RangeInt = 80
Case "85' +5/success (Max Bonus +50')"
RangeInt = 85
End Select
If AoEInt > RangeInt Then
MessageBox.Show("The AoE is greater then the Range of the Spell, please select a smaller AoE or a larger Range, but your selection will be used!", "Area of Effect is too big!", MessageBoxButtons.OK, MessageBoxIcon.Stop)
End If
'--------------------------------------------------------------------------------------------------------------------
'With Me.rtb1
' 'rtb1.SelectionStart = 0
' 'rtb1.SelectionLength = 25
' rtb1.SelectionFont = New Font("Visitation", 11, FontStyle.Bold)
' rtb1.SelectedText = tbSpellName.Text '+ ControlChars.Cr
'End With
'Name1 = Name
'Dim Spellname As String = Name
'With Me.rtb1
' .SelectedText = Spellname
' .SelectionFont = New Font("Visitation", 11, FontStyle.Bold)
'End With
''tbResult.Text = Name & vbCrLf _
'Dim myfont As New Font("Visitation", 11, FontStyle.Bold)
'With Me.rtb1
' .SelectionStart = 0
' .SelectionLength = 50
' .SelectionFont = myfont
' '.SelectionFont = New Font(me.rtb1.selectionfont,
'End With
'Dim ndx As Integer, currentFont As System.Drawing.Font = Me.rtb1.SelectionFont
'If Not tbSpellName.Text Is Nothing Then
' ndx = Me.rtb1.Find(tbSpellName.Text)
' If ndx >= 0 Then
' Me.rtb1.SelectionColor = Color.Red
' Me.rtb1.SelectionFont = New Font( "Visitation", 11, FontStyle.Bold)
' End If
'End If
rtb1.Text = Name & vbCrLf _
& "Type: " & CStr(Type) & CStr(Energy) & CStr(Effect) & vbCrLf _
& "Power Rank: " & CStr(PowerRank) & vbCrLf _
& "Range: " & CStr(Range) & vbCrLf _
& "Duration: " & CStr(Duration) & vbCrLf _
& "Area of Effect: " & CStr(AoE) & vbCrLf _
& "Activiation Time: " & CStr(APC) & vbCrLf _
& "Defense: " & CStr(Defense) & vbCrLf _
& Description
End Sub
Private Sub clrfrmBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clrfrmBtn.Click
tbSpellName.Text = " "
tbPowerRank.Text = " "
tbDescription.Text = " "
cbxDuration.SelectedIndex = -1
cbxRange.SelectedIndex = -1
cbxAoE.SelectedIndex = -1
cbxActivation.SelectedIndex = -1
Description = " "
rtb1.Text = " "
lbType.SelectedIndex = -1
lbEnergy.SelectedIndex = -1
lbEffect.SelectedIndex = -1
lbDefense.SelectedIndex = -1
tbSpellName.Focus()
End Sub
End Class
Not Sure if that helps.....BTW trying your new code.....
-
Jun 15th, 2014, 10:02 AM
#14
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
I do have a question though....
How is it that :
Colour(TextBox1.Text) works for you? Your expression is not a Method is it?
I replaced it with: Name(tbSpellName.Text)
-
Jun 15th, 2014, 11:06 AM
#15
Re: Bolding and Changing Font Type and Size in Richtextbox
Wow, that is a mess. Let me begin by asking a simple question. How can you change the text color or font of something in the rtb before you put the text in the rtb.
-
Jun 15th, 2014, 11:12 AM
#16
Re: Bolding and Changing Font Type and Size in Richtextbox
Colour is a user-defined sub. I included it in the code I posted. I simply pass the value of the textbox text to my sub and the sub dies the highlighting.
I notice from your code however, that you have been trying to set the highlighting BEFORE you set the text of the RTB. Is that really how your code was or have you moved things around to post it?
-
Jun 15th, 2014, 12:58 PM
#17
Thread Starter
Junior Member
Re: Bolding and Changing Font Type and Size in Richtextbox
Originally Posted by wes4dbt
Wow, that is a mess. Let me begin by asking a simple question. How can you change the text color or font of something in the rtb before you put the text in the rtb.
I was under the impression that it was not yet in the rtb1 meaning that it was in the lbSpellName.text or held in Name. Are you meaning to say that I should attempt the change after putting it in the RTB1? hmmmm..I was not aware of that!!!! I moved it around and WHAM it worked... Thanks for the tip!!!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|