We help IT Professionals succeed at work.

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

Ahmet Ekrem SABAN
on
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.
Comment
Watch Question

Umar Topia.Net Full Stack Developer
CERTIFIED EXPERT

Commented:
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.
Ahmet Ekrem SABANSenior IT consultant

Author

Commented:
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.
Amro OsamaDevelopment, GIS Consultant - Haram Presidency
CERTIFIED EXPERT

Commented:
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

Amro OsamaDevelopment, GIS Consultant - Haram Presidency
CERTIFIED EXPERT

Commented:
Type error:
just change
if (parent is Form)
to
 if (parent is MyBaseClass)
Amro OsamaDevelopment, GIS Consultant - Haram Presidency
CERTIFIED EXPERT

Commented:
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

Ahmet Ekrem SABANSenior IT consultant

Author

Commented:
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.
Development, GIS Consultant - Haram Presidency
CERTIFIED EXPERT
Commented:
no problem, the while loop will loop untill the parent is either null or that the parent is the baseform you are looking for.

if you mean that you want to change all existing forms, then you should have that event in the main form, and inside that event, loop through the controls and each child form call the translate function.
Ahmet Ekrem SABANSenior IT consultant

Author

Commented:
Thank you!
Amro OsamaDevelopment, GIS Consultant - Haram Presidency
CERTIFIED EXPERT

Commented:
you are welcome :)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.