C# 重置表单中的所有项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15569641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reset all the items in a form
提问by Yuki Kutsuya
I was wondering, is there a way I can reset all the checkboxes, textboxes, numerics and other controls back to the default values without writing code for every control individually? This is the code I've tried, but doesn't seem to work:
我想知道,有没有一种方法可以将所有复选框、文本框、数字和其他控件重置回默认值,而无需为每个控件单独编写代码?这是我试过的代码,但似乎不起作用:
for (int i = 0; i < this.Controls.Count; i++)
{
this.Controls[i].ResetText();
}
EDIT:
I've fixed it by manually setting the control values, sorry for all the trouble >.<.
编辑:
我已经通过手动设置控制值来修复它,很抱歉所有的麻烦>.<。
回答by Pranay Rana
Do as below create class and call it like this
做如下创建类并像这样调用它
Check : Reset all Controls (Textbox, ComboBox, CheckBox, ListBox) in a Windows Form using C#
检查:使用 C# 重置 Windows 窗体中的所有控件(文本框、组合框、复选框、列表框)
private void button1_Click(object sender, EventArgs e)
{
Utilities.ResetAllControls(this);
}
public class Utilities
{
public static void ResetAllControls(Control form)
{
foreach (Control control in form.Controls)
{
if (control is TextBox)
{
TextBox textBox = (TextBox)control;
textBox.Text = null;
}
if (control is ComboBox)
{
ComboBox comboBox = (ComboBox)control;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
}
if (control is CheckBox)
{
CheckBox checkBox = (CheckBox)control;
checkBox.Checked = false;
}
if (control is ListBox)
{
ListBox listBox = (ListBox)control;
listBox.ClearSelected();
}
}
}
}
回答by Tomtom
You can reset all controls of a certain type. Something like
您可以重置某种类型的所有控件。就像是
foreach(TextBox tb in this.Controls.OfType<TextBox>().ToArray())
{
tb.Clear();
}
But you can't reset all controls at once
但是您不能一次重置所有控件
回答by ispiro
Quick answer, maybe it'll help:
快速回答,也许会有所帮助:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
while (f2.DialogResult == DialogResult.Retry)
{
f2 = new Form2();
f2.ShowDialog();
}
}
and in Form2 (The 'settings' Form):
并在 Form2(“设置”表单)中:
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.OK;
Close();
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Retry;
Close();
}
回答by LoftyWofty
I recently had to do this for Textboxes and Checkboxes but using JavaScript ...
我最近不得不为文本框和复选框执行此操作,但使用 JavaScript ...
How to reset textbox and checkbox controls in an ASP.net document
Here is the code ...
这是代码...
<script src="http://code.jquery.com/jquery-1.7.1.js" type="text/javascript"></script>
<script type="text/javascript">
function ResetForm() {
//get the all the Input type elements in the document
var AllInputsElements = document.getElementsByTagName('input');
var TotalInputs = AllInputsElements.length;
//we have to find the checkboxes and uncheck them
//note: <asp:checkbox renders to <input type="checkbox" after compiling, which is why we use 'input' above
for(var i=0;i< TotalInputs ; i++ )
{
if(AllInputsElements[i].type =='checkbox')
{
AllInputsElements[i].checked = false;
}
}
//reset all textbox controls
$('input[type=text], textarea').val('');
Page_ClientValidateReset();
return false;
}
//This function resets all the validation controls so that they don't "fire" up
//during a post-back.
function Page_ClientValidateReset() {
if (typeof (Page_Validators) != "undefined") {
for (var i = 0; i < Page_Validators.length; i++) {
var validator = Page_Validators[i];
validator.isvalid = true;
ValidatorUpdateDisplay(validator);
}
}
}
</script>
And call it with a button or any other method ...
并用按钮或任何其他方法调用它......
<asp:button id="btnRESET" runat="server" onclientclick="return ResetForm();" text="RESET" width="100px"></asp:button>
If you don't use ValidationControls on your website, just remove all the code refering to it above and the call Page_ClientValidateReset();
如果您不在您的网站上使用 ValidationControls,只需删除上面引用它的所有代码和调用 Page_ClientValidateReset();
I am sure you can expand it for any other control using the DOM. And since there is no post to the server, it's faster and no "flashing" either.
我相信您可以使用 DOM 为任何其他控件扩展它。而且由于没有向服务器发送信息,因此速度更快,也不会“闪烁”。
回答by Teju MB
Additional-> To clear the Child Controls The below function would clear the nested(Child) controls also, wrap up in a class.
附加-> 清除子控件下面的函数也将清除嵌套(子)控件,并封装在一个类中。
public static void ClearControl(Control control)
{
if (control is TextBox)
{
TextBox txtbox = (TextBox)control;
txtbox.Text = string.Empty;
}
else if (control is CheckBox)
{
CheckBox chkbox = (CheckBox)control;
chkbox.Checked = false;
}
else if (control is RadioButton)
{
RadioButton rdbtn = (RadioButton)control;
rdbtn.Checked = false;
}
else if (control is DateTimePicker)
{
DateTimePicker dtp = (DateTimePicker)control;
dtp.Value = DateTime.Now;
}
else if (control is ComboBox)
{
ComboBox cmb = (ComboBox)control;
if (cmb.DataSource != null)
{
cmb.SelectedItem = string.Empty;
cmb.SelectedValue = 0;
}
}
ClearErrors(control);
// repeat for combobox, listbox, checkbox and any other controls you want to clear
if (control.HasChildren)
{
foreach (Control child in control.Controls)
{
ClearControl(child);
}
}
}
回答by Alireza Masali
function setToggleInputsinPnl(pnlName) {
var domCount = pnlName.length;
for (var i = 0; i < domCount; i++) {
if (pnlName[i].type == 'text') {
pnlName[i].value = '';
} else if (pnlName[i].type == 'select-one') {
pnlName[i].value = '';
}
}
}
回答by Angus Chung
You can create the form again and dispose the old one.
您可以再次创建表单并处理旧表单。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnReset_Click(object sender, EventArgs e)
{
Form1 NewForm = new Form1();
NewForm.Show();
this.Dispose(false);
}
}
回答by Mohamed Yunus
foreach (Control field in container.Controls)
{
if (field is TextBox)
((TextBox)field).Clear();
else if (field is ComboBox)
((ComboBox)field).SelectedIndex=0;
else
dgView.DataSource = null;
ClearAllText(field);
}
回答by Andrey Krasilnikov
If you have some panels or groupboxes reset fields should be recursive.
如果您有一些面板或分组框,则重置字段应该是递归的。
public class Utilities
{
public static void ResetAllControls(Control form)
{
foreach (Control control in form.Controls)
{
RecursiveResetForm(control);
}
}
private void RecursiveResetForm(Control control)
{
if (control.HasChildren)
{
foreach (Control subControl in control.Controls)
{
RecursiveResetForm(subControl);
}
}
switch (control.GetType().Name)
{
case "TextBox":
TextBox textBox = (TextBox)control;
textBox.Text = null;
break;
case "ComboBox":
ComboBox comboBox = (ComboBox)control;
if (comboBox.Items.Count > 0)
comboBox.SelectedIndex = 0;
break;
case "CheckBox":
CheckBox checkBox = (CheckBox)control;
checkBox.Checked = false;
break;
case "ListBox":
ListBox listBox = (ListBox)control;
listBox.ClearSelected();
break;
case "NumericUpDown":
NumericUpDown numericUpDown = (NumericUpDown)control;
numericUpDown.Value = 0;
break;
}
}
}
回答by Tharindu Sachin Wanniarachchi
There is a very effective way to use to clear or reset Windows Form C# controls like TextBox, ComboBox, RadioButton, CheckBox, DateTimePicker etc.
有一种非常有效的方法可以用来清除或重置 Windows 窗体 C# 控件,如 TextBox、ComboBox、RadioButton、CheckBox、DateTimePicker 等。
private void btnClear_Click(object sender, EventArgs e)
{
Utilities.ClearAllControls(this);
}
internal class Utilities
{
internal static void ClearAllControls(Control control)
{
foreach (Control c in control.Controls)
{
if (c is TextBox)
{
((TextBox)c).Clear();
}
else if (c is ComboBox)
{
((ComboBox)c).SelectedIndex = -1;
}
else if (c is RadioButton)
{
((RadioButton)c).Checked = false;
}
else if (c is CheckBox)
{
((CheckBox)c).Checked = false;
}
else if (c is DateTimePicker)
{
((DateTimePicker)c).Value = DateTime.Now; // or null
}
}
}
}
To accomplish this with overall user experience in c# we can use one statement to clear them. Pretty straight forward so far, above is the code.
为了在 c# 中以整体用户体验来实现这一点,我们可以使用一个语句来清除它们。到目前为止非常简单,上面是代码。