Cannot access a disposed object – How to fix?

Try checking the IsDisposed property before accessing the control. You can also check it on the FormClosing event, assuming you’re using the FormClosed event. We do stop the Timer on the FormClosing event and we do check the IsDisposed property on the schedule component before using it in the Timer Tick event but it doesn’t help. Calling GC.Collect before … Read more

Cannot access a disposed object – How to fix?

Try checking the IsDisposed property before accessing the control. You can also check it on the FormClosing event, assuming you’re using the FormClosed event. We do stop the Timer on the FormClosing event and we do check the IsDisposed property on the schedule component before using it in the Timer Tick event but it doesn’t help. Calling GC.Collect before … Read more

How to properly exit a C# application?

From MSDN: Application.Exit Informs all message pumps that they must terminate, and then closes all application windows after the messages have been processed. This is the code to use if you are have called Application.Run (WinForms applications), this method stops all running message loops on all threads and closes all windows of the application. Environment.Exit … Read more

How to open a new form from another form

In my opinion the main form should be responsible for opening both child form. Here is some pseudo that explains what I would do: You will just need to create a simple event MoreClick in the first child. The main benefit of this approach is that you can replicate it as needed and you can … Read more

Can’t use System.Windows.Forms

A console application does not automatically add a reference to System.Windows.Forms.dll. Right-click your project in Solution Explorer and select Add reference… and then find System.Windows.Forms and add it.

Get current folder path

You should not use Directory.GetCurrentDirectory() in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut. It’s better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); for your purpose. This returns the pathname where the currently executing assembly resides. While my suggested approach allows you to differentiate between the executing assembly, the entry assembly or … Read more