Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,309 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 839 people online right now. Registration is fast and FREE... Join Now!




Setting different fonts in a concatenated string

 
Reply to this topicStart new topic

Setting different fonts in a concatenated string, I'm writing a Referencing and Citation builder

Apache
3 Nov, 2008 - 08:39 PM
Post #1

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 45



Thanked: 8 times
My Contributions
I'm writing a Reference and Citation Builder based on the Harvard Method of Referencing, later I plan to incorporate Numerical method into it as well as being able to reference many kinds of source, possibly incorporating an ISBN Lookup and/or BarCode reader.

For now though, while my head isn't so far in the clouds, I've come across a problem that I can't figure out. (I'm using this bespoke project to teach me VB.NET).

I've manually concatenated various strings together to create the reference but the title needs to be in BoldItalic. How would I change the font properties of the HVD_ARTICLE_TITLE string to reflect this?

i.e.
QUOTE
Bloggs, J. (2008) Need VB.NET Help England:Publishment INC

instead of
QUOTE
Bloggs, J. (2008) Need VB.NET Help England:Publishment INC


Here is my code at the moment:

vb
Private Sub btnHVDBuildRef_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHVDBuildRef.Click

HVD_ARTICLE_AUTHOR_SURNAME = txtHvdArticleAuthorSurname.Text
HVD_ARTICLE_AUTHOR_INITIAL = txtHvdArticleAuthorInitial.Text.ToUpper & _
txtHvdArticleAuthorInitial2.Text.ToUpper & _
txtHvdArticleAuthorInitial3.Text.ToUpper
HVD_ARTICLE_TITLE = txtHvdArticleTitle.Text
HVD_ARTICLE_DATE = txtHvdArticleDateYear.Text
HVD_ARTICLE_PLACE_OF_PUBLISHMENT = txtHvdPlaceOfPublishment.Text
HVD_ARTICLE_PUBLISHER = txtHvdPublisher.Text
OUTPUT_REFERENCE = HVD_ARTICLE_AUTHOR_SURNAME & ", " & HVD_ARTICLE_AUTHOR_INITIAL & ". (" & HVD_ARTICLE_DATE & ") " & HVD_ARTICLE_TITLE & " " & HVD_ARTICLE_PLACE_OF_PUBLISHMENT & ":" & HVD_ARTICLE_PUBLISHER
OUTPUT_CITATION = "(" & HVD_ARTICLE_AUTHOR_SURNAME & ", " & HVD_ARTICLE_DATE & ")"

Select Case ValidateReference()
Case True
txtRefOutput.Text = OUTPUT_REFERENCE
txtCiteOutput.Text = OUTPUT_CITATION
Case False
MsgBox("Could not build reference.", MsgBoxStyle.OkOnly, "Invalid Data Entry")
End Select

End Sub 'Build Reference Button'

Private Function ValidateReference() As Boolean
If HVD_ARTICLE_AUTHOR_SURNAME = "" Or HVD_ARTICLE_AUTHOR_INITIAL = "" Or HVD_ARTICLE_DATE = "" Or HVD_ARTICLE_TITLE = "" Or HVD_ARTICLE_PLACE_OF_PUBLISHMENT = "" Or HVD_ARTICLE_PUBLISHER = "" Then
MsgBox("One or more fields are empty, please check and try again.", MsgBoxStyle.OkOnly, "Invalid Data Entry")
Return False
Else
Return True
End If
End Function


And my variables...
vb
       'The Big List Of Variables'
'~~~~~~~~~~~~~~~~~~~~~~~~~'

'Global Variables'
Dim BR As String = Chr((13) & (10))

'Reference Variables'
Dim REFERENCE_METHOD As String
Dim REFERENCE_SOURCE As String
Dim OUTPUT_REFERENCE As String
Dim OUTPUT_CITATION As String
Dim VALID_REFERENCE As Boolean
Dim NUM_REFERENCE_NUMBER As Integer

'Harvard Specific Variables'
Dim _
HVD_ARTICLE_AUTHOR_SURNAME, _
HVD_ARTICLE_AUTHOR_INITIAL, _
HVD_ARTICLE_CO_AUTHOR_NAME, _
HVD_ARTICLE_CO_AUTHOR_INITIAL, _
HVD_ARTICLE_DATE, _
HVD_ARTICLE_TITLE, _
HVD_ARTICLE_PAGE, _
HVD_ARTICLE_PLACE_OF_PUBLISHMENT, _
HVD_ARTICLE_PUBLISHER, _
HVD_NEWSPAPER_NAME, _
HVD_WEBSITE_URL, _
HVD_DATE_ACCESSED, _
HVD_DATE_UPDATED, _
HVD_INTERVIEWER _
As String


Also is it better to declare variables in singular (Like the reference variable strings) or in block (Like the Harvard strings)?

Cheers in advance.

--EDIT--

NB: All txtNames are RichTextBox Elements rather than TextBox Elements, I know they should be rtfNames, I'll go back and change them later on.

This post has been edited by Apache: 3 Nov, 2008 - 08:45 PM

User is offlineProfile CardPM
+Quote Post


Martyr2
RE: Setting Different Fonts In A Concatenated String
3 Nov, 2008 - 09:03 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well what you got to do is...

1) Add your text to the richtextbox
2) Select the portion of your richtextbox you want to change. You can do this through code using selectionstart, find etc (example below)
3) Change the font and style of that selection.

vb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Here is our string, we give it to the RichTextbox
Dim hello As String = "hello there sir"
RichTextBox1.Text = hello

' Here we find the words we want to bold and start a selection
RichTextBox1.SelectionStart = RichTextBox1.Find("there")

' Tell the Richtextbox to assign a new font and style to the selection
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
End Sub


In the code above we assign the sentence "hello there sir" to the richtextbox. We then find the word "there" and select it. We then tell the richtextbox to modify the text selected with our new font and font style.

The result is that in our example "there" is bold.

Hope that helps you out. smile.gif

"At DIC we be bold code ninjas... we leap into danger without a second thought we are so bold... boy we shouldn't have gotten in that white truck with the rather 'large' woman huh winstinology?" decap.gif
User is offlineProfile CardPM
+Quote Post

Apache
RE: Setting Different Fonts In A Concatenated String
3 Nov, 2008 - 09:59 PM
Post #3

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 45



Thanked: 8 times
My Contributions
That would be ok but the string is a variable, there's no way of knowing until runtime what the value of the string HVD_ARTICLE_TITLE will be, or how long it will be, or where it will appear within the concanenated OUTPUT_REFERENCE string.

I've thought of a rather messy and longwinded way of doing it but it may mess up the validation when it becomes more thorough.

1. Add in "TITLESTART" and "TITLEEND" as hardcoded strings into the concatenation.
2. Find and Select this range within the RTF
3. BoldItalic the Selection
4. Find, Select and Delete "TITLESTART"
5. Find, Select and Delete "TITLEEND"

This is very bad coding practice though and I'd like to steer clear of doing anything to the string once it's concatented (because it's been validated). There must be a way to set the Font Properties for the HVD_ARTICLE_TITLE string alone.

From your code above, imagine it was like this and we only wanted the text in RichTextBox2 to be bold. 1, 2 and 3 are inputted by the user at runtime.

vb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Here is our string, we give it to the RichTextbox
Dim hello As String = RichTextBox1.Text & " " & RichTextBox2.Text & " " & RichTextBox3.ToUpper
RichTextBox4.Text = hello

' Here we find the words we want to bold and start a selection
RichTextBox4.SelectionStart = RichTextBox4.Find("WHAT GOES HERE?")

' Tell the Richtextbox to assign a new font and style to the selection
RichTextBox4.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
End Sub


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Setting Different Fonts In A Concatenated String
3 Nov, 2008 - 10:07 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well remember, my demo and using Find was only an example. You can use selectionstart and selectionlength. You can use the length of your values in each variable to determine where a title starts and ends.

If you wanted to concatenate "hello " + "there " + "sir" you can select "there" by finding the length of "hello " (which will give you the selectionstart) and go for the length of "there".

Example tweaked below...

vb

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Here is our string, we give it to the RichTextbox
Dim hello As String = "hello there sir"
RichTextBox1.Text = hello

' Here we start the bold from where hello ends and for the length of "there"
RichTextBox1.SelectionStart = "hello ".Length
RichTextBox1.SelectionLength = "there".Length

' Tell the Richtextbox to assign a new font and style to the selection
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
End Sub


Notice that we hardcoded the strings for length etc, but you can certainly use your variables in their place... (aka HVD_ARTICLE_TITLE.length would get the length of the title)

wink2.gif
User is offlineProfile CardPM
+Quote Post

Apache
RE: Setting Different Fonts In A Concatenated String
3 Nov, 2008 - 10:36 PM
Post #5

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 45



Thanked: 8 times
My Contributions
ok, this almost works now...

vb
Select Case ValidateReference()
Case True
txtRefOutput.Text = OUTPUT_REFERENCE
txtCiteOutput.Text = OUTPUT_CITATION

'Format the output.
txtRefOutput.SelectionStart = HVD_ARTICLE_AUTHOR_SURNAME.Length + HVD_ARTICLE_AUTHOR_INITIAL.Length + HVD_ARTICLE_DATE.Length + 7
txtRefOutput.SelectionLength = HVD_ARTICLE_TITLE.Length
txtRefOutput.SelectionFont = New System.Drawing.Font(txtRefOutput.Font, FontStyle.Bold)
Case False
MsgBox("Could not build reference.", MsgBoxStyle.OkOnly, "Invalid Data Entry")
End Select


It's still very messy for my liking, it would be a lot better to just set the font properties of the string itself rather than mess around with the output. Once all the different options are set our within the program, this will mean another 100 lines minimum of code that it seems shouldn't be needed. It does seem like very bad coding practice as well to go back and mess with it once it's set in stone so to speak.

Also, if I set

txtRefOutput.SelectionFont = New System.Drawing.Font(txtRefOutput.Font, FontStyle.Bold)

to

txtRefOutput.SelectionFont = New System.Drawing.Font(txtRefOutput.Font, FontStyle.Bold & FontStyle.Italic)

it sets it as underlined and strikethrough. Using the Boolean And it dosn't change it at all. There's no BoldItalic setting so how do I get both Bold and Italic.

This post has been edited by Apache: 3 Nov, 2008 - 10:38 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Setting Different Fonts In A Concatenated String
3 Nov, 2008 - 10:43 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 6,656



Thanked: 613 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well the code is not that messy at all really. I think you are being just a little TOO critical in it.

As for the font being bold and italic, you can combine values by "ORing" them together...

vb

' Selection is bolded and italicized
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold Or FontStyle.Italic)


smile.gif
User is offlineProfile CardPM
+Quote Post

Apache
RE: Setting Different Fonts In A Concatenated String
3 Nov, 2008 - 10:54 PM
Post #7

New D.I.C Head
*

Joined: 3 Nov, 2008
Posts: 45



Thanked: 8 times
My Contributions
Thanks for your help with it, I'll try and find a simpler way to do it once the program is build up a bit more.

I'll probably end up throwing all the formatting in one big Select Case function so atleast the core code won't be so packed.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 07:07PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month