In my Windows Forms project I have a BindingNavigator control in a user control (It is a data-binding scenario). Pressing one of the buttons, First, Previous, Next or Last throws exception, crashing the application entirely. To tackle this I have written following code to handle the exception, in the user control.
protected override void OnLoad(EventArgs e)
{
Application.ThreadException += Application_ThreadException;
base.OnLoad(e);
}
private void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
if (bindingNavigatorMovePreviousItem.Pressed)
{
MessageBox.Show(e.Exception.Message);
bsRestrictions.CancelEdit();
}
}
How do I know if the exception thrown is the result of ToolStripButton click event?
The code above is helpless as Pressed property is false in the method, so the message box will not pop out.
Following is from my Program.cs file
[STAThread]
static void Main()
{
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new AppForm());
}
Experienced developers must have understood that I am trying to solve the well known issue with BindingNavigator. This control simply manages BindingSource component. In scenarios where strongly typed data sets are used, navigating through records throws exception crashing the application.

Just debug your application. If you have an idea where the exception is thrown, put a breakpoint there. When the exception is thrown, you can go back in the call stack. In the call stack, you should be able to see where the exception came from. From the code that you are providing, seems hard to help more.