在C#中,我如何编码'this = that'? [英] In C#, how do I code 'this = that'?

查看:92
本文介绍了在C#中,我如何编码'this = that'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个可以通过编程方式点击的MenuItem。我通过编码来启动



class ClickableMenuItem:MenuItem {


public void DoClick(EventArgs e) {

OnClick(e);

}


ClickableMenuItem(MenuItem mi){

/ /我在这做什么?!?

// this = mi; < ---不起作用

}

}


如你所见,我不知道如何告诉ClickableMenuItem在构建时从MenuItem获取所有字段和事件




你会怎么做?

解决方案

在继承中,子类*本质上由*基础

类组成,所以你不能再传递另一个进入ctor,让它吸收。

你理论上可以将一个作为模板传递来复制,但我不会这样做b $ b认为这就是你想要的实现。

但是,你也不需要做任何这样的事情......


假设你在ClickableMenuItem上提供ctors来模仿<对于MenuItem,
ctors,那么它应该足以简单地创建并添加

ClickableMenuItem实例到你的菜单,而不是MenuItem

实例。缺点是这可能会让IDE有点困惑,

可能。


Marc

Charles Jenkinsaécrit:


我想创建一个MenuItem,我可以通过编程方式点击。我用以下代码开始




class ClickableMenuItem:MenuItem {


public void DoClick(EventArgs e) {

OnClick(e);

}


ClickableMenuItem(MenuItem mi){

/ /我在这做什么?!?

// this = mi; < ---不起作用

}

}


如你所见,我不知道如何告诉ClickableMenuItem在构建时从MenuItem获取

所有字段和事件。


你会怎么做?






你检查过MSDN中的方法MenuItem.PerformClick吗?...


至于你的解决方案,我觉得你有点困惑,因为你想继承和组合

:你必须选择。如果你从MenuItem继承
,那么为什么不在你需要的时候创建一个ClickableMenuItem实例

,而不是创建一个

的MenuItem实例只用于初始化一个新的ClickableMenuItem实例?...


好​​吧,我想那是因为你用设计师创建你的菜单,

和你只能用这种方式创建MenuItem实例......那么

将是三种解决方法:

*将你的ClickableMenuItem隔离在一个库中并将其添加到

设计师控制。使用继承。构造函数没有

参数,你可以像MenuItem一样使用它。

*手动创建你的ClickableMenuItem元素。在您的表单中

构造函数,在调用InitializeComponents()之后。使用继承。

你也不需要构造函数中的任何参数。

*让设计师创建你的MenuItem。然后手动创建您的

ClickableMenuItem元素。在你的表单构造函数中,在

调用InitializeComponents()之后。使用组合(在这种情况下,如果你正确地做的话,
似乎是相当漫长和挑剔的......)。


希望这会有所帮助。


Mathieu


On 2006-08-21 10:16:34 -0400,Marc Gravell < ma ********** @ gmail.comsaid:


假设你在ClickableMenuItem上提供ctors来模仿

ctors of MenuItem,那么它就足以简单地创建并添加

ClickableMenuItem实例到你的菜单,而不是MenuItem

实例。缺点是这可能会让IDE有点困惑,

可能。


Marc



你的建议是完美的,但我最近遇到了严重的问题

我创建的任何自定义控件都可以正常使用一段时间,但是更快或者稍后,它们总是等于
混淆IDE。 VS 2005 Designer处于

状态,它将不再显示自定义控件(或任何包含它的形式

)。


因此,我试图找到一个解决方案,允许我在我的表单上只使用

标准控件,没有可视继承,然后添加

我在运行时实际需要的行为。我希望能够做到这样的事情:


//将设计师理解的控件转换为我实际需要的控件

ClickableMenuItem usefulMenuItem = new ClickableMenuItem(

sadlyLimitedMenuItem);

usefulMenuItem.DoClick();


但是,你是绝对正确的:如果Visual Studio 2005设计师

并不傻,在IDE中使用继承的控件将是正确的

解决方案


I want to make a MenuItem that I can ''click'' programmatically. I
started by coding this:

class ClickableMenuItem : MenuItem {

public void DoClick( EventArgs e ) {
OnClick( e );
}

ClickableMenuItem( MenuItem mi ) {
// What do I do here?!?
// this = mi; <--- does not work
}
}

As you can see, I have no idea of how to tell ClickableMenuItem to take
all of its fields and events from MenuItem upon construction.

How would you do this?

解决方案

In inheritance, the sub-class *is intrinsically composed of* the base
class, so you can''t pass another one into the ctor for it to absorb.
You could theoretically pass one in as a template to copy, but I don''t
think this is what you are trying to achieve.
However, you also shouldn''t need to do any of this...

Assuming you make ctors available on ClickableMenuItem to mimic the
ctors of MenuItem, then it should be enough to simple create and add
ClickableMenuItem instances to your menus, instead of MenuItem
instances. The downside is that this might confuse the IDE a bit,
maybe.

Marc


Charles Jenkins a écrit :

I want to make a MenuItem that I can ''click'' programmatically. I started
by coding this:

class ClickableMenuItem : MenuItem {

public void DoClick( EventArgs e ) {
OnClick( e );
}

ClickableMenuItem( MenuItem mi ) {
// What do I do here?!?
// this = mi; <--- does not work
}
}

As you can see, I have no idea of how to tell ClickableMenuItem to take
all of its fields and events from MenuItem upon construction.

How would you do this?

Hi,

Have you checked the method MenuItem.PerformClick in MSDN ?...

As for your solution, I think you are a bit confused because you want
both inheritance and composition : you will have to choose. If you
inherit from MenuItem, then why not create a ClickableMenuItem instance
whenever you need one, instead of creating a MenuItem instance that will
only be used to initialize a new ClickableMenuItem instance ?...

OK, I guess that is because you used the designer to create your menu,
and you could only create MenuItem instances this way... Then there
would be three workarounds :
* isolate your ClickableMenuItem in a library and add it to the
designer controls. Use inheritance. The constructor would have no
parameters, and you could use it exactly like a MenuItem.
* create your ClickableMenuItem elements "by hand" in your form
constructor, after the call to InitializeComponents(). Use inheritance.
You would not want any parameter in your constructor either.
* let the designer create your MenuItem. Then create your
ClickableMenuItem elements "by hand" in your form constructor, after the
call to InitializeComponents(). Use composition (which in this case
seems to be a rather long and fastidious if you do it properly...).

Hope this helps.

Mathieu


On 2006-08-21 10:16:34 -0400, "Marc Gravell" <ma**********@gmail.comsaid:

Assuming you make ctors available on ClickableMenuItem to mimic the
ctors of MenuItem, then it should be enough to simple create and add
ClickableMenuItem instances to your menus, instead of MenuItem
instances. The downside is that this might confuse the IDE a bit,
maybe.

Marc

Your advice is perfect, but I have had severe problems lately in that
any custom conrol I create works just fine for a while, but sooner or
later, they always confuse the IDE. The VS 2005 Designer gets in a
state where it will no longer display the custom control (or any form
which contains it).

Therefore, I am trying to find a solution that allow me to use only
standard controls on my form, with no visual inheritance, and then add
the behaviors I actually need at runtime. I was hoping to be able to do
something like this:

// Convert the control the designer understands to the one I actually need
ClickableMenuItem usefulMenuItem = new ClickableMenuItem(
sadlyLimitedMenuItem );
usefulMenuItem.DoClick();

But, you are absolutely right: If the Visual Studio 2005 designer
didn''t suck, using inherited controls in the IDE would be the correct
solution


这篇关于在C#中,我如何编码'this = that'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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