LuckWallace
LuckWallace

Reputation: 133

Update textbox when I do an operation c#

I have a initial screen where I have a textbox that show a name that has been extracted from a key of file app.config.

enter image description here

Initially, the key shows for example 60, and if I do an operation, the key is 3 but I don't know how can I update the text in the textbox that represents that key. This is my code from where I get the value 3:

        string nempresa;
        Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);

        nempresa = config.AppSettings.Settings["EMPRESA"].Value;

        this.textBoxNombreEmpresa.Dock = System.Windows.Forms.DockStyle.Bottom;
        this.textBoxNombreEmpresa.Font = new System.Drawing.Font("Bookman Old Style", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.textBoxNombreEmpresa.ForeColor = System.Drawing.SystemColors.MenuHighlight;
        this.textBoxNombreEmpresa.Location = new System.Drawing.Point(0, 742);
        this.textBoxNombreEmpresa.Multiline = true;
        this.textBoxNombreEmpresa.Name = "textBoxNombreEmpresa";
        this.textBoxNombreEmpresa.Size = new System.Drawing.Size(1548, 68);
        this.textBoxNombreEmpresa.TabIndex = 3;
        this.textBoxNombreEmpresa.Text = nempresa;
        this.textBoxNombreEmpresa.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;

Any idea?

Upvotes: 1

Views: 322

Answers (1)

Mong Zhu
Mong Zhu

Reputation: 23732

1) make nempresa to a property:

string nempresa;

public string Nempresa
{
    get { return nempresa; }
    set { nempresa = value; }
}

2) make a method UpdateTextBoxNombreEmpresa(string value):

void UpdateTextBoxNombreEmpresa(string value)
{
    // put whatever code you want to be executed that is associated
    // with the update in here, (styling, position, size, ect...)
    this.textBoxNombreEmpresa.Text = value;
}

3) call the method in the setter:

public string Nempresa
{
    get { return nempresa; }
    set 
    {
        nempresa = value; 
        UpdateTextBoxNombreEmpresa(nempresa);
    }
}

4) use the property instead of the field:

this.Nempresa = whatEverYourValueIs;

EDIT: This line has to be of course after the initialization of the textbox!

Upvotes: 2

Related Questions