Update Text linked to a Databinding

Asked

Viewed 445 times

4

I have the following property linked to a label, where the value of this property appears in the text field. Like this:

 this.labelPedidosHoje.DataBindings.Add("Text", pedidosIntegrados, "PedidosIntegradosModelo.PedidosIntegradosDia",true, DataSourceUpdateMode.OnPropertyChanged);

When I start the application the values appear perfectly.

But unfortunately when I change the value of the property the same does not replicate on the screen (text field).

Can anyone tell me how to refresh the text field on the screen to show the updated value ?

Note: 1- When this value is updated I call the Onpropertychanged method to warn that the property value has changed.

2 - When I debug the value of the property, and it is updated. So, I just need to update the text field.

2 answers

2

Here is an example of the solution I found. Thanks ! @Maiconcarraro.

 using System;
 using System.ComponentModel;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;
 using System.Collections;


namespace System_Windows_Forms_UpdateBinding
{
class Form1 : Form
{
    // Declare the objects on the form. 
    private Label label1;
    private Label label2;
    private TextBox textBox1;
    private TextBox textBox2;
    private Button button1;
    private BindingSource bindingSource1;
    ArrayList states;

    public Form1()
    {
        // Basic form setup. 
        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.label2 = new System.Windows.Forms.Label();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.button1.Location = new System.Drawing.Point(12, 18);
        this.button1.Size = new System.Drawing.Size(119, 38);
        this.button1.Text = "RemoveAt(0)";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.textBox1.Location = new System.Drawing.Point(55, 75);
        this.textBox1.ReadOnly = true;
        this.textBox1.Size = new System.Drawing.Size(119, 20);
        this.label1.Location = new System.Drawing.Point(12, 110);
        this.label1.Size = new System.Drawing.Size(43, 14);
        this.label1.Text = "Capital:";
        this.label2.Location = new System.Drawing.Point(12, 78);
        this.label2.Size = new System.Drawing.Size(34, 14);
        this.label2.Text = "State:";
        this.textBox2.Location = new System.Drawing.Point(55, 110);
        this.textBox2.Size = new System.Drawing.Size(119, 20);
        this.textBox2.ReadOnly = true;
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.Add(this.textBox2);
        this.Controls.Add(this.label2);
        this.Controls.Add(this.label1);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.button1);
        this.Text = "Form1";

        // Create an ArrayList containing some of the State objects.
        states = new ArrayList();
        states.Add(new State("California", "Sacramento"));
        states.Add(new State("Oregon", "Salem"));
        states.Add(new State("Washington", "Olympia"));
        states.Add(new State("Idaho", "Boise"));
        states.Add(new State("Utah", "Salt Lake City"));
        states.Add(new State("Hawaii", "Honolulu"));
        states.Add(new State("Colorado", "Denver"));
        states.Add(new State("Montana", "Helena"));

        bindingSource1 = new BindingSource();

        // Bind BindingSource1 to the list of states.
        bindingSource1.DataSource = states;

        // Bind the two text boxes to properties of State.
        textBox1.DataBindings.Add("Text", bindingSource1, "Name");
        textBox2.DataBindings.Add("Text", bindingSource1, "Capital");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // If items remain in the list, remove the first item.  
        if (states.Count > 0)
        {
            states.RemoveAt(0);

            // Call ResetBindings to update the textboxes.
            bindingSource1.ResetBindings(false);
        }
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());

    }

    // The State class to add to the ArrayList. 
    private class State
    {
        private string stateName;
        public string Name 
        {
            get {return stateName;}
        }

        private string stateCapital;
        public string Capital 
        {
            get {return stateCapital;}
        }

        public State ( string name, string capital)
        {
            stateName = name;
            stateCapital = capital;
        }
    }

}

}

1

DataBindings has no effect as long as the control does not lose focus, if you want to force its action can do something like:

this.labelPedidosHoje.DataBindings.Add("Text", pedidosIntegrados, "PedidosIntegradosModelo.PedidosIntegradosDia",true, DataSourceUpdateMode.OnPropertyChanged);
this.labelPedidosHoje.DataBindings[0].WriteValue(); // força

Assuming you only add a single Binding, so the zero.

  • So this control doesn’t update the value, but the others I have in my application do. Is there any more elegant way to do this ?

  • Dude, I got it. Here’s the link for anyone with the same problem: https://msdn.microsoft.com/en-us/library/f61k6akt(v=vs.110).aspx. Thank you. !

  • 1

    @Emersonbrito Converts your link into an answer here because even if the link is out of the air people can see the solution here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.