none
Value of type 'font' cannot be converted to 'OpenXmlElement' when trying to set font on form from module RRS feed

  • Question

  • Hi Gurus,

    I'm dynamically adding a label to a form from a module & in trying to set the font I get the error "Value of type 'font' cannot be converted to 'OpenXmlElement' when trying to set font on form from module"

            Dim lblLabel As New Label
            'create label
            With lblLabel
                .AutoSize = True
                .Location = New Point(370, 20)
                .Text = "Processing Points Calculations ....."
                .Font = New Font(lblLabel.Font, FontStyle.Bold)
                '.ForeColor() = Color.Black
    
            End With
            frmMainMenu.TabControl1.SelectedTab.Controls.Add(lblLabel)
    I read elsewhere that System.Drawing.dll needed to be added in references but when I tried it said the could not be added & to add a reference to the .NET assembly instead. The system.drawing reference is set in assemblies so I'm lost & need advise


    Saturday, November 3, 2018 1:25 PM

All replies

  • Hello,

    Try .FontColor = Color.Black

    And note the direct reference in grey in the screenshot. You should try that as otherwise there may be an import or project reference interfering.

    Dim lblLabel As New Label With
            {
                .Location = New Point(370, 20),
                .AutoSize = True,
                .ForeColor = System.Drawing.Color.Black,
                .Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!,
                                                System.Drawing.FontStyle.Bold,
                                                System.Drawing.GraphicsUnit.Point, CType(0, Byte)),
                .Text = "Processing Points Calculations ....."
            }


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites




    Saturday, November 3, 2018 2:36 PM
    Moderator
  • Hi

    I can't reproduce your issue here.

    I used the following and all worked as expected. Tried various fonts etc and all OK.

    Image

    Form1 Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        AddLabel()
      End Sub
    End Class

    Module1 Code

    Module Module1
      Sub AddLabel()
        Dim lblLabel As New Label
        With lblLabel
          .AutoSize = True
          .Location = New Point(20, 40)
          .Text = "Processing Points Calculations ....."
          .Font = New Font("Arial", 18, FontStyle.Bold)
          .ForeColor() = Color.Red
        End With
        Form1.TabControl1.SelectedTab.Controls.Add(lblLabel)
      End Sub
    End Module


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, November 3, 2018 2:46 PM
    Saturday, November 3, 2018 2:45 PM
  • Try this too:

       .Font = New System.Drawing.Font(Me.Font, System.Drawing.FontStyle.Bold)

    assuming that Me is the current form.

    You need to specify the namespace, because you probably have an interfering directive like ‘Imports DocumentFormat.OpenXml.Spreadsheet’.



    • Edited by Viorel_MVP Saturday, November 3, 2018 3:32 PM
    Saturday, November 3, 2018 3:22 PM
  • If I copy the code to a form it works fine but if I execute it from the module I have the issue. The imports on the form are

    Imports System.Data.SqlClient
    Imports System.Globalization
    Imports CrystalDecisions.Windows.Forms

    The imports in the module are

    Imports System.Data.SqlClient
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Runtime.InteropServices
    Imports System.Data.OleDb
    Imports System.Drawing.Drawing2D
    Imports DocumentFormat.OpenXml.Spreadsheet
    Imports SpreadsheetLight

    What I've done is create a new module & moved this code there with "Imports System.Data.SqlClient" as the only imports & now it works. I can only imagine it's the "DocumentFormat.OpenXml.Spreadsheet" import that's causing the issue.

    Saturday, November 3, 2018 3:53 PM
  • Hi Leshay,

    Thanks for responding. I was changing my code to invoke the module from the form (not how I need this to work but as a test to determine the cause) when Karen's mention of the imports made me look at that & subsequently made a new module without all the unnecessary imports for this code only. I did have the exact format you suggested previously before resorting to the forum.

    Saturday, November 3, 2018 3:57 PM
  • Hi Viorel,

    Wish I'd seen your post before as you may see in my response to Karen that I've created a new module because that pesky import you mentioned had to be the one causing me issues.

    Thanks for responding. Without you guys I wouldn't get past some of these issues.

    Saturday, November 3, 2018 4:03 PM
  • Hi

    I believe you are correct regarding the DocumentFormat.OpenXml.Spreadsheet import interfering with things.

    I have managed to get things to work together by fully qualifying the offending code lines.

    This module code works fine.

    Option Strict On
    Option Explicit On
    Imports System.Data.SqlClient
    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    Imports System.Runtime.InteropServices
    Imports System.Data.OleDb
    Imports System.Drawing.Drawing2D
    Imports DocumentFormat.OpenXml.Spreadsheet
    Imports SpreadsheetLight
    
    Module Module1
      Sub AddLabel()
        Dim lab As New Label
        With lab
          .AutoSize = True
          .Location = New Point(20, 40)
          .Text = "Processing Points Calculations ....."
          lab.Font = New System.Drawing.Font("Arial", 18, FontStyle.Bold)
          .ForeColor() = System.Drawing.Color.Red
        End With
        Form1.TabControl1.SelectedTab.Controls.Add(lab)
      End Sub
    End Module


    Regards Les, Livingston, Scotland

    Saturday, November 3, 2018 4:15 PM