473,395 Members | 1,653 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,395 software developers and data experts.

form object

I am trying to a add a method to a helper class library I built that will
fade out the current form. My code is this:

public void fade(object currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

This works great if I go ahead and put it in my exit event on the form like
this:

int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
this.Opacity = i;
while(z<10000)
{
z++;
}
}
this.Close();

When I have it in my helper Class Library it tells me that "'object' does
not contain a definition for 'Opacity'". Am I doing this wrong or is this not
allowed?

Thank you for your help!
Jun 5 '06 #1
14 1991
James,

Well, you are passing in an object. In .NET, an object only has four
methods on it.

I assume you are coming from VB, which allows for calling
methods/properties without knowing the actual type. VB would then call the
property/method (slower than if it knew the type), not caring about the
type.

Needless to say, C# does not allow this. However, since you want to do
this on forms, you can easily change your type declaration to Form, like so:

public void fade(Form currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

And then it will compile.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <Ja***@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
I am trying to a add a method to a helper class library I built that will
fade out the current form. My code is this:

public void fade(object currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

This works great if I go ahead and put it in my exit event on the form
like
this:

int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
this.Opacity = i;
while(z<10000)
{
z++;
}
}
this.Close();

When I have it in my helper Class Library it tells me that "'object' does
not contain a definition for 'Opacity'". Am I doing this wrong or is this
not
allowed?

Thank you for your help!

Jun 5 '06 #2
Nicholas,

Yes, I am "coming from VB". I had tried doing it as (Form currentForm)
and when I compiled I would get this message: "The type or namespace name
'Form' could not be found (ar you missing a using directive or an assembly
reference?)".

James

"Nicholas Paldino [.NET/C# MVP]" wrote:
James,

Well, you are passing in an object. In .NET, an object only has four
methods on it.

I assume you are coming from VB, which allows for calling
methods/properties without knowing the actual type. VB would then call the
property/method (slower than if it knew the type), not caring about the
type.

Needless to say, C# does not allow this. However, since you want to do
this on forms, you can easily change your type declaration to Form, like so:

public void fade(Form currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

And then it will compile.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <Ja***@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
I am trying to a add a method to a helper class library I built that will
fade out the current form. My code is this:

public void fade(object currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

This works great if I go ahead and put it in my exit event on the form
like
this:

int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
this.Opacity = i;
while(z<10000)
{
z++;
}
}
this.Close();

When I have it in my helper Class Library it tells me that "'object' does
not contain a definition for 'Opacity'". Am I doing this wrong or is this
not
allowed?

Thank you for your help!


Jun 5 '06 #3
When you have the code pasted into your form event, "this" is a Form and
has Opacity.

The type "object" does not have an opacity property, hence your error.
You would have to unbox the form instance you pass in -- a costly and
unnecessary step.

Typically you would instead have this signature:

public static void Fade(Windows.Forms.Form currentForm)

By making it static in some class, call it MyUtils, you can call it from
any form like so:

MyUtils.Fade(this);

Alternately, you could make it a protected member of a parent form class
that your form inherits from, and eliminate passing the argument at all,
because the code internal to the routine could just use "this". So the
call would be:

this.Fade();

or simply,

Fade();

--Bob

James wrote:
I am trying to a add a method to a helper class library I built that will
fade out the current form. My code is this:

public void fade(object currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

This works great if I go ahead and put it in my exit event on the form like
this:

int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
this.Opacity = i;
while(z<10000)
{
z++;
}
}
this.Close();

When I have it in my helper Class Library it tells me that "'object' does
not contain a definition for 'Opacity'". Am I doing this wrong or is this not
allowed?

Thank you for your help!

Jun 5 '06 #4
Your utility class needs a reference to System.Windows.Forms, and it
needs a using directive for same, or else a fully qualified reference to
the System.Windows.Forms.Form class. ("using" in this context is
equivalent to "Imports" in VB.NET).

--Bob

James wrote:
Nicholas,

Yes, I am "coming from VB". I had tried doing it as (Form currentForm)
and when I compiled I would get this message: "The type or namespace name
'Form' could not be found (ar you missing a using directive or an assembly
reference?)".

James

Jun 5 '06 #5
James,

When you do that, you will have to add a reference to
System.Windows.Forms.dll to your project (if it is not there already).

Then, at the top of your file, do this:

using System.Windows.Forms;
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <Ja***@discussions.microsoft.com> wrote in message
news:5F**********************************@microsof t.com...
Nicholas,

Yes, I am "coming from VB". I had tried doing it as (Form currentForm)
and when I compiled I would get this message: "The type or namespace name
'Form' could not be found (ar you missing a using directive or an assembly
reference?)".

James

"Nicholas Paldino [.NET/C# MVP]" wrote:
James,

Well, you are passing in an object. In .NET, an object only has four
methods on it.

I assume you are coming from VB, which allows for calling
methods/properties without knowing the actual type. VB would then call
the
property/method (slower than if it knew the type), not caring about the
type.

Needless to say, C# does not allow this. However, since you want to
do
this on forms, you can easily change your type declaration to Form, like
so:

public void fade(Form currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

And then it will compile.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"James" <Ja***@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
>I am trying to a add a method to a helper class library I built that
>will
> fade out the current form. My code is this:
>
> public void fade(object currentForm)
> {
> int z = 0;
> for(double i=1.0; i> 0; i-=.1)
> {
> currentForm.Opacity = i;
> while(z<10000)
> {
> z++;
> }
> }
> }
>
> This works great if I go ahead and put it in my exit event on the form
> like
> this:
>
> int z = 0;
> for(double i=1.0; i> 0; i-=.1)
> {
> this.Opacity = i;
> while(z<10000)
> {
> z++;
> }
> }
> this.Close();
>
> When I have it in my helper Class Library it tells me that "'object'
> does
> not contain a definition for 'Opacity'". Am I doing this wrong or is
> this
> not
> allowed?
>
> Thank you for your help!


Jun 5 '06 #6
Bob & Nicholas,

Thank you for your help. I added the reference of System.Windows.Forms to
my helper class library and it works great! Just what I wanted to do! Now I
can call it from any form.

Thank you!

James

"Bob Grommes" wrote:
When you have the code pasted into your form event, "this" is a Form and
has Opacity.

The type "object" does not have an opacity property, hence your error.
You would have to unbox the form instance you pass in -- a costly and
unnecessary step.

Typically you would instead have this signature:

public static void Fade(Windows.Forms.Form currentForm)

By making it static in some class, call it MyUtils, you can call it from
any form like so:

MyUtils.Fade(this);

Alternately, you could make it a protected member of a parent form class
that your form inherits from, and eliminate passing the argument at all,
because the code internal to the routine could just use "this". So the
call would be:

this.Fade();

or simply,

Fade();

--Bob

James wrote:
I am trying to a add a method to a helper class library I built that will
fade out the current form. My code is this:

public void fade(object currentForm)
{
int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
while(z<10000)
{
z++;
}
}
}

This works great if I go ahead and put it in my exit event on the form like
this:

int z = 0;
for(double i=1.0; i> 0; i-=.1)
{
this.Opacity = i;
while(z<10000)
{
z++;
}
}
this.Close();

When I have it in my helper Class Library it tells me that "'object' does
not contain a definition for 'Opacity'". Am I doing this wrong or is this not
allowed?

Thank you for your help!

Jun 5 '06 #7
Two observations:

1: any static method should probably be thread-aware; when dealing with
forms, this means that it should probably test currentForm.InvokeRequired
and (if true) push the method onto the owning UI's thread; I'm pretty sure
(not 100%) that Opacity has thread affinity..

2: while(z<10000) {z++;}
I don't mean to be rude - just frank; this is quite possibly the worst way
of putting a delay into code:
* It will run at different speeds on different computers
* it might even get completely removed by an optimizing compiler (typically
only in release mode)
You don't want to Sleep(), as this will hang the UI thread (unless you spin
up a second thread that alternates between Sleep() [on its own thread] and
BeginInvoke() [on the form]); a timer would be the normal implementation?

Marc
Jun 6 '06 #8
2.1: plus it burns up CPU cycles for no good reason ;-p
Jun 6 '06 #9
Bob Grommes wrote:
The type "object" does not have an opacity property, hence your error.
You would have to unbox the form instance you pass in -- a costly and
unnecessary step.


Correct me if I'm wrong, but boxing does not occur on references types.
Boxing refers to the conversion of a value type to a reference type
and unboxing is the conversion back. In this case, object is a
reference type already so no boxing occurs.

I took the OP code and ran it through Reflector and there are no box
instructions.

Jun 6 '06 #10
Marc,

Is Frank your alias???
Well, early on I thought about using a timer since a counter does not run
at the same speed on every cpu. I really don't care about burning up cpu
cycles on the local machine as they don't cost anything. I am somewhat
concerned about the differing speeds on the various local workstations.
But my first and foremost concern was to get the stupid thing to work and
since I didn't know how to reference the form and Bob and Nicholas told me
how, now I can refine the rest of the process. Now I just need to figure out
how to reference the timer.
Since I am new to C# (as Nicholas summized) solutions rather than
critiques are helpful. I already knew that I should use the timer, but I
still don't know how to.

James
"Marc Gravell" wrote:
Two observations:

1: any static method should probably be thread-aware; when dealing with
forms, this means that it should probably test currentForm.InvokeRequired
and (if true) push the method onto the owning UI's thread; I'm pretty sure
(not 100%) that Opacity has thread affinity..

2: while(z<10000) {z++;}
I don't mean to be rude - just frank; this is quite possibly the worst way
of putting a delay into code:
* It will run at different speeds on different computers
* it might even get completely removed by an optimizing compiler (typically
only in release mode)
You don't want to Sleep(), as this will hang the UI thread (unless you spin
up a second thread that alternates between Sleep() [on its own thread] and
BeginInvoke() [on the form]); a timer would be the normal implementation?

Marc

Jun 6 '06 #11
Marc,

You said I shouldn't use sleep, but I couldn't figure out how to do it with
a time in my class library.

This is what I have done:

using System.Windows.Forms;
using System.Threading;
public void fade(Form currentForm)
{
for(double i=1.0; i> 0; i-=.1)
{
currentForm.Opacity = i;
Thread.Sleep(100);
}
}

Is this dangerous? It is working for me.

James
-------------------------------------------------------------------------------------
"James" wrote:
Marc,

Is Frank your alias???
Well, early on I thought about using a timer since a counter does not run
at the same speed on every cpu. I really don't care about burning up cpu
cycles on the local machine as they don't cost anything. I am somewhat
concerned about the differing speeds on the various local workstations.
But my first and foremost concern was to get the stupid thing to work and
since I didn't know how to reference the form and Bob and Nicholas told me
how, now I can refine the rest of the process. Now I just need to figure out
how to reference the timer.
Since I am new to C# (as Nicholas summized) solutions rather than
critiques are helpful. I already knew that I should use the timer, but I
still don't know how to.

James
"Marc Gravell" wrote:
Two observations:

1: any static method should probably be thread-aware; when dealing with
forms, this means that it should probably test currentForm.InvokeRequired
and (if true) push the method onto the owning UI's thread; I'm pretty sure
(not 100%) that Opacity has thread affinity..

2: while(z<10000) {z++;}
I don't mean to be rude - just frank; this is quite possibly the worst way
of putting a delay into code:
* It will run at different speeds on different computers
* it might even get completely removed by an optimizing compiler (typically
only in release mode)
You don't want to Sleep(), as this will hang the UI thread (unless you spin
up a second thread that alternates between Sleep() [on its own thread] and
BeginInvoke() [on the form]); a timer would be the normal implementation?

Marc

Jun 6 '06 #12
Fair point about solutions rather than critique; I'm hectic at the moment,
but I will dedicate a few mintes later on today to provide a timer based
solution for this.
The Sleep() solution addresses both CPU thrashing and the differing speeds
(on different CPUs), but it could still leave your UI feeling (slightly)
unresponsive; during all of those those 100ms back-to-back (so for a second)
you form will not really be available to process messages. Since this is
only a second this might not be a big problem; certainly a big improvement
on a loop.

Again - I will throw something together later today.

Marc
Jun 7 '06 #13
As promised, this is what I came up with over lunch...

Overview:
FormFader; externally only has a single static method (Fade); this checks to
see if a fade is already in progress, and if so aborts the old fade; it then
sets up a new object to represent the fade (and to use in the above check),
and starts running a method on a pool thread; this method fades by the set
"step" amount (slipping into the UI thread for a moment), then sleeps for an
interval (back on the pool thread); this gives the UI time (between steps)
to draw itself, respond to events, etc. When the opacity is "close enough"
it sets the opactiy to the target (to round out any floating point issues)
and exits the pool thread.

You could also add some kind of event or callback to indicate when the fade
has been completed...

Regards,

Marc
using System;
using System.Windows.Forms;
using System.Threading;
using System.Collections.Generic;
static class Program {

static void Main() {
using (Form f = new Form())
using (Button fadeIn = new Button())
using (Button fadeOut = new Button())
{
fadeIn.Dock = DockStyle.Left;
fadeOut.Dock = DockStyle.Right;
fadeIn.Text = "Fade In";
fadeOut.Text = "Fade Out";
f.Text = "Fade";
fadeIn.Click += delegate { FormFader.Fade(f, 1.0F, 0.1F,
100); };
fadeOut.Click += delegate { FormFader.Fade(f, 0.2F, 0.1F,
100); };
f.Controls.AddRange(new Control[] { fadeIn, fadeOut });
f.ShowDialog();
}
}
}
public class FormFader {

private readonly Form _form;
private readonly double _targetOpacity, _stepSize;
private readonly int _ticksPerStep;
private volatile bool IsAlive = true;
private FormFader(Form form, double targetOpacity, double stepSize, int
ticksPerStep) {
_form = form;
_targetOpacity = targetOpacity;
_stepSize = stepSize;
_ticksPerStep = ticksPerStep;
}
private void Enact(object state) {
// just to meet WaitCallback
Enact();
}
private void Enact() {
while(EnactStep()) {
//pause between steps
Thread.Sleep(_ticksPerStep);
}
lock (_fadingForms) {
_fadingForms.Remove(_form);
}
}
private bool EnactStep() {
bool result = false;
if (IsAlive) {
_form.Invoke((ThreadStart)delegate {
// now on UI thread
double delta = _form.Opacity - _targetOpacity;
if (Math.Abs(delta) <= _stepSize) {
_form.Opacity = _targetOpacity;
} else if (delta > 0) { // form's opacity is greater than
target
_form.Opacity -= _stepSize;
result = true; // keep going
} else {
_form.Opacity += _stepSize;
result = true; // keep going
}
});
}
return result;
}

private readonly static Dictionary<Form, FormFader> _fadingForms = new
Dictionary<Form, FormFader>();

public static void Fade(Form form, double targetOpacity, double
stepSize, int ticksPerStep) {
if (form == null) throw new ArgumentNullException("form");
if (stepSize <= 0.0F) throw new ArgumentException("stepSize");
FormFader newFader = new FormFader(form, targetOpacity, stepSize,
ticksPerStep);
lock (_fadingForms) {
FormFader oldFader;
if (_fadingForms.TryGetValue(form, out oldFader)) {
oldFader.IsAlive = false;
}
_fadingForms[form] = newFader;
}
ThreadPool.QueueUserWorkItem(newFader.Enact);
}
}
Jun 7 '06 #14
Correction: the _fadingForms.Remove(_form); code should read:

FormFader fader;
if (_fadingForms.TryGetValue(_form, out fader) &&
ReferenceEquals(this, fader)) {
_fadingForms.Remove(_form);
}

(all inside the lock)
This ensures that if two Fade() requests are made re the same form, then the
original request does not take the second fade-request out of the
dictionary.

Marc
Jun 7 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bob | last post by:
Declaring a module level form object in multiple froms within a project to show another project form causes a stack overflow and other assorted errors. Can anyone help?
4
by: Stuart Perryman | last post by:
Hi, I have the following code which works just fine in IE6 but not in Firefox. It is an extract of several table rows each with an individual form. It is generated by php. <form...
1
by: kyma via .NET 247 | last post by:
Hi, I haveto use VB to create a form that reads an exisiting XML fileand then allows updates via the VB form. My problem is that I was able to get VB to read a simple XML file(people.XML), but...
8
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
18
by: Colin McGuire | last post by:
Hi - this was posted last weekend and unfortunately not resolved. The solutions that were posted almost worked but after another 5 days of working on the code everynight, I am not further ahead....
21
by: Simon Verona | last post by:
Hope somebody can help! I want to automatically be able to add code to the initialize routine on a Windows form when I add a custom control that I've written to a form. Specifically, I'm trying...
3
by: BakelNB | last post by:
I am new to the .Net environment, so please bear with me. I am passing a form object (e.g. formA) to an existing form (e.g. formB) by setting a property value defined in formB to hold the formA...
4
by: Rod Gill | last post by:
Hi, I have a form that when opened in the designer appears of the screen. The form selector can't be dragged (or resized) and if I scroll right and down to centralise it the form simply jumps...
6
by: dbuchanan | last post by:
VS2005 I've been reading all the help I can on the topic (MSDN, other) but I can't make sense of this. Desired behavior; The user is to choose from the displayed list of the databound combobox...
11
by: joey.powell | last post by:
Hello, I have a windows forms application (VS2005) where I need to do the following... 1. Startup with a main form. 2. Have the user to select a file and then bring up a second form modally...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.