形式对象 [英] form object

查看:52
本文介绍了形式对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个方法添加到我构建的帮助器类库中,这将使b / b
淡出当前表单。我的代码是这样的:


public void fade(object currentForm)

{

int z = 0;

for(double i = 1.0; i> 0; i - =。1)

{

currentForm.Opacity = i;

while(z <10000)

{

z ++;

}

}

}


如果我继续把它放在表格上的退出事件中,这很好用

这个:


int z = 0;

for(double i = 1.0; i> 0; i - =。1)

{

this.Opacity = i;

while(z <10000)

{

z ++;

}

}

this.Close();


当我在帮助器类库中它有它告诉我 ;''对象''确实

不包含''Opacity''的定义。我这样做错了还是这不是允许



感谢您的帮助!

解决方案

James,


嗯,你传递的是一个物体。在.NET中,一个对象上只有四个

方法。


我假设你来自VB,允许调用

方法/属性,不知道实际类型。然后VB将调用

属性/方法(比它知道类型的速度慢),而不是关心

类型。


不用说,C#不允许这样做。但是,既然您希望在表单上执行

,则可以轻松地将类型声明更改为Form,如下所示:


public void fade(Form currentForm )

{

int z = 0;

for(double i = 1.0; i> 0; i - = .1)

{

currentForm.Opacity = i;

while(z <10000)

{

z ++;

}

}

}


然后它会编译。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com

" James" ; < JA *** @ discussions.microsoft.com>在消息中写道

news:42 ********************************** @ microsof t.com ...

我正在尝试向我构建的辅助类库中添加一个方法,它将淡出当前的表单。我的代码是这样的:

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


如果我继续把它放在表格上的退出事件中这很好用
这样:

int z = 0;
for(double i = 1.0; i> 0; i - =。1)
{
this.Opacity = i;
while(z< 10000 )
{
z ++;
}
}
这个。关闭();

当我在帮助器类库中有它时告诉我'''对象''不包含'不透明度''的定义。我做错了还是这个
不允许?

感谢您的帮助!



< blockquote> Nicholas,


是的,我是来自VB。我曾经尝试过这样做(Form currentForm)

当我编译时我会得到这样的消息:类型或名称空间名称

''表单''不能被发现(你错过了使用指令或装配

参考?)"。


James


Nicholas Paldino [.NET / C#MVP]"写道:

詹姆斯,
嗯,你传递的是一个物体。在.NET中,一个对象只有四种方法。

我假设你来自VB,它允许在不知道实际类型的情况下调用方法/属性。然后VB将调用
属性/方法(比它知道类型的速度慢),而不是关心
类型。

毋庸置疑,C#不允许这样做。但是,既然您想在表单上执行此操作,则可以轻松地将类型声明更改为Form,如下所示:

public void fade(Form currentForm)
{
int z = 0;
for(double i = 1.0; i> 0; i - =。1)
{
currentForm.Opacity = i;
while(z< ; 10000)
{
z ++;
}
}
}
然后它会编译。
希望这会有所帮助。

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

詹姆斯 < JA *** @ discussions.microsoft.com>在消息中写道
新闻:42 ********************************** @ microsof t.com。 ..

我正在尝试向我构建的辅助类库添加一个方法,它将淡出当前的表单。我的代码是这样的:

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


如果我继续把它放在表格上的退出事件中这很好用
这样:

int z = 0;
for(double i = 1.0; i> 0; i - =。1)
{
this.Opacity = i;
while(z< 10000 )
{
z ++;
}
}
这个。关闭();

当我在帮助器类库中有它时告诉我'''对象''不包含'不透明度''的定义。我做错了还是这个
不允许?

感谢您的帮助!




当您将代码粘贴到表单事件中时,this是一个表格和

有不透明度。


类型对象没有不透明属性,因此你的错误。

你必须打开你传入的表单实例 - 这是一个代价高昂且不必要的步骤。


通常你会改为签名:


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


通过在某些类中将其设置为静态,将其称为MyUtils,您可以从

中调用它,如下所示:


MyUtils.Fade(this) ;


或者,你可以使它成为你的表格继承的父表单类

的受保护成员,并消除传递参数,

因为例程内部的代码只能使用this。所以

的电话会是:


this.Fade();


或简单地说,


Fade();


--BOB


James写道:

我正在尝试向我构建的帮助器类库添加一个方法,它将淡出当前的表单。我的代码是这样的:

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


如果我继续将它放在我的退出事件中,就像
这样:

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

当我在帮助器类库中它有它告诉我 ;''对象''
不包含''Opacity''的定义。我做错了还是不允许这样做?

感谢您的帮助!



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!

解决方案

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!



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!




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!



这篇关于形式对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆