473,408 Members | 1,788 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Trying to create a dynamic class...?

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
4 2310
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bil Muh | last post by:
Esteemede Developers, I would like to Thank All of You in advance for your sincere guidances. I am developing a software using Visual C++ .NET Standard Edition with Windows Form (.NET)...
1
by: Tom | last post by:
Hi All Thanks However, I want to create the vbproject . One vb project (type is windows application .exe) dynamic call another vbproject (class libary .dll) . while windows application project...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
7
by: MarkoH | last post by:
Wsdl.exe /server creates abstract class derived from WebService. Is there a way to create this class at runtime based on some WSDL file given at runtime ? What would be even better - creating...
1
by: Russell Mangel | last post by:
Sorry about the Cross-Post, I posted my question in the wrong group. Hello, What is the simplest way to create a dynamic collection (during run-time), using basic C (Struct data types). Since...
4
by: Benny Dein | last post by:
Hi I want to create a servlet or something similar by which im able to create virtual webservices. Lets say i would like to create a webservice with a method called 'getDate' which returnes...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
1
by: Sundhas | last post by:
Hey! I am working on jrxml to create dynamic re0ports. I have parameterized the columns i.e. the jrxml for that report can be used to generate other reports as well. However, i have not managed...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.