c#中的变量和文本框映射,不使用textbox changed事件
我正在尝试一个代码,当映射到它的变量相应地改变时,它会改变c#中的变量和文本框映射,不使用textbox changed事件,c#,C#,我正在尝试一个代码,当映射到它的变量相应地改变时,它会改变TextBox值,而不使用TextBoxchanged事件。我找不到从哪里开始的线索,请帮帮我 代码如下: public void varChange(TextBox text) { String name; name="sachin"; text.Text = name; MessageBox.Show("" + text.Text); } 您可以“扩展”文本框: public class MeTe
TextBox
值,而不使用TextBox
changed事件。我找不到从哪里开始的线索,请帮帮我
代码如下:
public void varChange(TextBox text)
{
String name;
name="sachin";
text.Text = name;
MessageBox.Show("" + text.Text);
}
您可以“扩展”文本框
:
public class MeTextBox : TextBox
{
public override string Text
{
get
{
return base.Text;
}
set
{
//base.Text = value; // use it or not .. whatever
MyTextWasChanged();
}
}
void MyTextWasChanged()
{
String name;
name="sachin";
//text.Text = name;
base.Text = name;
MessageBox.Show("" + text.Text);
}
}
如果这不是您想要的,那么请提供更多详细信息,我将更新此答案。您可以使用BindingSource
public partial class Form1 : Form
{
private System.Windows.Forms.BindingSource form1BindingSource;
public string BindedProp { get; set; } //Variable or property binded with TextBox
public Form1()
{
InitializeComponent();
this.form1BindingSource = new System.Windows.Forms.BindingSource(new System.ComponentModel.Container());
this.form1BindingSource.DataSource = typeof(binding.Form1);
this.textBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.form1BindingSource, "BindedProp", true));
this.form1BindingSource.DataSource = this;
}
//add a button control to assing value code event click
private void btAssingValueProperty_Click(object sender, EventArgs e)
{
BindedProp = "Value assigned";
form1BindingSource.ResetBindings(false);
}
//add a other button control to show value code event click
private void btShowValueProperty_Click(object sender, EventArgs e)
{
MessageBox.Show(BindedProp);
}
}
我假设您使用的是WinForms&不是WPF?您能更具体一点吗?我通常会说event,但如果您不使用它,为什么不使用一个100毫秒间隔的计时器来设置变量呢?