自定义面板控件->不确定我是否遵循严格的原则... [英] Custom Panel control -> Not sure if I'm following the rigth path...

查看:93
本文介绍了自定义面板控件->不确定我是否遵循严格的原则...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是OO编程的相对新手,所以请耐心:D
为了锻炼,我一直在编写一个小型桌面应用程序(例如酒店的房间预订系统).为了可视化所有可用房间,我创建了一个自定义面板控件(颜色编码-红色表示忙碌的房间,绿色表示可用的房间,依此类推).在我的PaintedPanel类中,我有一个创建网格的方法(如国际象棋棋盘)
使用简单的"for"循环,它创建指定数量的面板并将其添加到父控件中.问题是->是否可以在类本身内创建特定类的对象(或对象集合)? (例如,我的PaintedPanel类具有方法"createPanelGrid",在其中创建PaintedPanel对象的实例).
我决定以这种方式进行操作,以便只有一个"onclick"事件处理程序可为所有PaintedPanel实例提供服务,而不是在我的主窗体逻辑中创建一个Panels面板,并且必须为每个和每个PaintedPanels.
是否有意义 ?不知道我是否有自定义控件的想法...
任何建议将不胜感激.

干杯,

Mike

Hi guys,

I am a relative newbie to OO programming so please be patient :D
For the sake of exercise I''ve been writing a small desktop app (room booking system, eg. in hotel). In order to visualise all of the rooms available I''ve created a custom panel control (color coded - red for busy room, green for available and so on). Within my PaintedPanel class I have a method that creates a grid (like a chess board)
of the panels... Using a simple ''for'' loop it creates specified number of panels and adds them to parent control. Question is -> is it ok to create an object (or collection of objects) of the particular class within the class itself ? (eg. my PaintedPanel class has a method ''createPanelGrid'' in which I create an instances of the PaintedPanel objects).
I decided to do it this way in order to have only one ''onclick'' event handler that serves all of the PaintedPanel instances, rather than creating a grid of Panels within my main form logic and having to code multiple event handlers for each and every PaintedPanels.
Does it make sense ? Not sure if I got the idea of custom controls rigth...
Any suggestions will be greatly appreciated.

Cheers,

Mike

推荐答案

您可以做到,但是您的推理有一些缺陷.

我决定采用这种方式,以便只有一个"onclick"事件处理程序可以为所有PaintedPanel实例提供服务,而不是在我的主窗体逻辑中创建一个Panels面板,并且必须为每个事件处理程序编码多个事件处理程序以及每个PaintedPanels."

同一事件处理程序可以处理所需的任意多个实例:sender参数告诉您它与哪个实例相关:
You can do that, but your reasoning is slightly flawed.

"I decided to do it this way in order to have only one ''onclick'' event handler that serves all of the PaintedPanel instances, rather than creating a grid of Panels within my main form logic and having to code multiple event handlers for each and every PaintedPanels."

The same event handler can handle as many instances as you need: the sender parameter tells you which one it relates to:
void myEventHandler(object sender, EventArgs e)
   {
   MyCustomControl mcc = sender as MyCustomeControl;
   if (mcc != null)
      {
      Console.WriteLine(mcc.APropertyOfTheControl);
      }
   }



通常,您将其保留下来,以使Room类甚至不知道或不需要知道其他房间的存在:相反,您将拥有一个Floor类,它知道楼层中的所有房间,以及一个Hotel类,它知道楼层中的所有房间.大约所有楼层.



Normally, you would keep it so that the Room class does not know or need to know about even the existence of other rooms: instead, You would have a Floor class which knew about all the rooms on a floor, and a Hotel class which knew about all the floors.


这篇关于自定义面板控件->不确定我是否遵循严格的原则...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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