Search code examples
vb.netfontsreferenceinitializationdrawing

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.


Solution

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