none
How to set font color in a font construct RRS feed

  • Question

  • I went looking for a way to allow me to set a font color using the FONT construct but was unable to find it in any documentation.

    I want to be able to set style, font, size and color.

    For example

    Dim f7 AsNewFont("Calabria", 20, FontStyle.Italic) allows me to set font type, size and style but how do I incorporate the color of the font?

    Thanks in advance,

    Les

    Sunday, March 15, 2020 4:48 PM

Answers

  • Hi

    A Font doesn't have a ForeGround/BackGround color as such. The container you use it in does, or, in Graphics, a Brush.

    e.g. A TextBox has a Font Property and if you check it our, there are no color properties for it - the TextBox itself does have ForeColor and BackColor Properties

    The fonr is strictly that, a Font.


    Regards Les, Livingston, Scotland




    • Edited by leshay Sunday, March 15, 2020 5:29 PM
    • Proposed as answer by Ashidacchi Sunday, March 15, 2020 6:26 PM
    • Marked as answer by Les2011 Sunday, March 15, 2020 7:31 PM
    Sunday, March 15, 2020 5:24 PM
  • Hello,

    Place a control on a form such as a TextBox, set the font and color. Then under the project window select show all properties, look at the control, in this case a TextBox.

    You have this

    TextBox1.Font = New System.Drawing.Font(
        "Calibri", 20.25!,
        System.Drawing.FontStyle.Italic,
        System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    
    TextBox1.ForeColor = System.Drawing.Color.Red

    Then in your code

    TextBox1.Font = New Font(
        "Calibri", 20.25!,
        FontStyle.Italic,
        GraphicsUnit.Point, 0)
    
    TextBox1.ForeColor = Color.Red

    Or

    With TextBox1
        .Font = New Font(
            "Calibri", 20.25!,
            FontStyle.Italic,
            GraphicsUnit.Point, 0)
        .ForeColor = Color.Red
    End With


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Sunday, March 15, 2020 5:24 PM
    Moderator
  •  

    For drawing graphics you have the color of brushes and pens.

    The font is like the "stencil" and then you "paint" the stencil with a line pen or a fill brush using the assigned "paint" color. Pens and Brushes can also have hatches, dashstyles, gradient fills, etc.

     

     

    Public Class Form6
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles Me.Load
            Text = "Brushes and Pens"
            ClientSize = New Size(300, 300)
    
        End Sub
    
        Private Sub Form6_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    
            With e.Graphics
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Using pSolid As New Pen(Color.Yellow, 10) With
                                {.EndCap = Drawing2D.LineCap.ArrowAnchor},
                        pDash As New Pen(Color.Red, 5) With
                                {.DashStyle = Drawing2D.DashStyle.Dash},
                        brSolid As New SolidBrush(Color.Firebrick),
                        brSemiOpaque As New SolidBrush(Color.FromArgb(150, Color.SkyBlue)),
                        brLg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(100, 100), Color.White, Color.Maroon),
                        brHatch As New Drawing2D.HatchBrush(Drawing2D.HatchStyle.DiagonalBrick, Color.Blue, Color.SkyBlue),
                        pHatch As New Pen(brHatch, 16)
    
                    .DrawLine(pSolid, 50, 40, 200, 50)
                    .DrawEllipse(pDash, New Rectangle(50, 80, 150, 30))
    
                    .DrawLine(pHatch, 50, 140, 200, 130)
                    .FillRectangle(brSemiOpaque, New Rectangle(50, 120, 150, 30))
    
                    .FillRectangle(brLg, New Rectangle(50, 160, 150, 30))
    
                    .FillPie(brHatch, New Rectangle(50, 200, 150, 30), 0, 270)
    
                    .DrawString("tommytwotrain", New Font("arial", 30, FontStyle.Bold), brLg, 5, 250)
                End Using
            End With
        End Sub
    End Class



    • Edited by tommytwotrain Sunday, March 15, 2020 6:13 PM add text
    • Marked as answer by Les2011 Sunday, March 15, 2020 7:33 PM
    Sunday, March 15, 2020 6:04 PM

All replies

  • Hi

    A Font doesn't have a ForeGround/BackGround color as such. The container you use it in does, or, in Graphics, a Brush.

    e.g. A TextBox has a Font Property and if you check it our, there are no color properties for it - the TextBox itself does have ForeColor and BackColor Properties

    The fonr is strictly that, a Font.


    Regards Les, Livingston, Scotland




    • Edited by leshay Sunday, March 15, 2020 5:29 PM
    • Proposed as answer by Ashidacchi Sunday, March 15, 2020 6:26 PM
    • Marked as answer by Les2011 Sunday, March 15, 2020 7:31 PM
    Sunday, March 15, 2020 5:24 PM
  • Hello,

    Place a control on a form such as a TextBox, set the font and color. Then under the project window select show all properties, look at the control, in this case a TextBox.

    You have this

    TextBox1.Font = New System.Drawing.Font(
        "Calibri", 20.25!,
        System.Drawing.FontStyle.Italic,
        System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    
    TextBox1.ForeColor = System.Drawing.Color.Red

    Then in your code

    TextBox1.Font = New Font(
        "Calibri", 20.25!,
        FontStyle.Italic,
        GraphicsUnit.Point, 0)
    
    TextBox1.ForeColor = Color.Red

    Or

    With TextBox1
        .Font = New Font(
            "Calibri", 20.25!,
            FontStyle.Italic,
            GraphicsUnit.Point, 0)
        .ForeColor = Color.Red
    End With


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange



    Sunday, March 15, 2020 5:24 PM
    Moderator
  •  

    For drawing graphics you have the color of brushes and pens.

    The font is like the "stencil" and then you "paint" the stencil with a line pen or a fill brush using the assigned "paint" color. Pens and Brushes can also have hatches, dashstyles, gradient fills, etc.

     

     

    Public Class Form6
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles Me.Load
            Text = "Brushes and Pens"
            ClientSize = New Size(300, 300)
    
        End Sub
    
        Private Sub Form6_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    
            With e.Graphics
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                Using pSolid As New Pen(Color.Yellow, 10) With
                                {.EndCap = Drawing2D.LineCap.ArrowAnchor},
                        pDash As New Pen(Color.Red, 5) With
                                {.DashStyle = Drawing2D.DashStyle.Dash},
                        brSolid As New SolidBrush(Color.Firebrick),
                        brSemiOpaque As New SolidBrush(Color.FromArgb(150, Color.SkyBlue)),
                        brLg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(100, 100), Color.White, Color.Maroon),
                        brHatch As New Drawing2D.HatchBrush(Drawing2D.HatchStyle.DiagonalBrick, Color.Blue, Color.SkyBlue),
                        pHatch As New Pen(brHatch, 16)
    
                    .DrawLine(pSolid, 50, 40, 200, 50)
                    .DrawEllipse(pDash, New Rectangle(50, 80, 150, 30))
    
                    .DrawLine(pHatch, 50, 140, 200, 130)
                    .FillRectangle(brSemiOpaque, New Rectangle(50, 120, 150, 30))
    
                    .FillRectangle(brLg, New Rectangle(50, 160, 150, 30))
    
                    .FillPie(brHatch, New Rectangle(50, 200, 150, 30), 0, 270)
    
                    .DrawString("tommytwotrain", New Font("arial", 30, FontStyle.Bold), brLg, 5, 250)
                End Using
            End With
        End Sub
    End Class



    • Edited by tommytwotrain Sunday, March 15, 2020 6:13 PM add text
    • Marked as answer by Les2011 Sunday, March 15, 2020 7:33 PM
    Sunday, March 15, 2020 6:04 PM
  • Yes I see that now thx Leshay

    Sunday, March 15, 2020 7:31 PM
  • Hi Karen,

    Thx for the examples.  With them it provides me with a way of getting around this issue.

    Les

    Sunday, March 15, 2020 7:32 PM
  • Hi Tom,

    As always you are the graphics guru <S>.  Thx for the examples I appreciate it.

    Les

    Sunday, March 15, 2020 7:33 PM