How to remember last closed form

ankit goel 766 Reputation points
2023-10-01T09:59:34.1566667+00:00

Hi ,

Working on a application , where i have two pages in my application : sale and purchase . I need to find out on which form i was working before closing the application , so that it can be the starting form of my application at run time . For example , if i was working on sale page and then i closed the application , then on next start of my application , sales should be opened first .

            if () // if last page closed  was sale 
            {
                this.BackColor = Color.FromArgb(227, 220, 192);
                this.VoucherType.Text = "Sales";
                this.label1.Text = "Reference no.:";
                label2.Hide();
                textBox2.Hide();

            }
            else if () // if last page closed was purchase 
            {
                this.BackColor = Color.FromArgb(250, 43, 198);
                this.VoucherType.Text = "Purchase";
                this.label1.Text = "Supplier Invoice no.:";
                label2.Show();
                textBox2.Show();

            }
Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

Answer accepted by question author
  1. KOZ6.0 6,735 Reputation points
    2023-10-01T11:04:29.9766667+00:00
    1. Create a field to record the last closed formenter image description here
    2. Create class that inherits ApplicationContext to save the LastForm
    class SaveLastFormContext : ApplicationContext
    {
        public SaveLastFormContext(Form firstForm) : base(firstForm) { }
    
        protected override void OnMainFormClosed(object sender, EventArgs e) {
            foreach (Form form in Application.OpenForms) {
                if (form.TopLevel && !form.InvokeRequired) {
                    MainForm = form;
                    return;
                }
            }
            // Save LastForm TypeName
            var settings = MyApp.Properties.Settings.Default;
            settings.LastForm = MainForm.GetType().Name;
            settings.Save();
            base.OnMainFormClosed(sender, e);
        }
    }
    
    1. Main as follows
    namespace MyApp
    {
        internal static class Program
        {
            [STAThread]
            static void Main() {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                // choise FirstForm
                var settings = MyApp.Properties.Settings.Default;
                string lastForm = settings.LastForm;
                Form firstForm;
                switch (lastForm) {
                    case "Sale":
                        firstForm = new Sale();
                        break;
                    default:
                        firstForm = new Purchase();
                        break;
                }
                Application.Run(new SaveLastFormContext(firstForm));
            }
        }
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.