Link to home
Start Free TrialLog in
Avatar of Ahmet Ekrem SABAN
Ahmet Ekrem SABANFlag for Austria

asked on

How to create an event out of a class that is caught by other forms?

Hello!

I am new to C# and .NET. My question is simple to formulate, but I do not know whether there is a simple answer. I have created the GUI for a WinForms project, where there are a number of forms that all derive from a class BaseForm : Form. This class is only used to derive other classes from it, although it is not an abstract class - wanted to open it in the MVS2010 design editor, which is not possible for abstract classes...

The BaseForm does have a singleton class called ButtonMenu in it. My question is about the interaction over event handling between the ButtonMenu and all BaseForm-derived forms.

Question
When the "language" button is pressed in the ButtonMenu, all the texts of the buttons are switched from one language to the next. How can I inform the active form/all the forms that this button was clicked?

What I want to do is to change the text content in the active form. Note that the class ButtonMenu is a Control and not related to any Form class.

Thank you for your reply.
Avatar of Umar Topia
Umar Topia
Flag of India image

You can use event delegates to achieve this functionality.

You can define a public event in the base class and then invoke that event on certian conditions.

That way you can register to the publicly exposed events from other forms.
Avatar of Ahmet Ekrem SABAN

ASKER

That is both the BaseForm and the ButtonMenu have to be derived from some base class. Am I right?

Currently, the ButtonMenu is a Control, and the BaseForm is a Form. How can I derive both from one class and keep their property of being a Control and a Form? I do not know how to do it with single inheritance.
you can make a public event, and call it when the button is clicked, after checking the parent of the button if its that base form class or not.


object parent = button1.Parent;
            while (parent != null)
            {
                if (parent is Form)
                {

                    break;
                }
                parent = ((Control)parent).Parent;
            }
            if (parent != null)
                ((MyBaseClass)parent).ChangeLanguage(Languages.DE);

Open in new window

Type error:
just change
if (parent is Form)
to
 if (parent is MyBaseClass)
sorry, i have rewritten the code:
object parent = button1.Parent;
            while (parent != null)
            {
                if (parent is MyBaseClass)
                {
                    ((MyBaseClass)parent).ChangeLanguage(Languages.DE);
                    break;
                }
                parent = ((Control)parent).Parent;
            }

Open in new window

But the base class (parent) of the ButtonMenu is Control. BaseForm is the parent of the forms I use, but it is not related directly to ButtonMenu.
ASKER CERTIFIED SOLUTION
Avatar of Amro Osama
Amro Osama
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you!
you are welcome :)