【问题标题】:Why is my button state inconsistant?为什么我的按钮状态不一致?
【发布时间】:2015-10-08 12:39:42
【问题描述】:

在我的应用程序中,我有 3 种方式来浏览本地数据:

  • 简单的文件浏览器 (OpenFileControlUI)
  • 最近打开的文件 (RecentlyOpenedFilesUI)
  • 带过滤器的文件浏览器 (FileFilterControlUI)

论文 3 控件继承自基类:

  • OpenFileControlUI 继承自 OpenFileControl
  • RecentlyOpenedFilesUI 继承自 RecentlyOpenedFiles
  • FileFilterControlUI来自FileFilterControl

这 3 个论文继承自同一个基础:DataBrowser。这个类有一个工具条(toolStrip1),它有一个加载数据的按钮(loadToolStripButton)。每次我的应用程序的阅读器很忙时,我都想禁用此按钮,我喜欢这样做:

protected DataBrowser()
{
    InitializeComponent();

    OpenFiles.Instance.ReaderStatus += ReaderStatus;
    //...
}
//...
protected void ReaderStatus(ReadStatusEventArgs e)
{
    loadToolStripButton.Enabled = (e.Status == ReadStatus.Ready);
}

事件被正常触发,按钮在OpenFileControlUIFileFilterControlUI中正常启用/禁用,但RecentlyOpenedFilesUI按钮未禁用而调试器告诉我它禁用了。


我添加了一个 MessageBox 来显示项目状态,使用: MessageBox.Show("The button is :" + (loadToolStripButton.Enabled ? "Enabled" : "Disabled"));

这是两个工作控件的结果:

这是不工作的(你可以看到按钮没有被禁用):


让我烦恼的是,这是:

  • 整个项目中唯一改变此按钮的启用/禁用的行;
  • 如果我在 Debug/Release 中构建也没关系;
  • 我已经用 VS 清理了项目并手动删除了文件;
  • 添加Refresh 呼叫没有帮助。

它不能修复这个错误。

简单的问题:我该如何解决这个问题?

编辑

这就是我所说的:

protected void ReaderStatus(ReadStatusEventArgs e)
{
    loadToolStripButton.Enabled = (e.Status == ReadStatus.Ready);
    MessageBox.Show("The button is :" + (loadToolStripButton.Enabled ? "Enabled" : "Disabled"));
}

这是DataBrowser.Designer.cs 文件(我只保留了这个愚蠢按钮的一部分):

partial class DataBrowser
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    
    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DataBrowser));
        this.toolStrip1 = new System.Windows.Forms.ToolStrip();
        this.loadToolStripButton = new System.Windows.Forms.ToolStripButton();
        this.toolStrip1.SuspendLayout();
        this.SuspendLayout();
        // 
        // toolStrip1
        // 
        this.toolStrip1.CanOverflow = false;
        this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
        this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
        this.loadToolStripButton});
        this.toolStrip1.Location = new System.Drawing.Point(0, 0);
        this.toolStrip1.Name = "toolStrip1";
        this.toolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
        this.toolStrip1.Size = new System.Drawing.Size(216, 25);
        this.toolStrip1.TabIndex = 4;
        this.toolStrip1.Text = "toolStrip1";
        // 
        // loadToolStripButton
        // 
        this.loadToolStripButton.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
        this.loadToolStripButton.Image = global::Sakura.UI.DataBrowsing.Properties.Resources.FormRunHS;
        this.loadToolStripButton.ImageTransparentColor = System.Drawing.Color.Magenta;
        this.loadToolStripButton.Name = "loadToolStripButton";
        this.loadToolStripButton.Size = new System.Drawing.Size(53, 22);
        this.loadToolStripButton.Text = "Load";
        this.loadToolStripButton.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
        this.loadToolStripButton.TextImageRelation = System.Windows.Forms.TextImageRelation.TextBeforeImage;
        this.loadToolStripButton.Click += new System.EventHandler(this.loadToolStripButton_Click);
        // 
        // DataBrowser
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.toolStrip1);
        this.Name = "DataBrowser";
        this.toolStrip1.ResumeLayout(false);
        this.toolStrip1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    protected System.Windows.Forms.ToolStrip toolStrip1;
    protected System.Windows.Forms.ToolStripButton loadToolStripButton;

}

【问题讨论】:

  • 可能不相关:即使在构造函数中,您也正在注册单例。您是否在处置该事件时取消注册?
  • 否则,在 ReaderStatus 事件上添加断点或打印,以查看是否在 eventargs 中以不同的状态调用它。
  • @Rotem 不,我不知道,对于 3 个控件。我是不是该 ?另外,考虑编辑;)
  • 这会是更新问题吗?如果在将Enabled 设置为false 后添加loadToolStripButton.Refresh(),会发生什么情况?我认为目前的问题会引起猜测,我知道这是一个复杂的情况,但请尝试考虑可能有用的其他信息。如果您说问题仅出在其中一个控件上,我们需要知道它与工作控件之间的区别是什么。
  • 显示您在哪里呼叫 hte 消息框。

标签: c# winforms user-interface inheritance


【解决方案1】:

好的,我做了大扫除:

  1. 右键Solution => Clean Solution;
  2. 杀死VS;
  3. 从我的首选控制台中使用find . -iname "bin" -type d -print0|xargs -0 rm -r --find . -iname "obj" -type d -print0|xargs -0 rm -r -- 删除了所有愚蠢的文件;
  4. 关闭计算机;
  5. 又喝了一杯咖啡;
  6. 重启 Windows;
  7. 重启VS;
  8. 喜欢工作代码。

旁注:如果VS 开发团队的任何人阅读此内容,请制定清洁解决方案 真正清洁解决方案!! p>

【讨论】:

  • 可能Closing VS + Deleting bin and obj 就足够了,但有时Another Coffee 有很大的不同;)
猜你喜欢
  • 2020-06-02
  • 2022-06-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2012-06-04
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多