簡體   English   中英

c#中的變量和文本框映射而不使用文本框更改事件

[英]variable and textbox mapping in c# without using textbox changed event

我正在嘗試一個代碼,當與之映射的變量相應更改時,它更改了TextBox值,而沒有使用TextBox更改事件。 我找不到從哪里開始的任何線索,請幫助我。

這是代碼:

public void varChange(TextBox text)
{
    String name;
    name="sachin";
    text.Text = name;
    MessageBox.Show("" + text.Text);  
}

您可以“擴展” TextBox

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);
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM