Hey, I have the code as follows:

public Form1()
{
    InitializeComponent();
    string[] _args = Environment.GetCommandLineArgs();
    if (_args.Contains("options"))
    {
        this.Show();
    }
}

This will be a tray app (and close quickly after the work is done), so if I add a notifyicon above the InitializeComponent(), could I move that just above the this.Show()?
As it will not be needed most of the time, it seems a waste to load the items.
The form does not show when the program runs- I stopped this happening in the program.cs.

Recommended Answers

All 7 Replies

You can add stuff which is not InitializeComponent(); to Form1_Load.

public Form1()
{
   InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
    string[] _args = Environment.GetCommandLineArgs();
    if (_args.Contains("options"))
    {
        this.Show();
    }
}

You can add stuff which is not InitializeComponent(); to Form1_Load.

public Form1()
{
   InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
    string[] _args = Environment.GetCommandLineArgs();
    if (_args.Contains("options"))
    {
        this.Show();
    }
}

Sorry, I know this; I was asking if the InitializeComponent() is needed if you are not showing the form?

If I am understanding you correctly, you only want to load the form if the command line arguments contain "options". If this is the case, then your best bet is going to be to take your command line arguments check out of Form1 completely and instead put it in the Program.cs file like this:

namespace WindowsFormsApplication1
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      if (Environment.GetCommandLineArgs().Contains("options"))
      {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
      }
    }
  }
}

If I am understanding you correctly, you only want to load the form if the command line arguments contain "options". If this is the case, then your best bet is going to be to take your command line arguments check out of Form1 completely and instead put it in the Program.cs file like this:

namespace WindowsFormsApplication1
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      if (Environment.GetCommandLineArgs().Contains("options"))
      {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
      }
    }
  }
}

Thanks for the reply. What I want is for the form not to load the form if it is not the options; the form is the options page. I will add a notify icon below the InitializeComponont(), so that will always be drawn. This application is designed to run on start up, do some work, and then close. You will be able to run it from the start menu with the "options" argument (there will also be one without the options argument, just to make it do the work).

Then you should just be able to expand on my previous post, and do the work and add the notifyicon before you do the check for the command line args. This way you are not loading the form into memory if it isn't needed, but you are still able to do the work and add the icon. My suggestion would be to crceate a seperate class file that contains all the code to do whatever work needs to be done in a method. Then you can create a new instance of it whenever it's needed to do work, in the main method or within the form.

Here is a rough sample...

public class HelperClass
{
  public HelperClass(){}

  public void DoWork()
  {
    //do work
  }
}

static void Main()
{
  HelperClass helper = new HelperClass();
  helper.DoWork();

  //add notify icon here

  if (Environment.GetCommandLineArgs().Contains("options"))
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
  }
}

Then you should just be able to expand on my previous post, and do the work and add the notifyicon before you do the check for the command line args. This way you are not loading the form into memory if it isn't needed, but you are still able to do the work and add the icon. My suggestion would be to crceate a seperate class file that contains all the code to do whatever work needs to be done in a method. Then you can create a new instance of it whenever it's needed to do work, in the main method or within the form.

Here is a rough sample...

public class HelperClass
{
  public HelperClass(){}

  public void DoWork()
  {
    //do work
  }
}

static void Main()
{
  HelperClass helper = new HelperClass();
  helper.DoWork();

  //add notify icon here

  if (Environment.GetCommandLineArgs().Contains("options"))
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new Form1());
  }
}

Cheers for the help. It's solved now.

Glad I could help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.