Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

i am stuck in a part to bypass the main form that loads when the program on run.ie. if some codition is satisfied then the main form should be closed and other selected form should run
my code is :
C#
private void frmEntrySecretKey_Load(object sender, EventArgs e)
      {

  string sk = BllLoginHelper.checkSecretKeys();
            if (sk == null)
            {
                frmLogin frmlogin = new frmLogin();
                frmlogin.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("there is A KEY");
            }

      }


but it did not worked
but i tried to put after a button click event
the same code worked
can any one help me with this..
Posted

Well Load event occurs before a form is displayed for the first time.

As there are other Form events that may be useful, try to check this condition before the frmEntrySecretKey.Show() method is called. If this is your "first form" check the Program.cs and modify it in a proper way. If there is something that you are not clear, improve this question.

Some references:

Form events http://msdn.microsoft.com/en-us/library/td1s43eb.aspx[^]

UPDATE:

Based on your comment, this is what you should try.

Open a file in your project called Program.cs

Inside you should find something like

XML
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmEntrySecretKey());
    }
}


And try to change it in

C#
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        string sk = BllLoginHelper.checkSecretKeys();
        if (sk == null)
        {
            Application.Run(new frmLogin());
        }
        else
        {
            Application.Run(new frmEntrySecretKey());
        }
    }
}


It is not an elegant solution but it will do your job. In this way you will be able to proceed and then in the future search for a more elegant solution.

Cheers!
 
Share this answer
 
v2
Comments
amsainju 22-Oct-11 9:05am    
frmEntrySecretKey is my first form..

i tried doing in this way but it also didn;t helped

public partial class frmEntrySecretKey : Form
{
public frmEntrySecretKey()
{
InitializeComponent();
initialevent();

}
public void initialevent()
{
string sk = BllLoginHelper.checkSecretKeys();
if (sk == null)
{
frmLogin frmlogin = new frmLogin();
frmlogin.Show();
this.Hide();
}
else
{
MessageBox.Show("there is A KEY");
}

}
Mario Majčica 22-Oct-11 9:12am    
Check the update.
amsainju 22-Oct-11 9:18am    
thanks i never thought to make change in program.cs thank u very much now i will try it
Mario Majčica 22-Oct-11 9:19am    
Let us know if worked!

Cheers!
Using a preprocessor #if directive might help you out - see here[^] for more documentation on this directive.
 
Share this answer
 
Comments
amsainju 22-Oct-11 9:11am    
sorry i didn't understand u. i have never tried #if...i tried it but it produced more problems

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900