[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

How to tell Form1 that Form2's button is pressed via Custom Delegate?

A class "Control" composes a Form1's and Form2's instances. All of them are under a same namespace. There is no other relation b/w Form1 and Form2.

Form2 have two buttons Previous and Finish. How can I manage configure delegate :
public delegate void TogglingOnForm2Closing(object sender, FormClosingEventArgs e);
in Control or Form1 class in such a way that when Previous is pressed Form2 is closed and Form1 is shown.
0
A-BM
Asked:
A-BM
  • 3
  • 2
2 Solutions
 
CB_ThirumalaiCommented:
What is the purpose of this?  Is not there a simplest way to do?  If you only have two forms, then set that to go to form 1 when clicking on previous in form 2.
0
 
A-BMAuthor Commented:
No, it ain't. As I stated in my prior post, there is no such relation b/w these classes by means of which the instance of Form2, created by Control class, could be accessed by Form1 and vice versa.
0
 
AndreSteffensCommented:
You do not need to make a custom delegate for this. Simply let the Control class subscribe to the Click event of the Previous button. The only trick that is needed is to make the Previous button internal in form2
    class Control
    {
        private Form1 form1;
        private Form2 form2;
 
        public Control()
        {
            form1=new Form1();
            form2=new Form2();
            form2.btnPrevious.Click += new EventHandler(btnPrevious_Click);
        }
 
        public void ShowForm2()
        {
            form2.Show();
        }
 
        void btnPrevious_Click(object sender, EventArgs e)
        {
            form2.Close();
            form1.Show();
        }

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
A-BMAuthor Commented:
Thanks AndreSteffens,
One reason for the custom delegate is, the perviousButton is defined private. Although, making it public implies no harm, could you please deliberately use the customdelegate in the very scenario, just to make it knowladgebase?
0
 
AndreSteffensCommented:
Sure.

Define an Action delegate in Form 2 and call it when the user clicks the Previous button,

Hook up the method that closes form2 and opens form 1 in the Control class.
   public partial class Form2 : Form
    {
        public Action Form2WantsClosing=delegate{};//added to prevent nullreference exceptions
 
        public Form2()
        {
            InitializeComponent();
        }
 
        private void btnPrevious_Click(object sender, EventArgs e)
        {
            Form2WantsClosing();
        }
    }
 
    class Control
    {
        private Form1 form1;
        private Form2 form2;
 
        public Control()
        {
            form1=new Form1();
            form2=new Form2();
            form2.Form2WantsClosing += CloseForm2OpenForm1;
        }
 
        public void ShowForm2()
        {
            form2.Show();
        }
 
        void CloseForm2OpenForm1()
        {
            form2.Close();
            form1.Show();
        }
 
    }

Open in new window

0
 
A-BMAuthor Commented:
Thanks, that worked for me!
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now