小弟初学c#,想请教如何创建自定义控件,最好能给给实例。还有帮我做做下面这道题。下面的代码片断中那一段可以定义一个自定义控件使得该控件包含一个TextBox和Button控件 ________。
A.
public class MyControl : UserControl
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
B.
public class MyContorl : UserControl
{
public MyContorl()
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
}
C.
public class MyContorl : TextBox,Button
{
public MyContorl()
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
}
D.
public class MyControl : TextBox,Button
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
A.
public class MyControl : UserControl
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
B.
public class MyContorl : UserControl
{
public MyContorl()
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
}
C.
public class MyContorl : TextBox,Button
{
public MyContorl()
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
}
D.
public class MyControl : TextBox,Button
{
TextBox txtBox = new TextBox();
Button btn = new Button();
this.Controls.Add(txtBox);
this.Controls.Add(btn);
}
http://www.cnblogs.com/badboy2008/articles/641371.html
A中this.Controls.Add(txtBox); this.Controls.Add(btn); 这两个语句是方法的使用,试试可以在类中直接调用方法吗?方法必须在方法中才能被调用。
楼主上鬼子当了,这个题目考的不是用户控件,而是考试对类的熟练程度!
我们在C#.NET中所见到的窗实质就是一个类。你可以把一个窗体的两个.CS源文件打开看一下就明白了.比如说Form1窗体,你会在两个CS文件中都能看到这样的定义partial class Form1。 partial是部分的意思,也就是说将Form1这个类分成了两个文件来写。一个是设计,一个是应用。再看看Form1这个类继承了什么
可以排除A、C、D
A就是後面兩句出問題.只能寫在方法或構造函數里
B嘛,你後面兩句加進去后,沒有設定位置,到時控件可能排放出問題,重壘在一起(不過這符合答案)