Setting a font directly vs using FontFamily - vb.net

Assume b to be an instance of the Button class. Is there any difference when we directly create a font as in: b.Font = New Font("Arial", 15, FontStyle.Bold Or FontStyle.Italic)
Versus
when we create a font family first and then create a font from that, for example:
Dim fontF As New FontFamily("Arial")
b.Font = New Font(fontF, 15, FontStyle.Bold Or FontStyle.Italic)
I have a feeling that there is no difference between these two and that there is no extra advantage if we use the FontFamily class. Am I Right? In other words, are there situations where using FontFamily class helps us?

Related

Can not we initialize a Font for second time in VB.NET?

I am making a 2D game in VB.NET. I use only one font object to draw strings on the form. This font is only needed in Game Menu. Therefore I dispose the font when it is not necessary and initialize it again when needed.
font_1 = New Font("Autobus Bold", 15.0)
When I use this font (font_1) to draw a string on the form, I get this error.
An unhandled exception of type 'System.ArgumentException' occurred in
System.Drawing.dll
Additional information: Parameter is not valid.
When I view the the font, it shows,
{Name = Reference to a non-shared member requires an object reference.
Size=15.0}
This error doesn't happen when the game menu is loaded for the first time (when font_1 is initialized for the first time). When the user plays the game, font is disposed. When user enters to Game Menu again, font is initialized again before it is used for drawing. When font is used for drawing a string on the window, this error happens.
It looks like error is only within the Font Family. I saw this question in few forums, but no one had given a solution. (This is my first question in a forum)
Edited : I removed the Font(font_1). But still I get the same error. Here is the code that draws the string.
Private Sub mcFramesHandler_TIMER_Tick(sender As Object, e As EventArgs) Handles mcFramesHandler_TIMER.Tick
gB.Clear(Color.Black)
gB.DrawImage(Background_IMG, 0, 0, 640, 480)
Select Case currentMode
Case GameMode.OnGame
If mcShoot_TIMER.Enabled Then gB.DrawImage(Bullet_IMG, Bullet_X, Bullet_Y, 20, 50)
If mcEneShoot_TIMER.Enabled Then gB.DrawImage(EneBullet_IMG, EneBullet_X, EneBullet_Y, 20, 50)
If Shooter_Lives Then gB.DrawImage(Shooter_IMG, Shooter_X, Bullet_Y_Def, 100, 105)
If mcMoveEnemy_TIMER.Enabled Then gB.DrawImage(Enemy_IMG, Enemy_X, 10, 100, 80)
If mcExplode_TMER.Enabled Then gB.DrawImage(Explotion_IMG, Explotion_X, Explotion_Y, 100, 80)
Case GameMode.Begining
gB.DrawString("Start", New Font("Autobus Bold", 15.0), textBrush(0), 110, 98) 'Error is generated in this line
gB.DrawString("Credits", New Font("Autobus Bold", 15.0), textBrush(1), 102, 158)
gB.DrawString("Exit", New Font("Autobus Bold", 15.0), textBrush(2), 114, 218)
End Select
Me.CreateGraphics.DrawImage(backbuffer, 0, 0, 640, 480)
End Sub
Here textBrudh(0) is a brush. gB is the Graphic object. gB successfully draws the background image before it draws the string. This happens only when Game Menu is displayed
Your support is really appreciated.
After you have finished to draw your things you need to dispose all objects around Graphics, not only the font but all (better with closure techniques like code below)
If you need a new instace of Graphics you can create this at the moment this is needed without using a Global object (when you work with Graphics).
Using gB As Graphics = Me.CreateGraphics
Using textBrush As Brush = Brushes.Black
Using font_1 As Font = New Font("Courier New", 15)
gB.DrawString("Start", font_1, textBrush, 400, 98)
'gB.DrawOtherthings()
'gB.DrawOtherthings()
'gB.DrawOtherthings()
'gB.DrawOtherthings()
'gB.DrawOtherthings()
'gB.DrawOtherthings()
'gB.DrawOtherthings()
'gB.DrawOtherthings()
'gB.DrawOtherthings()
End Using
End Using
End Using
Problem was with the brush (textBrush : an array of brush). When I dsipose each item in the array and re initialize, looks like something has happened to them (like they are not brushes anymore. See the error again. It says "Parameter is not valid"). So I just redim the array to 0.
Redim textBrush (0)
This clears previous items. Then I redim the array again initilizes each of them when needed.
Redim textBrush (2)
textBrush (0) = Brushes.Yellow
textBrush (1) = Brushes.Red
textBrush (2) = Brushes.Red
This array is used to change text color when up and down key is pressed.
I saw lot of people had mentioned this error in forums
{Name = Reference to a non-shared member requires an object reference. Size=15.0}
I don't what this name is, but it doesn't effect the programme. It is always like that in any Font.

How to change TextBox.text font style to Italic

I have a textbox control called tb_remarks ,tried to make texts written in tb_remarks using
tb_remark.Font.Italic = True but getting read only exception.
Note : I don't want to change control's property using designer
Use the Font-constructor and assign it to the textbox:
Dim font = New Font(tb_remark.Font, FontStyle.Italic)
tb_remark.Font = font
By passing the old Font you use all other properties as prototype.
You should initialize new style for the font as per Control.Font Property and assign italic-style
tb_remarks.Font = New Font(Font, FontStyle.Italic)

Changing text color in PdfSharp

I'm currently creating a PDF with PdfSharp which mostly consists of text and some images.
The text elements have different colors. My problem is that as soon as I use a different color than the color I started with, the text is not visible in the resulting PDF (e.g. I start with black text, switch to a red text, the red text is not visible). All text elements are in the resulting PDF (I can select them), but the red elements are invisible.
So here is the code:
// Create a new PDF document with one page
var document = new PdfDocument();
var page = document.AddPage();
page.Width = 800;
page.Height = 600;
var defaultFont = new XFont("Arial", 12, XFontStyle.Regular, new XPdfFontOptions(PdfFontEmbedding.Always));
var gfx = XGraphics.FromPdfPage(page);
// black text
gfx.DrawString("black", defaultFont, XBrushes.Black, new XRect(x, y, width, height), XStringFormats.Center);
// red text
gfx.DrawString("red", defaultFont, XBrushes.Red, new XRect(x2, y2, width2, height2), XStringFormats.Center);
I've already found a solution (re-creating the XGraphics object) but it's quiete messy because it needs to be called after each color change:
// ...
// black text
gfx.DrawString("black", defaultFont, XBrushes.Black, new XRect(x, y, width, height), XStringFormats.Center);
// disposing the old graphics context and creating a new one seems to help
gfx.Dispose();
gfx = XGraphics.FromPdfPage(page);
// red text
gfx.DrawString("red", defaultFont, XBrushes.Red, new XRect(x2, y2, width2, height2), XStringFormats.Center);
I guess there is a better solution, but I couldn't find one yet.
Edit
As suggested in this answer, I wanted to create a SSCCE. During the creation I found the actual bug. Instead of XBrushes.Red I used an own defined XBrush, but didn't mention it in the above code snippet, because I thought it was unnecessary.
As already mentioned in the last section of the question, I used an own defined brush instead of XBrushes.Red.
I defined it the following way:
XBrush redBrush = new XSolidBrush(new XColor {R = 207, G = 0, B = 44});
This way the brush only worked after I disposed the graphics object and created a new one. But after some googling I found the correct way to define a brush:
XBrush redBrush = new XSolidBrush(XColor.FromArgb(207, 0, 44));
I tried to replicate your problem using your code snippet and PDFsharp version 1.32. I used VS Express 2013 which automatically converted all projects to .NET 4.5.
I tried both builds (GDI+ and WPF) and all colours worked fine for me.
So instead of just a code snippet you should provide an SSCCE.
See also:
http://forum.pdfsharp.net/viewtopic.php?p=2094#p2094

Set Font.Bold on inherited label

I have to set .Font.Bold = True on label which havent defined .Font property but inherite Font (name, size, style) from form. For that I erases her .Font property from form's designer file.
Now I need to set text of this label to be bold without defining font name, size etc. for this label.
I try:
label6.Font.Bold = True
But this don't work (Property .Font.Bold is readonly).
If I set font for this label like:
label6.Font = New Font(myfontname, 10, FontStyle.Bold, GraphicsUnit.Point)
then I get bold text but label then don't inherite form's font size anymore.
Is here possible to keep form's font inheritance to label but get bold text on such label?
No, as you've already discovered the Font.Bold property is read-only. Font objects are immutable, meaning that their properties cannot be changed once they have been created. The only way to modify this property is to create a new Font object.
When creating this new Font, you can certainly copy the properties of an existing Font object (like the one used by your form), but there is no way to dynamically couple the two Font objects together. If the font size used by your form changes, a new Font object will be created with that new size for the form, but your custom bold Font object will not be updated.
The only thing that makes this confusing is that there is a bit of magic that goes on if you do not set a custom font for child controls. They automatically inherit the font of their parent (a container control, such as a form). Such properties that retrieve their value from their parent when they have not been explicitly set are referred to as ambient properties. Ambient properties are explained in the documentation where applicable. But the upshot is that the ambience goes away at the point where you explicitly set the property. So forget about that.
To achieve what you want, we need to get a notification when the form's font size changes and in response, you can create a new bold Font object with the new size for your Label control. Luckily, there is just such a mechanism in the form of the FontChanged event. Handle the FontChanged event for your form, and in response, create a new Font object for your Label control. For example:
Private Sub myForm_FontChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myForm.FontChanged
Dim oldFont As Font = myLabel.Font
myLabel.Font = New Font(myForm.Font, myForm.Font.Style Or FontStyle.Bold)
oldFont.Dispose()
End Sub
Although, I'm not sure if/why this is really necessary. It's rare that the font size of a form gets changed while the application is running. Generally that happens only at creation, in which case when you retrieve the value to create the custom Font object for your Label control, it would already be set correctly.

How can I use multiple combinations of font styles in VB.NET?

If I want to set my Font I can use
new Font=("Times New Roman", 12, Drawing.FontStyle.Bold)
Instead of Bold, I can use Italic, Regular or Underline.
But I want to make use of Bold and Italic at the same time.
How can I do this?
The FontStyle enumeration is a flags enumeration, so you can combine values (using the Or operator in VB.NET, | in c#):
new Font("Times New Roman", 12, Drawing.FontStyle.Bold Or Drawing.FontStyle.Italic)