简体   繁体   中英

variable and textbox mapping in c# without using textbox changed event

I'm trying a code which changes the TextBox values when variable mapped with it changes accordingly without using TextBox changed event. I am not finding any clue to where to start please help me.

Here is the code:

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

You can "extend" 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);  
    }
}

If that's not what you're looking for then give some more details and I'll update this answer.

You can use a 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);
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM