Hi,

I have got some code that at startup checks for the existence of a file, if it finds the particular file then it opens another form.

If it doesn't find the particular file then it shows a messagebox asking whether or not to enter some data, answer YES or NO.

When the user hits the NO button it displays a message that the program will now exit but the program does not exit, the StartUpForm appears on the desktop.

Here is the code:-

InitializeComponent();
			
			string path=System.IO.Path.Combine(Application.StartupPath, "SUsettings.ini");
            if (System.IO.File.Exists(path))
            {
	        MainProg form = new MainProg();
			form.ShowDialog();
            }
            
            else
            {
            	DialogResult result = MessageBox.Show("You need to enter your Flickr ID. Do you want to do this now?",
                "Flickr ID Required",
                MessageBoxButtons.YesNo);
            
            if (result == DialogResult.No)
            {
            	MessageBox.Show("The program will now exit");
            	Application.Exit();
            }
            	
            	if (result == DialogResult.Yes)
            	{
            		MessageBox.Show("you want to carry on");
            	}
            }

Why is the Application.Exit() bit not doing it's job?

Kind regards..,

MT ;)

Recommended Answers

All 4 Replies

Debug, because I'm pretty sure it don't enter "if" block //no condition
because DialogResult.No should be DialogResult.Cancel
Please try it..

Debug, because I'm pretty sure it don't enter "if" block //no condition
because DialogResult.No should be DialogResult.Cancel
Please try it..

OK, thanks for replying but I don't really understand what you mean.

I have change the code a little:-

InitializeComponent();
			
			string path=System.IO.Path.Combine(Application.StartupPath, "SUsettings.ini");
            if (System.IO.File.Exists(path))
            {
	        MainProg form = new MainProg();
			form.ShowDialog();
            }
            
            else
            {
            	DialogResult result = MessageBox.Show("You need to enter your Flickr ID. Do you want to do this now?",
                "Flickr ID Required",
                MessageBoxButtons.YesNo);
            
            if (result == DialogResult.No)
            {
            	MessageBox.Show("The program will now exit");
            	this.Close();
            }
            	
            	if (result == DialogResult.Yes)
            	{
            		MessageBox.Show("you want to carry on");
            	}
            }

you see I have taken out the Application.Exit() & changed it for this.Close().

This stops the StartUpForm from being shown but throws an exception in the Program.cs.

Cannot access a Diposed Object:-

Application.Run(new StartUpForm());

I am guessing the program is trying to still run.

Regards..,

MT

Hi,

I have figured it out, I moved the code from the InitializeComponent section and double clicked on the form to get the FormLoad section.

void StartUpFormLoad(object sender, EventArgs e)
		{
			string path=System.IO.Path.Combine(Application.StartupPath, "SUsettings.ini");
            if (System.IO.File.Exists(path))
            {
	        MainProg form = new MainProg();
			form.ShowDialog();
            }
            
            else
            {
            	DialogResult result = MessageBox.Show("You need to enter your Flickr ID. Do you want to do this now?",
                "Flickr ID Required",
                MessageBoxButtons.YesNo);
            
            if (result == DialogResult.No)
            {
            	MessageBox.Show("The program will now exit");
            	this.Dispose();
            }
            	
            	if (result == DialogResult.Yes)
            	{
            		MessageBox.Show("you want to carry on");
            	}
            }
			
		}

It is now all working.

Regards..,

MT ;)

put breakpoint at if (result == DialogResult.No) see if it reaches inside it or not

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.