Setting different fonts in a concatenated string

I'm writing a Referencing and Citation builder

Page 1 of 1

6 Replies - 16788 Views - Last Post: 03 November 2008 - 11:54 PM Rate Topic: -----

#1 Apache  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 46
  • Joined: 03-November 08

Setting different fonts in a concatenated string

Posted 03 November 2008 - 09:39 PM

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:

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...
       '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: 03 November 2008 - 09:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Setting different fonts in a concatenated string

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Setting different fonts in a concatenated string

Posted 03 November 2008 - 10:03 PM

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.

    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. :)

"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?" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Apache  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 46
  • Joined: 03-November 08

Re: Setting different fonts in a concatenated string

Posted 03 November 2008 - 10:59 PM

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.

    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


Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Setting different fonts in a concatenated string

Posted 03 November 2008 - 11:07 PM

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...

    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)

;)
Was This Post Helpful? 0
  • +
  • -

#5 Apache  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 46
  • Joined: 03-November 08

Re: Setting different fonts in a concatenated string

Posted 03 November 2008 - 11:36 PM

ok, this almost works now...

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: 03 November 2008 - 11:38 PM

Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4309
  • View blog
  • Posts: 12,088
  • Joined: 18-April 07

Re: Setting different fonts in a concatenated string

Posted 03 November 2008 - 11:43 PM

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...

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



:)
Was This Post Helpful? 0
  • +
  • -

#7 Apache  Icon User is offline

  • New D.I.C Head

Reputation: 11
  • View blog
  • Posts: 46
  • Joined: 03-November 08

Re: Setting different fonts in a concatenated string

Posted 03 November 2008 - 11:54 PM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1