【问题标题】:C# equivalent of Delphi's DisableControls/EnableControlsC# 相当于 Delphi 的 DisableControls/EnableControls
【发布时间】:2011-04-23 22:12:19
【问题描述】:

Delphi 的DisableControls/EnableControls 方法(用于在遍历基础数据集时禁用数据绑定控件的更新)的 C# 等效项是什么? google了半个小时也没找到答案……

我有一个列表框和一个绑定到绑定源的富编辑框,但我需要执行一个遍历整个数据集的操作,并且当我在底层数据集移动时,这两个控件都会更新。在 Delphi 中,这很简单:将在 DisableControlsEnableControls 之间进行迭代的块括起来。我找不到 C#/.NET 等价物,而且我真的很努力!

【问题讨论】:

  • 你在用什么? WinForms 还是 WPF?
  • 您是否尝试将绑定源的RaiseListChangedEvents 属性设置为false

标签: c# delphi dataset


【解决方案1】:

IIRC,将 Enabled 设置为 false 不会阻止控件对 WinForms 中的数据更改做出反应。

ListBox 这样的集合绑定控件通常具有方法BeginUpdate()EndUpdate(),它们会暂时禁用视觉更新。

另外,DarkSquirrel 提到的属性可能值得一看

【讨论】:

    【解决方案2】:

    我现在无法访问 Visual Studio,因此无法对此进行测试,但请查看控件实例的方法。代码如:

    // set the Enabled property of 
    // the controls to False; this should
    // disable the controls for user access
    
    listBox.Enabled = False;  
    richEditBox.Enabled = False;  
    
    // perform iteration  
    // and other operations
    
    // set the Enabled property back 
    // to True  
    
    listBox.Enabled = True;  
    richEditBox.Enabled = True;  
    

    该属性的确切名称可能略有不同,但我很确定它就是这样。

    【讨论】:

    • 您也可以通过this.Enabled = false 禁用整个表单,甚至将控件放在GroupBox 中,然后this.myGroupBox.Enabled = false 将禁用该组框内的所有控件;编辑:假设您在表单代码中,而不是外部方法
    【解决方案3】:

    我假设您使用的是 WinForms,在这种情况下您可以尝试使用方法SuspendLayout/ResumeLayout

    来自 MSDN 的代码示例:

    private void AddButtons()
    {
       // Suspend the form layout and add two buttons.
       this.SuspendLayout();
       Button buttonOK = new Button();
       buttonOK.Location = new Point(10, 10);
       buttonOK.Size = new Size(75, 25);
       buttonOK.Text = "OK";
    
       Button buttonCancel = new Button();
       buttonCancel.Location = new Point(90, 10);
       buttonCancel.Size = new Size(75, 25);
       buttonCancel.Text = "Cancel";
    
       this.Controls.AddRange(new Control[]{buttonOK, buttonCancel});
       this.ResumeLayout();
    }
    

    【讨论】:

      【解决方案4】:

      到目前为止,我知道,您不需要在 C# 中禁用/启用控件,因为这种类型的 DataSet 不适用于当前光标,例如 Delphi TDataSets。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 2011-02-04
        相关资源
        最近更新 更多