Reputation: 187
I have declared a class called SubjectRow
, each instance of which has its own controls inherited from System.Windows.Forms.Control
:
public class SubjectRow : Control
{
static int counter = 0;
public ComboBox subjectBox = new ComboBox();
public Label maxBox = new Label();
public TextBox nBox = new TextBox();
public TextBox aBox = new TextBox();
public TextBox mBox = new TextBox();
public TextBox eBox = new TextBox();
public TextBox cBox = new TextBox();
public SubjectRow()
{
counter++;
subjectBox.Location = new Point(100, 300);
}
}
In my SubjectRow()
method, you can see my attempt to make the ComboBox subjectBox
visible on the form using new Point(left, top)
. However, this does nothing and the form is still blank on runtime.
I am pretty new to C#, but my understanding is that the SubjectRow()
method is run whenever a new instance of class SubjectRow
is created. So why does this not work? How can I have the subjectBox
control appear on the form at a given location?
Upvotes: 0
Views: 234
Reputation: 1067
Below is the correct way of doing it
MyUserControl.cs:
using System.Windows.Forms;
namespace ControlTest
{
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.txtName = new System.Windows.Forms.TextBox();
this.lblName = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(107, 18);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(305, 20);
this.txtName.TabIndex = 0;
//
// lblName
//
this.lblName.AutoSize = true;
this.lblName.Location = new System.Drawing.Point(22, 23);
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(35, 13);
this.lblName.TabIndex = 1;
this.lblName.Text = "Name";
//
// MyUserControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.lblName);
this.Controls.Add(this.txtName);
this.Name = "MyUserControl";
this.Size = new System.Drawing.Size(433, 57);
this.ResumeLayout(false);
this.PerformLayout();
}
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.Label lblName;
}
}
In your Form, add the user control:
using System.Windows.Forms;
namespace ControlTest
{
public partial class Form1 : Form
{
private MyUserControl myUserControl1;
public Form1()
{
InitializeComponent();
this.myUserControl1.Location = new System.Drawing.Point(12, 12);
this.myUserControl1.Name = "myUserControl1";
this.myUserControl1.Size = new System.Drawing.Size(433, 57);
this.myUserControl1.TabIndex = 0;
}
}
}
Upvotes: 0
Reputation: 3800
public SubjectRow (Form f, int pointX=100, int pointY=300)
{
counter++;
subjectBox.Location = new Point(pointX, pointY);
f.Controls.Add(subjectBox);
}
SubjectRow test= new SubjectRow (this,100,300); // You can control position of every control
Upvotes: 0
Reputation:
Just a sample quick and dirty:
public partial class SubjectRow : UserControl
{
private ComboBox comboBox1;
private Label label1;
private TextBox textBox1;
private TextBox textBox2;
private TextBox textBox3;
private TextBox textBox4;
private TextBox textBox5;
public SubjectRow()
{
InitializeComponent();
comboBox1 = new ComboBox();
label1 = new Label();
textBox1 = new TextBox();
textBox2 = new TextBox();
textBox3 = new TextBox();
textBox4 = new TextBox();
textBox5 = new TextBox();
Controls.Add(textBox5);
Controls.Add(textBox4);
Controls.Add(textBox3);
Controls.Add(textBox2);
Controls.Add(textBox1);
Controls.Add(label1);
Controls.Add(comboBox1);
}
}
Upvotes: 0
Reputation: 4513
You told that you had a form, so you should add form
parameter to constructor;
public SubjectRow (Form f)
{
counter++;
subjectBox.Location = new Point(100, 300);
f.Controls.Add(subjectBox);
}
And in the form's code behind, create an instance like this.
SubjectRow test= new SubjectRow (this);
Hope helps,
Upvotes: 2