none
Font name as a variable RRS feed

  • Question

  • I was wondering if I can set up an array with a few different font names so that I can pick and choose them? The code I am using is

    Font("Calibri", 24, FontStyle.Regular)

    I would like to have it set up like the following:

    f(1)="Calibri"

    f(2)="Garamond"

    So I can set it up like:

    Font(f(2), 24, FontStyle.Regular)

    Can this be done?

    Thank you in advance,

    Les

    Tuesday, August 21, 2018 6:05 PM

Answers

  • My approach would probably be to store the entire font as font

            Dim FontList As New List(Of Font)
            FontList.Add(New Font("Arial", 12, FontStyle.Regular))
            'etc
            TextBox1.Font = FontList(0)


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by Les2011 Wednesday, August 22, 2018 12:18 AM
    Tuesday, August 21, 2018 7:08 PM
  • Hi

    Here is a stand alone example which may illustrate what you are asking for.

    ' Form1 with TextBox1 and
    ' Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim myFontFamilies As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With myFontFamilies
          .Add("Arial")
          .Add("Calibri")
          .Add("Garamond")
          .Add("WingDings")
        End With
        With TextBox1
          TextBox1.Font = New Font(myFontFamilies(0), 24, FontStyle.Regular)
          .Text = "Test for Font changes"
          .Location = New Point(8, 12)
          .Size = New Size(ClientRectangle.Width - 16, 24)
          .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        End With
        With Button1
          .Location = New Point(TextBox1.Right - Button1.Width - 4, ClientRectangle.Bottom - Button1.Height - 4)
          .Text = "GO"
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        End With
        Size = New Size(400, 130 + TextBox1.Height)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static a As Integer = 1
        TextBox1.Font = New Font(myFontFamilies(a), 24, FontStyle.Regular)
        a += 1
        If a > myFontFamilies.Count - 1 Then a = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Les2011 Tuesday, August 21, 2018 6:58 PM
    Tuesday, August 21, 2018 6:49 PM

All replies

  • Hi

    Here is a stand alone example which may illustrate what you are asking for.

    ' Form1 with TextBox1 and
    ' Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim myFontFamilies As New List(Of String)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With myFontFamilies
          .Add("Arial")
          .Add("Calibri")
          .Add("Garamond")
          .Add("WingDings")
        End With
        With TextBox1
          TextBox1.Font = New Font(myFontFamilies(0), 24, FontStyle.Regular)
          .Text = "Test for Font changes"
          .Location = New Point(8, 12)
          .Size = New Size(ClientRectangle.Width - 16, 24)
          .Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        End With
        With Button1
          .Location = New Point(TextBox1.Right - Button1.Width - 4, ClientRectangle.Bottom - Button1.Height - 4)
          .Text = "GO"
          .Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
        End With
        Size = New Size(400, 130 + TextBox1.Height)
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Static a As Integer = 1
        TextBox1.Font = New Font(myFontFamilies(a), 24, FontStyle.Regular)
        a += 1
        If a > myFontFamilies.Count - 1 Then a = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Les2011 Tuesday, August 21, 2018 6:58 PM
    Tuesday, August 21, 2018 6:49 PM
  • Hi Leshay,

    That's exactly what the doctored ordered <S>

    Thanks

    Les

    Tuesday, August 21, 2018 6:58 PM
  • My approach would probably be to store the entire font as font

            Dim FontList As New List(Of Font)
            FontList.Add(New Font("Arial", 12, FontStyle.Regular))
            'etc
            TextBox1.Font = FontList(0)


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    • Marked as answer by Les2011 Wednesday, August 22, 2018 12:18 AM
    Tuesday, August 21, 2018 7:08 PM