Need help with System.Windows.Forms.Timer

B

Bilo

I have a Windows Forms Class MainGUI
I have declared
MainGUI maingui;
public System.ComponentModel.Container components = new Container();
in the Class

I call another class MediaDriver with the Constructor

class MediaDriver
{
....
Form maingui;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.Timer timer1;
....
public MediaDriver(Label label1 , TrackBar trackBar1 , Form maingui)
{
....
this.timer1 = new
System.Windows.Forms.Timer(this.maingui.components);
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
....
}
But i get an error
'System.Windows.Forms.Form' does not contain a definition for 'components'

How can I solve this? How can I add a Timer for the MainGUI form in the
Class MediaDriver?
 
W

Will Pittenger

I believe that class needs a window. It is based on the standard Win32
timers. Try using System.Timers.Timer instead. I may be more familiar with
C++ than C#, but I do not see a window in the code you listed.
 
B

Bilo

The class MainGUI in a windows Form. Have only paste the needed parts.
public class MainGUI : System.Windows.Forms.Form

{
private System.ComponentModel.Container components = null;
Maingui maingui;
MediaDriver mediadriver;
.....
// HERE ARE ALL LABELS; BUTTONS and so on which are in the form
///

than I have somewhere a button event

private void file_button_Click(object sender, System.EventArgs e)
{
if (mediadriver == null)
mediadriver = new MediaDriver(label1, trackBar1, maingui);
maingui.mediadriver.open_media();
}

}

class MediaDriver
{
....
Form maingui;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.Timer timer1;
....
public MediaDriver(Label label1 , TrackBar trackBar1 , Form maingui)
{
....
this.timer1 = new
System.Windows.Forms.Timer(this.maingui.components);
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
....
}

....
//Here comes the other Methods
.....
}

It would work when I declare "MainGUI maingui" instead of "Form maingui" in
Class MediaDriver and change
Constructor to public MediaDriver(Label label1 , TrackBar trackBar1 ,
MainGUI maingui)

But the problem is that Class MediaDriver will be a DLL and someone could
use this DLL from a class
XYZ and than there would be no definition for MainGUI
 
W

Will Pittenger

Then you may need someone better with C# and .NET than I am. All I can
suggest now is putting the timer in the form. Did you attempt to use the
other class? It works whether or not you have a form, container, or
anything else.
 
M

mikeb

Bilo said:
The class MainGUI in a windows Form. Have only paste the needed parts.
public class MainGUI : System.Windows.Forms.Form

{
private System.ComponentModel.Container components = null;
Maingui maingui;
MediaDriver mediadriver;
.....
// HERE ARE ALL LABELS; BUTTONS and so on which are in the form
///

than I have somewhere a button event

private void file_button_Click(object sender, System.EventArgs e)
{
if (mediadriver == null)
mediadriver = new MediaDriver(label1, trackBar1, maingui);
maingui.mediadriver.open_media();
}

}

class MediaDriver
{
....
Form maingui;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.Timer timer1;
....
public MediaDriver(Label label1 , TrackBar trackBar1 , Form maingui)
{
....
this.timer1 = new
System.Windows.Forms.Timer(this.maingui.components);
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
....
}

....
//Here comes the other Methods
....
}

It would work when I declare "MainGUI maingui" instead of "Form maingui" in
Class MediaDriver and change
Constructor to public MediaDriver(Label label1 , TrackBar trackBar1 ,
MainGUI maingui)

But the problem is that Class MediaDriver will be a DLL and someone could
use this DLL from a class
XYZ and than there would be no definition for MainGUI

Since the components field is part of the MainGUI GUI class (and not
Form), what do you want:

System.Windows.Forms.Timer(this.maingui.components);

to do if a non-MainGUI Form is passed into the MediaDriver constructor?

If you want the timer to be skipped if MediaDriver's maingui is not a
MainGUI object, then you could code something like:

MainGUI x = maingui as MainGUI;
if (x != null) {
this.timer1 = new
System.Windows.Forms.Timer( x.components);
this.timer1.Enabled = true;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}

but, I suspect that you need to think through what should really happen
in this case.

Also, unless there's a typo in your example, I don't see how this would
compile anyway, since the MainGUI.components field is private.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top