Tiffany
Tiffany

Reputation: 519

C# form class event

So I have two classes "SplashScreenForm" and "Program" and on event want to run "MainForm". It looks like this:

public partial class SplashScreenForm : Form
{
    public event EventHandler<int> RaiseUpdateEvent;
    ........

and in "Program" defining class like this

static class Program
{
    static Form SplashScreen;
    static Form MainForm;

    SplashScreen = new SplashScreenForm();
    .........

but problem is that when I'm trying to access event it says that "SplashScreenForm" doesn't contain that event. How can I solve this problem?

SplashScreen.RaiseUpdateEvent += SplashScreen_RaiseUpdateEvent;

Error says: CS1061 C# 'Form' does not contain a definition for and no accessible extension method accepting a first argument of type 'Form' could be found (are you missing a using directive or an assembly reference?)

enter image description here

Upvotes: 1

Views: 114

Answers (1)

Poul Bak
Poul Bak

Reputation: 10940

The error says, that the Class called Form desn't have an event called RaiseUpdateEvent

Only the class SplashScreenForm have that event.

So you should define your field to be of type SplashScreenForm:

static SplashScreenForm SplashScreen;

More advanced alternative:

If for some reason you want your variable SplashScreen to be of type Form (may be you get the type from a library you don't control), you can actually still use it, because you know it's a SplashScreenForm when you call new SplashScreenForm().

In that case you can Cast it to your type like this:

((SplashScreenForm)SplashScreen).RaiseUpdateEvent += SplashScreen_RaiseUpdateEvent;

Now you're telling the compiler that although it's declared as Form, it's really a SplashScreenForm and can be used as that - meaning the cast type do have a RaiseUpdateEvent event.

Upvotes: 2

Related Questions