简体   繁体   English

使用C#添加表单控件的问题

[英]Issue in adding form controls using C#

hi all i am try to add some form controls using C#. 大家好,我尝试使用C#添加一些表单控件。 i am very much new in windows application. 我在Windows应用程序中非常新。 i have a main form. 我有一个主要形式。 and its class is is like that, 它的类就是这样,

  private void Main_Load(object sender, EventArgs e)
    {
        this.FormBorderStyle = FormBorderStyle.None;
        this.WindowState = FormWindowState.Maximized;
        var Painter = new Painter();
        Painter.Paint();
    }

i have to add many textboxes and labels using programmically (can't dragging from toolbar). 我必须使用编程方式添加许多文本框和标签(无法从工具栏拖动)。 so i created another class, Painter.CS 所以我创建了另一个类Painter.CS

class Painter
{
    Label label1;
    TextBox txtbx1;
    public void Paint(object sender, EventArgs e)
    {
        label1 = new Label();
        txtbx1 = new TextBox();
        label1.UseMnemonic = true;
        label1.Text = "First &Name:";
        label1.Location = new Point(15, 15);
        label1.BackColor = Color.Pink;
        label1.ForeColor = Color.Maroon;
        label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        label1.Size = new Size(label1.PreferredWidth, label1.PreferredHeight + 2);
        txtbx1.Text = "Enter Your Name";
        txtbx1.Location = new Point(15 + label1.PreferredWidth + 5, 15);
        txtbx1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        txtbx1.BackColor = Color.LightGray;
        txtbx1.ForeColor = Color.Maroon;
        txtbx1.Size = new Size(90, 20);
        Main.Controls.Add(label1);    //i dont know how to add this in main form
        Main.Controls.Add(txtbx1);
    }
}

i cant add this label and text box in my main form. 我无法在我的主要表单中添加此标签和文本框。 Please help me. 请帮我。

You're attempting to access a static property Controls on the Main class, but no such property exists. 您正在尝试访问Main类上的静态属性Controls ,但是不存在此类属性。 It wouldn't make sense either as controls belong to an instance of your form class. 这也没有意义,因为控件属于表单类的实例

Your entire Painter class is superfluous. 您的整个Painter类都是多余的。 Just add your controls in your Form's constructor. 只需将控件添加到Form的构造函数中即可。

public class Main : Form
{
    public Form() 
    {
        SuspendLayout();
        var label1 = new Label();
        var txtbx1 = new TextBox();
        label1.UseMnemonic = true;
        label1.Text = "First &Name:";
        label1.Location = new Point(15, 15);
        label1.BackColor = Color.Pink;
        label1.ForeColor = Color.Maroon;
        label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        label1.Size = new Size(label1.PreferredWidth, label1.PreferredHeight + 2);
        txtbx1.Text = "Enter Your Name";
        txtbx1.Location = new Point(15 + label1.PreferredWidth + 5, 15);
        txtbx1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        txtbx1.BackColor = Color.LightGray;
        txtbx1.ForeColor = Color.Maroon;
        txtbx1.Size = new Size(90, 20);
        Controls.Add(label1); 
        Controls.Add(txtbx1);
        ResumeLayout();
    }
}

This is where that code belongs anyway. 无论如何,这就是该代码所属的地方。 Your form should be responsible for its layout, not some other class. 您的表单应负责其布局,而不是其他类。 This is how the designer would have done it. 这就是设计者应该做的。

A partial class would have been created with a method named InitializeComponent which creates and lays out the controls. 将使用名为InitializeComponent的方法创建部分类,该方法创建并布置控件。 It would have been called from the constructor. 它将从构造函数中调用。

As @Ed S. pointed out is not a good way to do it like this, but to fix your code try: 正如@Ed S.指出的那样,这样做不是一个好方法,但是要修复代码,请尝试:

private Painter _painter;
private void Main_Load(object sender, EventArgs e)
{   
    //...    
    _painter = new Painter();
    _painter.Paint(this);
}

class Painter
{
   public void Paint(Form main)
   {   
//... 

    main.Controls.Add(label1);    
    main.Controls.Add(txtbx1);

//...

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM