473,662 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Opa city = 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 2012
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.Opa city = i;
while(z<10000)
{
z++;
}
}
}

And then it will compile.

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

"James" <Ja***@discussi ons.microsoft.c om> wrote in message
news:42******** *************** ***********@mic rosoft.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.Opa city = 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.Opa city = i;
while(z<10000)
{
z++;
}
}
}

And then it will compile.

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

"James" <Ja***@discussi ons.microsoft.c om> wrote in message
news:42******** *************** ***********@mic rosoft.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.Opa city = 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.Fo rms.Form currentForm)

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

MyUtils.Fade(th is);

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.Opa city = 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.co m

"James" <Ja***@discussi ons.microsoft.c om> wrote in message
news:5F******** *************** ***********@mic rosoft.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.Opa city = i;
while(z<10000)
{
z++;
}
}
}

And then it will compile.

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

"James" <Ja***@discussi ons.microsoft.c om> wrote in message
news:42******** *************** ***********@mic rosoft.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.Opa city = 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.Fo rms.Form currentForm)

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

MyUtils.Fade(th is);

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.Opa city = 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.Inv okeRequired
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

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

Similar topics

6
1902
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
9295
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 action="MaintNotification.php?ReqID=5" method="post" name="frm5"> <tr align="left" bgcolor="#dddddd" class="text" onClick="submit()"
1
2392
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 I'm having problems figuring out how to get VBto read a more complex XML file (people2.xml) and then useadditional text boxes on the same form to add more familymembers. Each family can have from 1 to 5 members. I've pasted the working code below...
8
4929
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, System.EventArgs e) { MessageBox.Show("keyboard button pressed!"); } Following is the code to load the frmTestBaby
18
2956
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. If you do have any ideas I would really like to hear them. Thanks Colin - 0 - 0 - 0 - I want a glorified popup/context menu on a button that shows only when
21
1878
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 to data bind to a normal class. So I've extended the standard text box to include a field for object name and property name. I want to be able to add a line such as : controlname.DataBindings.Add("Text", objectName, "myPropertyName") I've...
3
2220
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 object. Please keep in mind that formB is defined in a separate library project. I then display formB. Once formB is displayed, I have code in a double click event procedure in formB to display formA by getting the form object throught the property...
4
3147
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 further away, completely leaving the selector box area. Any ideas? VS 2003 and VB.Net This is a simple application at the moment but the form is inherited from a
6
2874
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 and the coresponding 'Id' from the lookup table is to be inserted into the field of the new record. I have two simple tables. "tblPerson" is the data table. The lookup
11
3237
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 (Form1.ShowDialog()) so that the user can make custom selections. 3. I can store the selections from the second form in many different
0
8856
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
8762
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...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7365
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
6185
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
4179
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
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.