windows forms tabbed pages: start a form on different tabs from the command line

StackOverflow https://stackoverflow.com/questions/8238845

  •  07-03-2021
  •  | 
  •  

Question

I have a windows form i want to start on different tab pages from the command line. This is because the form can be launched by the time/cron service at date/times the user specifies.

How can i get the form application context to go to the non default first tab ?

    [STAThread]
    static void Main (string[] args)
    {
        Debug.WriteLine("Environment Args= {0}", args.Count());
        for (int i=0; i < args.Count(); i++)
        {
            Debug.WriteLine(String.Format("{0}:{1}", i, args[i]));
        }
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Form myForm = new DailyDirectories();
        if (args.Count() > 0)
        {
            Application.Run(myForm.??? );
        }
    }
Was it helpful?

Solution

You may try to pass a parameter to your form class or even set a property.

Form myForm = null;
if (args.Count() > 0)
{
     // in case args[0] contains the start index for your tabControl
     int tabStartIndex = int.Parse(args[0]);
     myForm = new DailyDirectories(tabStartIndex);          
     Application.Run(myForm);
} else // e.g. start form without index

Within your form class you'll have to add/change the source.

class DailyDirectories ...
{
    /// add constructor or change default constructor
    pubic DailyDirectories(int tabStartIndex)
    {
       InitializeComponents();
       this.tabControl.SelectedIndex = tabStartIndex;
    }

}

OTHER TIPS

Since you're talking about tabs, I guess you have a Tabcontrol, so why don't you set the tab page you want in your form's Load() event?

 private void Form_Load(object sender, EventArgs e)
 {
   this.tabControl.SelectedIndex = YourIndex;
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top