Click here to Skip to main content
13,549,853 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a windows form that at one point needed to run but never show the form but now I am needing to show the form and I can't figure out how to make the form show again. I think it has something to do with the following code. Here is my code for program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace test_app
    static class Program
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
            Application.Run(new Form1());

And here is my code from form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Windows;
using System.Security.Permissions;
using System.Threading;
using System.ServiceProcess;

//working but not showing form
namespace test_app
    public partial class Form1 : Form
        public Form1()
            //this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.ShowInTaskbar = false;
            this.Load += new EventHandler(Form1_Load);

                   public static bool SendStringToPrinter(string szPrinterName, string szString)
                IntPtr pBytes;
                Int32 dwCount;
                //How many characters are in the string?
                dwCount = szString.Length;
                //Assume that the printer is expecting ANSI text, and then convert the string to ANSI text.
                pBytes = Marshal.StringToCoTaskMemAnsi(szString);
                //Send the converted ANSI string to the printer.
                SendBytesToPrinter(szPrinterName, pBytes, dwCount);
                return true;


        private void Form1_Load(object sender, System.EventArgs e)//This part works!!
            ServiceController serviceController = new ServiceController("Spooler");
            foreach (string path in Directory.GetFiles("C:\\Windows\\System32\\spool\\PRINTERS"))
            string szString = "~JR";
            PrintDialog printDialog = new PrintDialog();
            printDialog.PrinterSettings = new PrinterSettings();
            printDialog.PrinterSettings.PrinterName = "Label Printer";
            Form1.RawPrinterHelper.SendStringToPrinter(printDialog.PrinterSettings.PrinterName, szString);


        private void button1_Click(object sender, EventArgs e)//does the same as form_load

            //Stop Print Spooler Service then wait 3 seconds
            ServiceController controller = new ServiceController("Spooler");

            //Start Print Spooler Service then wait 3 seconds

            //Delete Print Jobs then wait 3 seconds
            string[] filePaths = Directory.GetFiles(@"C:\Windows\System32\spool\PRINTERS");
            foreach (string filePath in filePaths)

            string s = "~JR"; // device-dependent string, need a FormFeed?

            // Allow the user to select a printer.
            PrintDialog pd = new PrintDialog();
            pd.PrinterSettings = new PrinterSettings();
            pd.PrinterSettings.PrinterName = "Label Printer";
            // if (DialogResult.OK == pd.ShowDialog(this))
            // Send a printer-specific to the printer.
            RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s);


I'm sure this is something simple, I just can't figure it out! Could someone help?
Posted 6-Dec-12 6:10am
Dustin Prevatt 6-Dec-12 12:11pm
Sorry for the long code..
ridoy 6-Dec-12 12:29pm
not too much sure why you use these 2 lines in Form1() constructor..
this.ShowInTaskbar = false;
this.Load += new EventHandler(Form1_Load);
You necessarily need not call an eventhandler for working Form1_Load.Omit these 2 lines from there and see what happens..

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

I would recommend not calling Environment.Exit in the Load handler!


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03-2016 | 2.8.180515.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100