Chris
Chris

Reputation: 7621

Accessing form via another class

Forgive me if this is a bit garbled, I'm a bit new on Windows Forms, having spent months in ASP.NET

Basically, I am using Quartz.NET in my Windows Form application - when a job is executed, it fires another class file - the parameters it passes in do not contain a reference to the form, and I don't think I can change this.

What I want to do is refresh a grid on the page after the job executes - and the only place that 'tells' me a job has been executed are in other files, rather than the forms code. I can't figure out a way of accessing methods/objects on the form without starting a new instance of it, which I don't want to do.

EDIT: To sum up, I just want a way to sent a message or something to the already open Main form from another class

Upvotes: 0

Views: 502

Answers (3)

Rebecca Scott
Rebecca Scott

Reputation: 2431

The easiest way is to pass the instance of the main form to the class consuming the Quartz.NET event, so that the consuming class can then call methods on the main form. I'm guessing that class would be created in the main form somewhere anyway, so it would be something like:

var quartzConsumer = new QuartzConsumer(this);

...
class QuartzConsumer {
    MainForm _form;
    public QuartzConsumer(MainForm form) {
        _form = form;
        ...
    }
    void OnTimer(..) {
        _form.UpdateGrid();
    }
}

EDIT as @hundryMind says, another solution is for the main form to subscribe to an event on the consuming class:

class QuartzConsumer {
    public delegate void DataChangedEventHandler();
    public event DataChangedEventHandler DataChanged;
    void OnTimer(..) {
        if (this.DataChanged != null) this.DataChanged();
    }
}

// in MainForm:
var quartzConsumer = new QuartzConsumer(..);
quartzConsumer.DataChanged += this.OnDataChanged;
...
void OnDataChanged() {
    // update the grid
}

Upvotes: 0

hungryMind
hungryMind

Reputation: 7009

Why not raise event from your class to winform. Thats the elegant way to do this. To do send message, you can use interop to call sendMessage which requires handle of the window

Upvotes: 1

Akram Shahda
Akram Shahda

Reputation: 14781

Actualy, if members of a class were not static, you wont be able to access them without an instance of that class. Try to accuire the same instance of the class that your actions are applied on it.

Upvotes: 0

Related Questions