Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 Forms form
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>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            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()
        {
            InitializeComponent();
            //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);
                Marshal.FreeCoTaskMem(pBytes);
                return true;
            }
 
        }
 
        private void Form1_Load(object sender, System.EventArgs e)//This part works!!
        {
            ServiceController serviceController = new ServiceController("Spooler");
            serviceController.Stop();
            Thread.Sleep(7000);
            serviceController.Start();
            Thread.Sleep(3000);
            Thread.Sleep(3000);
            foreach (string path in Directory.GetFiles("C:\\Windows\\System32\\spool\\PRINTERS"))
                File.Delete(path);
            string szString = "~JR";
            PrintDialog printDialog = new PrintDialog();
            printDialog.PrinterSettings = new PrinterSettings();
            printDialog.PrinterSettings.PrinterName = "Label Printer";
            Form1.RawPrinterHelper.SendStringToPrinter(printDialog.PrinterSettings.PrinterName, szString);
            Environment.Exit(1);
 
        }
 
        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");
            controller.Stop();
 

 
            //Start Print Spooler Service then wait 3 seconds
            controller.Start();
            //Thread.Sleep(3000);

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

            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);
 

 
            //Environment.Exit(1);
        }
    }
}
 
 
I'm sure this is something simple, I just can't figure it out! Could someone help?
Posted 6-Dec-12 7:10am
Comments
Dustin Prevatt at 6-Dec-12 12:11pm
   
Sorry for the long code..
ridoy at 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
good
Please Sign up or sign in to vote.

Solution 1

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

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



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 6 Dec 2012
Copyright © CodeProject, 1999-2014
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