Sathed
Sathed

Reputation: 846

Text property for text box is not displaying its value

I have a list of objects that I'm adding to.

private List<Employee> employees = new List<Employee>();

I have a button on my form application to create a new employee object and add it to the list. Prior to clicking it, it displays "Add Hourly Employee". After clicking it, it changes to "Save Hourly Employee". I'm just using a boolean to determine what text to display.

private void addHrlyEmpBtn_Click(object sender, EventArgs e)
    {
        resetBtn.Enabled = true;
        cancelBtn.Enabled = true;

        if (!addHourly)
        {
            resetBtn.Enabled = true;
            cancelBtn.Enabled = true;
            textBox4.Enabled = false;
            textBox4.Text = (employees.Count + 1).ToString();
            textBox7.Enabled = false;
            addHrlyEmpBtn.Text = "Save Hourly Employee";
        }
        else if (addHourly)
        {
            //Grab values, create new object, and add to list.
            //Set addHourly to false;
        }
        //Other stuff
    }

I'm trying to display employees.Count + 1 to textBox4, but for some reason it isn't working. No text is being displayed at all. Idealy, I'd like to have the text box disabled but still display the value. And I only want it to display when !addHourly.

Am I doing something wrong?

Upvotes: 0

Views: 2469

Answers (2)

Jonny
Jonny

Reputation: 2927

Check if addHrlyEmpBtn_Click is being called at all.

if it isn't try associating the Click event with addHrlyEmpBtn_Click method from the designer

or add addHrlyEmpBtn.Click += addHrlyEmpBtn_Click; in the constructor

Upvotes: 1

Eric J.
Eric J.

Reputation: 150108

There's nothing wrong in principal with the code you wrote.

I would strongly suggest giving meaningful names to all of your variables. Names like textBox4 are likely to cause confusion for yourself and future maintainers of the code.

If the value is not changing as you expect, you are most likely not entering that if branch.

Set a breakpoint at

if (!addHourly)

See if addHourly has the value you expect.

Upvotes: 3

Related Questions