简体   繁体   English

控制Windows窗体C#应用程序中的更改

[英]Control changes in a Windows Forms C# application

I have a form with several components, like TextBox and ComboBox , and I need to know when click in the out button if there was any changes in the form. 我有一个包含几个组件的表单,如TextBoxComboBox ,如果表单中有任何更改,我需要知道何时单击out按钮。 Is there a way to do this? 有没有办法做到这一点?

You could create a generic change event handler which sets a flag on change, and then assign all the controls' Change events to it. 您可以创建一个通用的更改事件处理程序,它在更改时设置一个标志,然后将所有控件的Change事件分配给它。

This could probably be done pretty easily by looping through all of your controls onload. 通过循环遍历所有控件onload,这可能很容易完成。

You could loop through all controls but this would have to be recursive because a control can contain controls, eg (no null checks for brevity): 你可以循环遍历所有控件,但这必须是递归的,因为控件可以包含控件,例如(为了简洁,没有空检查):

private void IterateOverControls( Control parent )
{
    ProcessControl( parent );

    foreach( Control control in parent.Controls )
        IterateOverControls( control );
}

In ProcessControl you could hook up event handlers to handle OnEnter (to store the state) and OnLeave (to check the current state against the stored state). 在ProcessControl中,您可以挂钩事件处理程序来处理OnEnter(以存储状态)和OnLeave(以针对存储状态检查当前状态)。 You'd need to unhook all the event handlers when disposing. 处置时,您需要取消挂钩所有事件处理程序。 Also, the code to store check the state would have to change for different control types, eg TextBox would be the Text property, but a radio button would be an index, etc. Obviously this becomes simpler if you can compare form state to your underlying data store state, in which case you can just make the comparison on each OnLeave event. 此外,存储检查状态的代码必须针对不同的控件类型进行更改,例如TextBox将是Text属性,但单选按钮将是索引等。显然,如果您可以将表单状态与底层事件进行比较,这会变得更简单数据存储状态,在这种情况下,您可以只对每个OnLeave事件进行比较。

One thing also to consider is do you need to track real changes? 还有一点需要考虑的是你需要跟踪真正的变化吗? For example, I have 2 radio buttons: A and B. I check B (a change), so the out button or whatever has its Enabled property changes. 例如,我有2个单选按钮:A和B.我检查B(更改),因此out按钮或任何具有Enabled属性的按钮都会更改。 I then click on A (ie back to my original state). 然后我点击A(即回到原来的状态)。 Do you need to revert the button at that point? 你需要在那时恢复按钮吗?

This is why you should look towards a model view controller approach :) 这就是为什么你应该看看模型视图控制器方法:)

The easiest way to do this would be to simply use a variable on the form named something like "IsChanged." 最简单的方法是简单地在名为“IsChanged”的表单上使用变量。 Set it false when the form is initially displayed, and set it true if they make any changes. 在最初显示表单时将其设置为false,如果进行任何更改,则将其设置为true。

Alternately, you could record the values of everything when the form is displayed, and when they finish, check the current values against the old ones to see if anything changed. 或者,您可以在显示表单时记录所有内容的值,并在完成时检查当前值与旧表单的值,以查看是否有任何更改。

If this is already nearly finished, and you need something quick it's probably going to be easier to just always assume that something has changed, then in your update logic afterwards (whatever it's doing) don't update stuff that is still the same. 如果这已经快完成了,你需要一些快速的东西,可能会更容易一直假设某些东西发生了变化,然后在你的更新逻辑中(无论它在做什么)不要更新仍然相同的东西。

As someone else mentioned, it's very possible for someone to change something, then change it back. 正如其他人提到的那样,某人有可能改变某些东西,然后再改回来。 What would you want to do in that case? 那个案子你想做什么? You won't be able to maintain a proper dirty state of the form without a fair bit of additional work.. this is something that you need to plan for before you start, really. 如果没有相当多的额外工作,您将无法保持表单的正确脏状态。这是您在开始之前需要计划的事情,真的。

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

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