I have a piece of code like the one below. It gets a string from somewhere, checks it and if string is empty it should display the message and exit the program. The problem is that it doesn't exit but continues. I can not figure out what I'm doing wrong.
public Form1()
{
InitializeComponent();
string str=getString();
if (str=="")
{
MessageBox.Show("string is empty")
Application.Exit();
}
.....
.....
}
Vladimir OtaPosted Sep 26, 2008, 3:05 AM
I figured that I have to move the code away from the constructor just before I saw your suggestion. But problem persisted. Code after the 'if' statement block still got executed. Then I modified whole thing like below and now it works as expected. Seems to me that I have wrong understanding how exactly the Application.Exit() works. I checked this in MS docs, but I can't say I understand it any better.
Thanks for help anyway!
private void Form1_Load(object sender, EventArgs e)
{
string str = getString();
if (str == "")
{
MessageBox.Show("string is empty");
Application.Exit();
}
else
{
....
....
}
}
Blocked AccountPosted Sep 26, 2008, 2:29 AM
Hi,You had written that code inside the constructor. Write that code inside the page load then it will work fine.
private void Form1_Load(object sender, EventArgs e)
{
string str = getString();
if (str == "")
{
MessageBox.Show("string is empty");
Application.Exit();
}
}Happy Coding!!