473,779 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows. Forms;
namespace WindowsFormsApp lication1
{
public partial class IntroBox : Form
{
public IntroBox()
{
InitializeCompo nent();
faderClass(this , "out", 2000);
System.Timers.T imer fadeTimer = new System.Timers.T imer();
fadeTimer.Elaps ed += new ElapsedEventHan dler(OnTimer);
fadeTimer.Inter val = 50;
fadeTimer.Enabl ed = true;
fadeTimer.Synch ronizingObject = this;
fadeTimer.Start ();
}

private void OnTimer(Object source, ElapsedEventArg s 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.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.Threadin g;
using System.Timers;

namespace WindowsFormsApp lication1
{
class faderClass
{
public void Main(****** target, String inOut, int fadeTime)
{
System.Timers.T imer fadeTimer = new System.Timers.T imer();
fadeTimer.Elaps ed += new ElapsedEventHan dler(OnTimer);
fadeTimer.Inter val = 50;
fadeTimer.Enabl ed = true;
fadeTimer.Synch ronizingObject = target;
fadeTimer.Start ();
}
private void OnTimer(object source, ElapsedEventArg s 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 ElapsedEventHan dler. 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 2329
On Sat, 20 Sep 2008 16:21:01 -0700, Gorfy
<Go***@discussi ons.microsoft.c omwrote:
[...]
using System;
using System.Collecti ons.Generic;
using System.Linq;
using System.Text;
using System.Threadin g;
using System.Timers;

namespace WindowsFormsApp lication1
{
class faderClass
{
public void Main(****** target, String inOut, int fadeTime)
{
System.Timers.T imer fadeTimer = new System.Timers.T imer();
fadeTimer.Elaps ed += new ElapsedEventHan dler(OnTimer);
fadeTimer.Inter val = 50;
fadeTimer.Enabl ed = true;
fadeTimer.Synch ronizingObject = target;
fadeTimer.Start ();
}
private void OnTimer(object source, ElapsedEventArg s 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 ElapsedEventHan dler. 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.T imer fadeTimer = new System.Timers.T imer();
fadeTimer.Elaps ed += OnTimer;
fadeTimer.Inter val = 50;
fadeTimer.Synch ronizingObject = target;
fadeTimer.Start ();
}
private void OnTimer(object source, ElapsedEventArg s 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.T imer fadeTimer = new System.Timers.T imer();
fadeTimer.Elaps ed += OnTimer;
fadeTimer.Inter val = 50;
fadeTimer.Synch ronizingObject = target;
fadeTimer.Start ();
}
private void OnTimer(object source, ElapsedEventArg s e)
{
Form target =
(Form)((System. Timers.Timer)so urce).Synchroni zingObject;

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.T imer fadeTimer = new System.Timers.T imer();
fadeTimer.Elaps ed += delegate { target.Opacity += 0.1 };
fadeTimer.Inter val = 50;
fadeTimer.Synch ronizingObject = 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 SynchronizingOb ject 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.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows. Forms;

namespace WindowsFormsApp lication1
{
public partial class IntroBox : Form
{
public IntroBox()
{
InitializeCompo nent();
faderClass(this , "out", 2000);
....

And the code error I get for that last line is "Windows
FormsApplicatio n1.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 WindowsFormsApp lication1.fader Class 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
3562
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) template. Briefly -------------------------------------------------------------------------------------------- I need to create dynamically some controls on the forms, and display these
1
2402
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 do not need add reference "class libary ". I use VB6 as example: I create standard exe name "myApp" with the form "frmtest" . In this frmtest. I create the button name "cmdBtn" Meanwhile I create activex dll name "classLibs" with the class Name
7
6787
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 = LoadOracleData(sql) '____Do amazing things
7
4335
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 concrete class but with empty method implementation of course since WSDL does not provide any info on implementation. Thanks M.
1
2091
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 I am doing C++/CLI interop I wish to avoid using vector class. I am using Visual Studio 2005 C++/CLI, and I am writing an Un-Managed class library. The project type will be a static library (possibly a dll). I have
4
2810
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 the current date. This could be done by making a wsdl file (or whatever the name is) and via a wizard in some java tool make a real webservice. This webservice would be static since it creates a class in which i can make my code to return the
1
4660
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 ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by pressing Remove button the selecetd row will be removed. I used viewstate to keep my value for postback, I want by changing selectedvalue of...
0
3501
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- 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 ,another row will be created with the same control (I mean another dropdown and 2 button) and so on. and by...
1
9680
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 to make the fields flexible. That is, if the user selects 4 columns it would work but if 1 or 2 or 3 columns are selected, it gives an error since the field names are unidentified. Please post a solution urgently if something like a default...
0
9633
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10305
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10137
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9928
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8959
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7483
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6724
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.