我应该如何获取此代码以在课堂上工作? [英] How should I get this code to work in a class?

查看:99
本文介绍了我应该如何获取此代码以在课堂上工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我应该如何将这段代码放在一个类中,以便可以在主形式中使用它,因为我完全不知道.这些评论是荷兰语,所以不要介意.

基本上,我想在这里做的是创建一个可用于制作面板的类,并在主窗体中显示它们.我有一个列表,其中包含面板的所有名称.下面是代码.

Hello,

How should I put this code in a class so I could use it in the mainform, because I have literly no idea. The comments are in dutch, so don''t mind them.

Basically what I want to do here is to make a class that I can use for making panels and show them in the mainform. I have a list that contains all the names for the panels. Below is the code.

paneel.Controls.Clear();
            int xstart = 5;
            int ystart = 5;
            int klassenOmlaag = splitContainer.Panel2.Width / 300;
            for (int i = 0; i < films.Count; i++)
            {
                Panel klassen = new Panel(); // Nieuw paneel aanmaken.
                klassen.Name = films[i];
                klassen.Size = new Size(225, 350); // Groote van het paneel.
                klassen.Location = new Point(xstart, ystart); // Waar het paneel moet starten.
                klassen.BackColor = Color.White; // Kleur van het paneel.
                klassen.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; // Stijl.
                paneel.Controls.Add(klassen);
                if (i == klassenOmlaag)
                {
                    xstart = 5;
                    ystart = ystart + 355;
                    klassenOmlaag = klassenOmlaag + splitContainer.Panel2.Width / 230;
                }
                else
                {
                    xstart = xstart + 230;
                }
                Label klasLabel = new Label();
                klasLabel.Text = films[i];
                klasLabel.Location = new Point(5, 5);
                klassen.Controls.Add(klasLabel);


预先感谢.


Thanks in advance.

推荐答案

您好,您应该使用传递面板的函数创建一个类,然后才能使用此面板在其上创建控件.

在主窗体上,您可以创建此类的实例,或者可以使用单例模式构建该类,然后从任何地方访问它.

在这里以主要形式使用该类的实例时
Hi, You should create a class with a function that you pass the panel to, then you can use this panel to create your controls on.

On the main form, you can either create an instance of this class, or alternatively you can build the class using singleton pattern and then access it from anywhere.

Here when using an instance of the class in main form
public class MyControlManager()
{

   public void CreateControls(Panel paneel)
   {
     //here place your code
   }

}



在这里以主要形式使用该类的实例时



Here when using an instance of the class in main form

public class MyControlManager()
{
   private static MyControlManager _thisObj = null;

   public static MyControlManager Instance
   {
      get
      {
        if (_thisObj == null)
        {
           _thisObj = new MyControlManager();
        }

        return _thisObj;

      }
   }


   public void CreateControls(Panel paneel)
   {
     //here place your code
   }
}


将上述代码放入方法中,只要您希望此面板的内部定义了带有标签的属性,就只需调用此方法.

此方法可以在您的主窗体或实用程序类(具有适当访问说明的其他任何类)中.
Put the above code in a method and just call this method whenever you want to have this panel that has defined properties with a label inside it.

This method can be in your mainform or the utility class (any other class with proper access specifier).


老实说,请看一下您的代码,这不会太简单:您似乎引用了许多控件,这些控件的位置我不知道:
To be honest, look at your code, it is not going to be too easy: It looks like you refer to a number of controls, the location of which I do not know:
paneel
splitContainer

我不知道完整版本的代码中还有更多内容.
做这种事情的通常方法是创建一个自定义控件,并让它处理所有内部信息-然后,主窗体只负责将控件作为单个对象进行处理.

但是,这是我要讨论的主题之外的主题:Google将为您提供教程帮助.

There may be more in the full version of your code, I don''t know.
The normal way to do this kind of thing, would be to create a Custom Control and let it handle all the internals - the main form is then just responsible for handling the control as a single object.

However, that is a bigger subject than I want to go into here: Google will help with tutorials.


这篇关于我应该如何获取此代码以在课堂上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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