我已经声明了一个名为// Usuario.cs
public string Message { get; set; }
...
Silabas silabas = new Silabas(this);
// Silabas.cs
public Silabas(Usuario usuario)
{
// Here you can access the usuario.Message
}
的类,其中每个实例都有自己继承自SubjectRow
的控件:
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);
}
}
方法中,您可以看到我尝试使用SubjectRow()
在表单上显示ComboBox subjectBox
。但是,这没有任何作用,表单在运行时仍然是空白的。
我对C#很陌生,但我的理解是每当创建new Point(left, top)
的新实例时都会运行SubjectRow()
方法。那为什么这不起作用?如何在指定位置的表单上显示class SubjectRow
控件?
答案 0 :(得分:2)
你告诉你有一个表单,所以你应该将form
参数添加到构造函数中;
public SubjectRow (Form f)
{
counter++;
subjectBox.Location = new Point(100, 300);
f.Controls.Add(subjectBox);
}
在后面的代码中,创建一个这样的实例。
SubjectRow test= new SubjectRow (this);
希望有所帮助,
答案 1 :(得分:0)
以下是正确的做法
<强> 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;
}
}
在您的表单中,添加用户控件:
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;
}
}
}
答案 2 :(得分:0)
快速而肮脏的样本:
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);
}
}
答案 3 :(得分:0)
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