清除C#表单控制 [英] clearing the C# form contorls

查看:119
本文介绍了清除C#表单控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用C#表单应用程序创建了一个表单.

按下RESET按钮时如何清除单选按钮,组合框以及文本框之类的控件.

I''ve created a form using C# form application.

How to clear the controls like radio buttons, combo boxes along with text boxes when pressing the RESET button.

推荐答案

单选按钮在给定组中应始终至少有一个检查.否则,数据无效.

要回答您的问题,只需枚举控件并清除其中的内容.

Radio buttons should ALWAYS have at least one in a given group checked. Otherwise, the data is invalid.

To answer your question, just enumerate your controls and clear the contents.

private void ClearControls(Control ctrl)
{
    if (ctrl.IsContainer)
    {
        foreach (Control child in ctrl.Children)
        {
            ClearControls(child);
        }
    }
    else
    {
        if (ctrl is TextBox)
        {
            ((TextBox)ctrl).Text = "";
        }
        else if (ctrl is CheckBox)
        {
            ((CheckBox)ctrl).Checked = false;
        }
        else if (ctrl is RadioButton)
        {
            ((RadioButton)ctrl).Checked = false;
        }
    }
}


在涉及所有控件的整个集合时,术语清除"没有严格意义.这没有普遍意义,因为控件不一定是独立的,应检查John正确指出的组中的至少一个单选按钮,等等.

但是在很多情况下,您确实需要一些将所有控件置于某种初始状态的方法.通常,您在显示表单之前先调用它.如果要再次调用它,也可以在重置"按钮中调用它. (尽管这看起来有些奇怪;它的通常名称称为默认值"或类似名称.)在大多数情况下,这不是必需的,但我认为您有某些理由这样做.

我只想指出,在或多或少复杂的应用程序中,此类功能应语义且特定于您的应用程序.原因如下:您通常需要将UI与数据层分开.在这种情况下,您有一些数据模型,该数据模型填充 UI,并且当用户使用该UI编辑任何内容(只需对其进行修改)时,这些数据模型就会被修改.这样,您在整个应用程序,填充和数据更新的语义方法中将具有统一的方式.在这种方法中,初始UI状态的设置没有什么不同:您有一个数据的初始状态实例,而您的重置"是该数据模型的初始实例中UI的填充.

如果您采用这种简单的方法,则可以避免以不同的方式做同样的事情,从而避免很多错误.毕竟,请始终记住最重要的原则之一:不要重复自己(DRY),请参阅 http://en .wikipedia.org/wiki/Don%27t_repeat_yourself [^ ].

—SA
There is no a strict sense to be attributed to the term "clear" when it comes to a whole set of all controls. It makes no general sense because controls are not necessarily independent, at least one radio button in a group, as John correctly pointed out, should be checked, etc.

But in many cases, you really need some method of putting all the controls in some initial state. Normally, you call it before a form is shown. If you want to call it again, you can also call it in your "Reset" buttons. (This looks a bit strange though; the usual name for it is called "Defaults" or something like that.) In most cases this is not needed, but I presume you have some reasons to do it.

I just want to point out, that in any more or less complex application such function should be semantic and specific to your application. Here is why: you usually need to separate your UI from a data layer. In this case, you have some data model which populates UI and is modified when a user edits anything (just modifies) using the UI. This way, you have a uniform throughout the application, semantic methods of population and data update. In this approach, the setting up of initial UI state is no different: you have an initial-state instance of data and your "reset" is the population of the UI from this initial instance of the data model.

If you follow this simple approach, you can avoid doing the same thing in different ways and thus avoid many mistakes. After all, always remember one of the most important principles: Don''t Repeat Yourself (DRY), see http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

—SA


这篇关于清除C#表单控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆