简体   繁体   English

Windows窗体控件不会被代码隐藏更新

[英]Windows Form Controls Aren't Updated by Code Behind

I'm having an issue with a certain form in my Windows form project. 我的Windows窗体项目中存在某个窗体的问题。 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. 从将数据库中的值添加到组合框的Items集合(通过.Items.Add()方法),设置文本框或复选框以启用或不启用,设置文本框的文本或检查复选框的状态......没有任何作用。 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. 代码最初使用intellisense编写,以自动填充控件名称,因此从代码后面的代码链接到表单上的正确控件不应该有任何问题。 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. 这是Windows窗体的基本功能,我不知道可能是什么问题,我通过谷歌搜索或搜索此网站找不到任何类似的问题。

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: 选中名为chkDisable的复选框时,将运行以下事件:

    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. 我100%确定此事件连接正确,它正确触发,并且文本框的启用状态设置正确。 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: 编辑2:如果有人遇到他们自己的问题,这是导致我的问题的原因:

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. 事实证明,在构造函数和FormLoad事件中都调用了InitializeComponent()。 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. 基本上,我创建了一个具有相同控件的新表单,只运行了Disable事件,当我复制Load代码时,Disable停止工作。

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. 开始在整个表单中放置MessageBox.Show(“我执行!”)。 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. 把它放在click事件等中。你可能在某处处理异常,而更新表单的代码永远不会有机会执行。

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. 我有类似的问题,他们已经通过在我的表单中添加一个OnShown处理程序来帮助它,它会翻转一个保护其他事件处理代码的布尔值。 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. 它并不理想,但至少可能值得尝试作为调试措施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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