Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a ransom note style message by drawing text using different fonts for each character in a RichTextBox in Visual Basic .NET
DescriptionThis example shows how to make a ransom note style message by drawing text using different fonts for each character in a RichTextBox in Visual Basic .NET.
Keywordsransom note, text, string, random fonts, random characters, Visual Basic .NET, VB .NET
CategoriesStrings, Puzzles and Games, VB.NET
 

The example Draw text using different fonts for each character explains how you can draw a string with each character drawn in a random font. This example does the same thing except it places the characters in a RichTextBox so you can copy and paste it into a document or email (assuming your email system can handle RTF text).

The program loops through each character in the string and calls PrintCharacter to draw each with a random font.

 
' Print this character in a random font.
Private Sub PrintCharacter(ByVal ch As String, ByVal rch As _
    RichTextBox)
    ' Add the character at the end of the RichTextBox.
    rch.AppendText(ch)

    ' Select the new character.
    rch.Select(rch.TextLength - 1, 1)

    ' Pick a random font.
    Dim font_name As String = FontNames(Rand.Next(0, _
        FontNames.Count))
    Dim font_size As Double = MIN_SIZE + (MAX_SIZE - _
        MIN_SIZE) * Rand.NextDouble()
    Dim font_style As FontStyle = FontStyle.Regular
    If Rand.Next(0, 2) = 0 Then font_style = font_style Or _
        FontStyle.Bold
    If Rand.Next(0, 2) = 0 Then font_style = font_style Or _
        FontStyle.Italic
    ' If Rand.Next(0, 2) = 0 Then font_style = font_style Or
    ' FontStyle.Strikeout
    ' If Rand.Next(0, 2) = 0 Then font_style = font_style Or
    ' FontStyle.Underline
    Using the_font As New Font(font_name, _
     CSng(font_size), font_style, GraphicsUnit.Point)
        rch.SelectionFont = the_font
    End Using
    rch.SelectionCharOffset = Rand.Next(-5, 5)
    rch.SelectionColor = Color.FromArgb(QBColor(Rand.Next(0, _
        16)))
    ' rch.SelectionBackColor =
    ' Color.FromArgb(QBColor(Rand.Next(0, 16)))
End Sub
 
PrintCharacter adds a character to the end of the RichTextBox's text. It then selects that character and picks random font properties for it.
 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated