Reputation: 2920
Here is my code. I dont think there is anything wrong with the code. Sometimes it runs fine but sometimes it crashes unexpectedly.
namespace searchingConsoleWFA
{
public partial class Form1 : Form
{
searchingConsole.Class1 objj = new searchingConsole.Class1();
List<searchingConsole.Class1> obj = new List<searchingConsole.Class1>();
private void button3_Click(object sender, EventArgs e)
{
String compName, tit, conName, phone, fax, addr, pCode, city, reg, cntry;
if (String.IsNullOrEmpty(textBox1.Text) || String.IsNullOrEmpty(textBox2.Text)
|| String.IsNullOrEmpty(textBox3.Text) || String.IsNullOrEmpty(textBox4.Text)
|| String.IsNullOrEmpty(textBox5.Text) || String.IsNullOrEmpty(textBox6.Text)
|| String.IsNullOrEmpty(textBox7.Text) || String.IsNullOrEmpty(textBox8.Text)
|| String.IsNullOrEmpty(textBox9.Text) || String.IsNullOrEmpty(textBox10.Text)
)
{
MessageBox.Show("Please Enter Complete Information", "Invalid");
}
else
{
compName = textBox1.Text;
tit = textBox2.Text;
conName = textBox3.Text;
phone = textBox4.Text;
fax = textBox5.Text;
addr = textBox6.Text;
pCode = textBox7.Text;
city = textBox8.Text;
reg = textBox9.Text;
cntry = textBox10.Text;
obj.Add(new searchingConsole.Class1(compName, tit, conName, phone, fax, addr,
pCode, city, reg, cntry));
MessageBox.Show("Company Added!", "Registered");
}
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
String compName, conName;
compName = textBox11.Text;
conName = textBox12.Text;
if (String.IsNullOrEmpty(textBox11.Text) && String.IsNullOrEmpty(textBox12.Text))
{
MessageBox.Show("Enter Company Name and Contact Name", "Invalid");
}
else if (String.IsNullOrEmpty(textBox11.Text))
{
MessageBox.Show("Enter Company Name", "Invalid");
}
else if (String.IsNullOrEmpty(textBox12.Text))
{
MessageBox.Show("Enter Contact Name", "Invalid");
}
else
{
for (int i = 0; i < obj.Count; i++)
{
if (obj[i].getCompanyName() == compName && obj[i].getContactName() == conName)
{
MessageBox.Show("Match Found", "Search Result");
}
else
{
MessageBox.Show("Match Not Found", "Search Result");
}
textBox1.Clear();
textBox2.Clear();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
Environment.Exit(0);
}
else if (result == DialogResult.No)
{
}
}
}
This is the form I have created. On clicking 'Cancel' the program should exit. The 'OK' button is for searching and the 'Apply' button is for recording the data in some data structure.
The problem is, if I create a record first and then exit then the program exits fine, but if I exit first it crashes. Similarly if some record is searched the program runs fine, but if text fields are left empty then program crashes.
Any Help?
Upvotes: 0
Views: 1244
Reputation: 26
I think on a WPF application you don't use Environment.Exit(0)
, you use Application.Exit()
.
Let's see:
Environment.Exit(0)
-> "Exits this process and gives the underlying operative system the specified exit code". I have allways used this on on Console Applications.Application.Exit()
-> "Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed".
You must use this one on WPF.NOTE: Also instead of those if
and else
i think you could try to do a try catch
block. Use it like this:
try
{
//Code goes here
}
catch(Exception)
{
throw; //Say here what you want the message to show like: Insert a text there
}
Upvotes: 1
Reputation: 1235
use Application.Exit()
instead of Environment.Exit(0)
.
If it still crashes , put your exit code in try catch expression and read the inner exception . i ll show you your problem.
Upvotes: 0