我要在WinForm程序中调用一个方法中声明的一些控件
但是总是提示【未将对象引用设置到对象的实例。】
是不是我的思路有问题啊,请高手指点!winform代码:
private void Form1_Load(object sender, EventArgs e)
{
Class1 c1 = new Class1();
c1.Component();
this.Controls.Add(c1.textBox2);
}
被调用绘制控件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
class Class1
{
private System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.TextBox textBox2;
public System.Windows.Forms.Panel panel1; public void Component()
{
//
// panel1
//
//【未将对象引用设置到对象的实例。】 this.panel1.Location = new System.Drawing.Point(13, 85);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// textBox1
//
this.textBox1 = new System.Windows.Forms.TextBox();
//this.textBox1.Location = new System.Drawing.Point(40, 40);
//this.button1.Margin = new System.Windows.Forms.Padding(100);
this.textBox1.Name = "textBox2";
this.textBox1.Size = new System.Drawing.Size(100, 21);
//this.Controls.Add(textBox1);
//
// textBox2
//
this.textBox2 = new System.Windows.Forms.TextBox();
//this.textBox2.Location = new System.Drawing.Point(40, 40);
//this.button1.Margin = new System.Windows.Forms.Padding(100);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
//this.Controls.Add(textBox2); panel1.Controls.Add(textBox1);
panel1.Controls.Add(textBox2);
}
}
}
但是总是提示【未将对象引用设置到对象的实例。】
是不是我的思路有问题啊,请高手指点!winform代码:
private void Form1_Load(object sender, EventArgs e)
{
Class1 c1 = new Class1();
c1.Component();
this.Controls.Add(c1.textBox2);
}
被调用绘制控件代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
class Class1
{
private System.Windows.Forms.TextBox textBox1;
public System.Windows.Forms.TextBox textBox2;
public System.Windows.Forms.Panel panel1; public void Component()
{
//
// panel1
//
//【未将对象引用设置到对象的实例。】 this.panel1.Location = new System.Drawing.Point(13, 85);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// textBox1
//
this.textBox1 = new System.Windows.Forms.TextBox();
//this.textBox1.Location = new System.Drawing.Point(40, 40);
//this.button1.Margin = new System.Windows.Forms.Padding(100);
this.textBox1.Name = "textBox2";
this.textBox1.Size = new System.Drawing.Size(100, 21);
//this.Controls.Add(textBox1);
//
// textBox2
//
this.textBox2 = new System.Windows.Forms.TextBox();
//this.textBox2.Location = new System.Drawing.Point(40, 40);
//this.button1.Margin = new System.Windows.Forms.Padding(100);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
//this.Controls.Add(textBox2); panel1.Controls.Add(textBox1);
panel1.Controls.Add(textBox2);
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货