Question

hi all i am try to add some form controls using C#. i am very much new in windows application. 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

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.

Était-ce utile?

La solution 2

As @Ed S. pointed out is not a good way to do it like this, but to fix your code try:

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);

//...

    }
}

Autres conseils

You're attempting to access a static property Controls on the Main class, but no such property exists. It wouldn't make sense either as controls belong to an instance of your form class.

Your entire Painter class is superfluous. Just add your controls in your Form's constructor.

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. It would have been called from the constructor.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top