Reputation: 867
I need to only allow user whose role is "Admin" to exit application.I use following code for this purpose but after delivering message application is terminating.
private void Login_FormClosed(object sender, FormClosedEventArgs e)
{
if (Login.role != "Admin")
{
MessageBox.Show("You are not authorized to Exit Application.");
}
else
{
Application.Exit();
}
}
i also used this on FormClosing event.but not get proper functioning. i am using c# windows application.
Upvotes: 1
Views: 187
Reputation: 455
Use the FormClosing event and try the following code:
private void Login_FormClosing(object sender, FormClosingEventArgs e)
{
if (Login.role != "Admin")
{
MessageBox.Show("You are not authorized to Exit Application.");
e.Cancel = true;
}
else
{
Application.Exit();
}
}
Upvotes: 4
Reputation: 43264
You need to add:
e.Cancel = true;
to the if (Login.role != "Admin")
section.
You'll need to add this to the FormClosing
event handler, rather than the FormClosed
event handler though.
Upvotes: 3
Reputation: 3797
Add FormClosing
event handler and put the following code inside it:
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = Login.role != "Admin";
}
Upvotes: 1
Reputation: 354
The FormClosed event is too late. Use the FormClosing event! There you have e.Cancel
, which helps you to cancel the closing process.
Upvotes: 2