CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2008
    Posts
    27

    Parameter is not valid

    I am developing an application in which this error occurs some time when my program is running....
    This error occurs sometime not redundant..
    I have paste the detail of error below:

    System.ArgumentException was unhandled
    Message="Parameter is not valid."
    Source="System.Drawing"
    StackTrace:
    at System.Drawing.FontFamily.GetName(Int32 language)
    at System.Drawing.FontFamily.get_Name()
    at System.Windows.Forms.Internal.WindowsFont.FromFont(Font font, WindowsFontQuality fontQuality)
    at System.Windows.Forms.Internal.WindowsGraphicsCacheManager.GetWindowsFont(Font font, WindowsFontQuality fontQuality)
    at System.Windows.Forms.TextRenderer.DrawText(IDeviceContext dc, String text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags)
    at System.Windows.Forms.Label.OnPaint(PaintEventArgs e)
    at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
    at System.Windows.Forms.Control.WmPaint(Message& m)
    at System.Windows.Forms.Control.WndProc(Message& m)
    at System.Windows.Forms.Label.WndProc(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
    at System.Windows.Forms.Application.Run(Form mainForm)
    at BarcodeLabelPrinter.Program.Main() in E:\BarcodeLabelPrinter\BarcodeLabelPrinter\Program.cs:line 17
    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Parameter is not valid

    Can you put the code that is causing this? The exception message gives you a hint...
    at BarcodeLabelPrinter.Program.Main() in E:\BarcodeLabelPrinter\BarcodeLabelPrinter\Program.cs:line 17
    You don't need to put the whole code file just the method/function or part of the function. Particularly close to the line 17 in your Program.cs. And needless to say...use the code tags

  3. #3
    Join Date
    Mar 2008
    Posts
    27

    Re: Parameter is not valid

    Quote Originally Posted by nelo
    Can you put the code that is causing this? The exception message gives you a hint...

    You don't need to put the whole code file just the method/function or part of the function. Particularly close to the line 17 in your Program.cs. And needless to say...use the code tags

    LINE WHERE THIS ERROR OCCURS

    Application.Run(new MainForm());

    THIS IS LINE 17 OF PROGRAM.CS

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Parameter is not valid

    I am developing an application in which this error occurs some time when my program is running....
    This error occurs sometime not redundant..
    Ok. Maybe we need to understand the problem better. Do you get the exception everytime you run the program? What do you have in your MainForm?

  5. #5
    Join Date
    Mar 2008
    Posts
    27

    Re: Parameter is not valid

    Quote Originally Posted by nelo
    Ok. Maybe we need to understand the problem better. Do you get the exception everytime you run the program? What do you have in your MainForm?

    NO I DID NOT GET THIS PROBLEM EVERY TIME WHEN I RUN MY APPLICATION IT COMES SOME TIME WHEN MY APPLICATION IS RUNNING. WHETHER I AM DOING CERTAIN TASK WITH MY APPLICATION OR NOT....

    IN THIS APPLICATION I AM ADDING FONTS TO MY APPLICATION I AM SENDING CODE THAT I HAVE USED TO ADD FONTS

    class PrivateFonts
    {
    [DllImport("Gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
    private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, int cbFont, int pdv, ref int pcFonts);
    public System.Drawing.Text.PrivateFontCollection GetFont(string[] FontResource)
    {
    //Get the namespace of the application
    string NameSpc = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
    System.IO.Stream FntStrm;
    System.Drawing.Text.PrivateFontCollection FntNc = new System.Drawing.Text.PrivateFontCollection();
    int i;
    for (i = 0; i <= FontResource.GetUpperBound(0); i++)
    {
    //Get the resource stream area where the font is located
    FntStrm = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(NameSpc + "." + FontResource[i]);
    //Load the font off the stream into a byte array
    byte[] ByteStrm = new byte[(int)FntStrm.Length];
    FntStrm.Read(ByteStrm, 0, Convert.ToInt32((int)FntStrm.Length));
    //Allocate some memory on the global heap
    IntPtr FntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(System.Runtime.InteropServices.Marshal.SizeOf(typeof(byte)) * ByteStrm.Length);
    //Copy the byte array holding the font into the allocated memory.
    System.Runtime.InteropServices.Marshal.Copy(ByteStrm, 0, FntPtr, ByteStrm.Length);
    //Add the font to the PrivateFontCollection
    FntNc.AddMemoryFont(FntPtr, ByteStrm.Length);
    Int32 pcFonts;
    pcFonts = 1;
    AddFontMemResourceEx(FntPtr, (int)ByteStrm.Length, 0, ref pcFonts);
    //Free the memory
    System.Runtime.InteropServices.Marshal.FreeHGlobal(FntPtr);
    }
    return FntNc;
    }
    }
    ------------------------------------------------------------------------------------------------
    ON BUTTON CLICK
    string[] fontNames = { "IDAutomationXC39M.ttf" };
    PrivateFonts privateFonts = new PrivateFonts();
    System.Drawing.Text.PrivateFontCollection FntNc = privateFonts.GetFont(fontNames);
    labelBcode.Font = new System.Drawing.Font(FntNc.Families[0], 12);

  6. #6
    Join Date
    Jun 2007
    Posts
    16

    Re: Parameter is not valid

    You are getting that error because MainForm() is not an ApplicationContext nor is it a Form.

    I suspect you are trying to do one of two things.

    either
    1. start a new process
    2. open up a new form

    to start a new process use
    System.Diagnostics.Process.Start(fileToStart);

    for opening a new form you can either open it is a dialog or as just a new form
    that would be

    this.Show(theForm);

    or

    this.ShowDialog(theForm);

  7. #7
    Join Date
    Mar 2008
    Posts
    27

    Re: Parameter is not valid

    Quote Originally Posted by joe1985
    You are getting that error because MainForm() is not an ApplicationContext nor is it a Form.

    I suspect you are trying to do one of two things.

    either
    1. start a new process
    2. open up a new form

    to start a new process use
    System.Diagnostics.Process.Start(fileToStart);

    for opening a new form you can either open it is a dialog or as just a new form
    that would be

    this.Show(theForm);

    or

    this.ShowDialog(theForm);

    sir i did not understand it please can u explain it......
    i am not using any other form except this one......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured