简体   繁体   English

C#相当于Delphi的DisableControls / EnableControls

[英]C# equivalent of Delphi's DisableControls/EnableControls

What is the C# equivalent of Delphi's DisableControls / EnableControls methods (used to disable updating of databound controls while iterating through the underlying dataset)? 什么是Delphi的DisableControls / EnableControls方法的C#等价物(用于在迭代底层数据集时禁用数据绑定控件的更新)? I have googled for half an hour and did not find an answer... 我用谷歌搜索了半个小时,没有找到答案......

I have a list box and a rich edit box bound to a binding source, but I need to do an operation that iterates through the entire dataset, and both controls get updated as I move through the underlying dataset. 我有一个列表框和一个绑定到绑定源的丰富编辑框,但我需要执行一个迭代整个数据集的操作,并且当我在底层数据集中移动时,两个控件都会更新。 In Delphi this is easy enough: enclose the block that does the iteration between DisableControls and EnableControls . 在Delphi中,这很容易:将在DisableControlsEnableControls之间进行迭代的块包含EnableControls I can't find the C#/.NET equivalent, and I have looked really hard! 我找不到C#/ .NET等价物,我看起来真的很难!

IIRC, setting Enabled to false does not prevent the controls from reacting to data changes in WinForms. IIRC,将Enabled设置为false不会阻止控件对WinForms中的数据更改做出反应。

Collection-bound controls like the ListBox typically have methods BeginUpdate() and EndUpdate() which temporarily disable visual updates. ListBox这样的集合绑定控件通常具有BeginUpdate()EndUpdate() ,这些方法暂时禁用了可视更新。

Also, the property mentioned by DarkSquirrel might be worth a look 此外,DarkSquirrel提到的财产可能值得一看

I don't have access to Visual Studio right now, so I can't test this, but look through the methods for the control instance. 我现在无权访问Visual Studio,所以我无法对此进行测试,但请查看控件实例的方法。 Code such as: 代码如:

// 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;  

The exact name of the property may differ slightly, but I'm pretty sure that this is what it is. 该属性的确切名称可能略有不同,但我很确定这就是它的本质。

到目前为止,我知道,你不需要在C#中使用Disiable / EnableControls,因为这种类型的DataSet不适用于当前游标,如Delphi TDataSets。

I assume you are using WinForms, in that case you can try using the methods SuspendLayout / ResumeLayout . 我假设您使用的是WinForms,在这种情况下,您可以尝试使用SuspendLayout / ResumeLayout方法。

Code sample from MSDN: 来自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();
}

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

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