简体   繁体   中英

Windows Form Controls Aren't Updated by Code Behind

I'm having an issue with a certain form in my Windows form project. Only one form is having problems; there are several others that use the same type of referencing which do not have this problem.

The issue is that modifying the controls through the code behind does not actually update them on the form. Anything from adding values pulled from a database to a combo box's Items collection (through the .Items.Add() method), to setting a text box or check box to be enabled or not, to setting the text for a text box or the checked status of a check box... nothing works. I've tried commenting out all the code in all methods except for the code to actually set the enabled status for some controls, to make sure that nothing else is running without me knowing, but the issue still occurs.

The code to change the enabled status is definitely hit, as the debugger shows, and the Enabled status is set correctly, whether true or false depending on conditions. The controls that should be disabled are still editable and still look like they are editable. The code was originally written using the intellisense to automatically fill in the control names, so there shouldn't be any issues with linking from the code behind to the correct control on the form. I've also tried making the form larger and dropping in a blank Panel, with a new text box and check box on it, and setting the panel to be enabled or disabled by the same method, and when it ran the controls were also not changed by the code behind.

Any suggestions on what I could be missing? This is such basic functionality of Windows forms that I have no idea what could be the problem, and I couldn't find any similar issues by Googling or searching this site.

Thanks!

EDIT: Here's a code sample of a piece that should be working but it isn't. Similar code is used on a few other forms without issue.

When a checkbox called chkDisable is checked, this event runs:

    private void chkDisable_CheckedChanged(object sender, EventArgs e)
    {
        SetControlsEnabledStatus(!((CheckBox)sender).Checked);
    }

    private void SetControlsEnabledStatus(bool enabledStatus)
    {
        textBox1.Enabled = enabledStatus;
        textBox2.Enabled = enabledStatus;
    }

I'm 100% sure that this event is wired correctly, that it fires correctly, and that the textbox's Enabled statuses are set correctly. No other method has uncommented code that could overwrite this. But, as soon as the form loads again after clicking the checkbox, the textboxes are always enabled.

EDIT2: In case anyone comes across this for their own problems, here is what was causing my problem:

I had to start from scratch on a new form to finally get this tracked down. It turned out that there was a call to InitializeComponent() in both the constructor and the FormLoad event. Basically, I created a new form with the same controls on it, with only the Disable event running, and when I copied over the Load code, the Disable stopped working.

Try cleaning your solution and rebuilding all to start. (:

If that doesn't work, can you post some code?

Edit:

Okay, with no code to look at here,... I'm going to offer some basic advice.

Start putting MessageBox.Show("I executed!") throughout your form. Put it in the constructor / initialization event. Put it in the click event, etc. It's possible that you're handling an exception somewhere and the code to update the forms never gets a chance to execute.

Obviously there's something wrong, and until we can rule out that the problem exists in your code, we're going to assume it's there. If you post your code, we can copy it and paste it into our own project and see how it behaves. If it works, then we can confirm that the code is good, and the problem must exist somewhere else.

Hope that helps a little. (:

Edit:

Alright, using the code you provided, I created a sandbox project:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinFormsSandbox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void chkDisable_CheckedChanged(object sender, EventArgs e)
        {
            SetControlsEnabledStatus(!((CheckBox)sender).Checked);
        }

        private void SetControlsEnabledStatus(bool enabledStatus)
        {
            textBox1.Enabled = enabledStatus;
            textBox2.Enabled = enabledStatus;
        }
    }
}

在此输入图像描述

在此输入图像描述

It works for me. The error must not exist in the code you posted.

Edit:

You know, I hate giving up on finding the root cause of a problem, but if this is a time-sensitive issue, it might be quicker for you to create a new form from scratch and just copy in the functionality. (:

I've had similar issues and they've been helped by adding an OnShown handler to my form, which flips a boolean value that guards the other event handling code. The idea is that something in the initialization process is causing the control's events to fire in ways that you didn't want to happen. It's not ideal, but it might be worth trying as a debugging measure at least.

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