一个自定义控件,不防设这个自定义控件为MyText,其中只有一个文本框
然后建了个测试程序,在窗体中添加了一个MyText控件,一个标准的Text控件和一个button.
想实现的效果是:在MyText控件中输入几个字符,点击button,将MyText中的字符显示在Text中public class MyText:UserControl
{
private string uText;
//文本属性
public string UText
{
get
{
return uText;
}
set
{
uText=value;
textBox1.Text=uText;
}
}
}//测试程序
public void button1_Click(object sender, EventArgs e)
{
textBox1.Text=myText1.UText;
//********
//myText1.Text="求大侠帮助";
//textBox1.Text=myText1.UText;
}
但是结果并没有将MyText中的值显示在textBox1中.如果去掉下面的注释,则能够显示出来"求大侠帮助"这几个字.请各位高手解释下这是为什么,应该怎么解决.谢谢
然后建了个测试程序,在窗体中添加了一个MyText控件,一个标准的Text控件和一个button.
想实现的效果是:在MyText控件中输入几个字符,点击button,将MyText中的字符显示在Text中public class MyText:UserControl
{
private string uText;
//文本属性
public string UText
{
get
{
return uText;
}
set
{
uText=value;
textBox1.Text=uText;
}
}
}//测试程序
public void button1_Click(object sender, EventArgs e)
{
textBox1.Text=myText1.UText;
//********
//myText1.Text="求大侠帮助";
//textBox1.Text=myText1.UText;
}
但是结果并没有将MyText中的值显示在textBox1中.如果去掉下面的注释,则能够显示出来"求大侠帮助"这几个字.请各位高手解释下这是为什么,应该怎么解决.谢谢
{
private string uText; //uText并没有赋值,他原本就是String.Empty
//文本属性
public string UText
{
get
{
return uText;
}
set
{
uText=value;
textBox1.Text=uText;
}
}
} //测试程序
public void button1_Click(object sender, EventArgs e)
{
textBox1.Text=myText1.UText; //这里取UText默认值,而默认值是String.Empty,textBox1.Text就是String.Empty,你看上去像是啥也没有,实际他是String.Empty
//********
//myText1.Text="求大侠帮助"; //给UText赋值“求大侠帮助”
//textBox1.Text=myText1.UText;//取uText值,因为已赋值为"求大侠帮助",所以这里自然显示“求大侠帮助”
}
public class MyText:UserControl
{
private string uText;
//文本属性
public string UText
{
get
{
return uText;
}
set
{
uText=value;
textBox1.Text=uText;//这个textBox1是自定义控件的textbox
}
}
}//测试程序
public void button1_Click(object sender, EventArgs e)
{
//这里的textBox1是form的textBox
textBox1.Text=myText1.UText;
//********
//myText1.Text="求大侠帮助";
//textBox1.Text=myText1.UText;
}
textBox1.Text=myText1.UText; //这里取UText默认值,而默认值是String.Empty,
这里的UText的值应该是null吧
public class MyText:UserControl
{
private string uText="求大侠帮助";
//文本属性
public string UText
{
get
{
return uText;
}
set
{
uText=value;
textBox1.Text=uText;
}
}
} 在测试程序中,
现在MyText控件中输入几个字符,比如"大家好",然后点击button,
在textBox1的值还是"求大侠帮助",而不是"大家好"?
public class MyText:UserControl
{
public string UText
{
get
{
return textBox1.Text;
}
}
}