By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,668 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,668 IT Pros & Developers. It's quick & easy.

Trying to create a dynamic class...?

P: n/a
Hey everyone. This is only my first week with C#, but I use actionscript a
lot in Flash, so I have a very basic understanding of programming, but bear
with me...

I am trying to create a "FadeClass" that I can use to fade windows in my
programs. I am using Visual C# 2008 Express right now.

This is my "Form1.cs" in it's current state:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class IntroBox : Form
{
public IntroBox()
{
InitializeComponent();
faderClass(this, "out", 2000);
System.Timers.Timer fadeTimer = new System.Timers.Timer();
fadeTimer.Elapsed += new ElapsedEventHandler(OnTimer);
fadeTimer.Interval = 50;
fadeTimer.Enabled = true;
fadeTimer.SynchronizingObject = this;
fadeTimer.Start();
}

private void OnTimer(Object source, ElapsedEventArgs e)
{
this.Opacity += 0.1;
}
}
}

That works. The program loads up and the first window fades in perfectly.
Here is my "faderClass.cs" which is what I'm trying to get working. I'm
trying to understand how to make the class know which window it needs to fade
(in Flash it was very easy). You can see what I've tried in the code, but to
no avail:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;

namespace WindowsFormsApplication1
{
class faderClass
{
public void Main(****** target, String inOut, int fadeTime)
{
System.Timers.Timer fadeTimer = new System.Timers.Timer();
fadeTimer.Elapsed += new ElapsedEventHandler(OnTimer);
fadeTimer.Interval = 50;
fadeTimer.Enabled = true;
fadeTimer.SynchronizingObject = target;
fadeTimer.Start();
}
private void OnTimer(object source, ElapsedEventArgs e)
{
target.Opacity += 0.1;
}
}
}

As you can see, I'm lost in 2 places... First of all I don't know what to
put where the stars ****** are, and second, I want to be able to use the
"target" variable in the OnTimer ElapsedEventHandler. Basically I would want
to call the function like so:

faderClass(this, "in", 2000);

So my class knows what to fade (this), which way to fade it (in or out), and
how long it should take (2 seconds). Any and all help is MUCH appreciated.
Thanks!
Sep 20 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sat, 20 Sep 2008 16:21:01 -0700, Gorfy
<Go***@discussions.microsoft.comwrote:
[...]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;

namespace WindowsFormsApplication1
{
class faderClass
{
public void Main(****** target, String inOut, int fadeTime)
{
System.Timers.Timer fadeTimer = new System.Timers.Timer();
fadeTimer.Elapsed += new ElapsedEventHandler(OnTimer);
fadeTimer.Interval = 50;
fadeTimer.Enabled = true;
fadeTimer.SynchronizingObject = target;
fadeTimer.Start();
}
private void OnTimer(object source, ElapsedEventArgs e)
{
target.Opacity += 0.1;
}
}
}

As you can see, I'm lost in 2 places... First of all I don't know what to
put where the stars ****** are, and second, I want to be able to use the
"target" variable in the OnTimer ElapsedEventHandler. Basically I would
want
to call the function like so:

faderClass(this, "in", 2000);
Well, following your first example, a suitable type might be "Form". But
it really depends on what kind of object(s) you want to support.

As far as dealing with the target, you have at least three reasonable
choices. One is to store the reference in your class:

class faderClass
{
private Form _target;

public void faderClass(Form target, String inOut, int fadeTime)
{
_target = target;
System.Timers.Timer fadeTimer = new System.Timers.Timer();
fadeTimer.Elapsed += OnTimer;
fadeTimer.Interval = 50;
fadeTimer.SynchronizingObject = target;
fadeTimer.Start();
}
private void OnTimer(object source, ElapsedEventArgs e)
{
_target.Opacity += 0.1;
}
}

Or you could take advantage of the fact that you're actually using the
object in the timer:

class faderClass
{
public void faderClass(Form target, String inOut, int fadeTime)
{
System.Timers.Timer fadeTimer = new System.Timers.Timer();
fadeTimer.Elapsed += OnTimer;
fadeTimer.Interval = 50;
fadeTimer.SynchronizingObject = target;
fadeTimer.Start();
}
private void OnTimer(object source, ElapsedEventArgs e)
{
Form target =
(Form)((System.Timers.Timer)source).SynchronizingO bject;

target.Opacity += 0.1;
}
}

Another alternative would be to take advantage of an anonymous method in
your class:

class faderClass
{
public void faderClass(Form target, String inOut, int fadeTime)
{
System.Timers.Timer fadeTimer = new System.Timers.Timer();
fadeTimer.Elapsed += delegate { target.Opacity += 0.1 };
fadeTimer.Interval = 50;
fadeTimer.SynchronizingObject = target;
fadeTimer.Start();
}
}

By the way, note that I took out the superfluous statement to set the
Timer.Enabled property to "true". Not only is this redundant with the
call to Start(), your use of it was actually a bug, because you were
starting the timer before you'd set the SynchronizingObject property.

Pete
Sep 21 '08 #2

P: n/a
Looks like the code is error free now! I appreciate the help. However it
seems I am still not familiar enough with this because I have a different
problem now:

The code of my "Form1.cs" starts like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class IntroBox : Form
{
public IntroBox()
{
InitializeComponent();
faderClass(this, "out", 2000);
....

And the code error I get for that last line is "Windows
FormsApplication1.faderClass is a type but is used like a variable." In
actionscript that is how we called a function (or class) but it seems that I
am missing something in C#
Sep 21 '08 #3

P: n/a
Okay I changed that line of code to this:

faderClass fader1 = new faderClass(this, "in", 2000);

but now it's telling me that WindowsFormsApplication1.faderClass does not
contain a constructor that takes 3 arguments...
Sep 21 '08 #4

P: n/a
Okay problems solved... learned a lot...

It would be a good idea for me to add "using System.Windows.Forms;" to the
faderClass.cs since I use the "Form" type.

=D

Cheers and thanks for your help!
Sep 21 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.