C# 清除所有文本框并取消选中 WPF 中的所有复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37739338/
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
C# clear all textboxes and uncheck all checkboxes in WPF
提问by Programmer7
I am trying to figure out how to clear all textboxes and uncheck all checkboxes in a C# WPF. The form has a lot of textboxes and it will get tedious to do .Clear() or = "" for every single textbox. Same thing with checkboxes.
我想弄清楚如何清除所有文本框并取消选中 C# WPF 中的所有复选框。该表单有很多文本框,为每个文本框执行 .Clear() 或 = "" 会变得乏味。复选框也是一样。
I heard it is easy in Windows Forms doing something like below using a foreach loop, but I am doing this on a WPF so I cant get that to work.
我听说在 Windows 窗体中使用 foreach 循环执行类似下面的操作很容易,但是我在 WPF 上执行此操作,因此无法使其正常工作。
foreach (Control c in Controls)
{
if (c is CheckBox)
{
((CheckBox) c).Checked = false;
}
else if (c is TextBox)
{
((TextBox) c).Text = "";
}
}
Anyone have any advice? Thanks
有人有什么建议吗?谢谢
回答by sujith karivelil
Let the whole controls are inside a Container( let it be a Stack panel) like the following:
让整个控件都在一个容器内(让它成为一个堆栈面板),如下所示:
<StackPanel Name="containerCanvas" Margin="0,0,0,191">
<CheckBox Name="chk1" >chk1</CheckBox>
<CheckBox Name="chk2" >chk2</CheckBox>
<CheckBox Name="chk3" >chk3</CheckBox>
<TextBox Name="txt1" Text="xxxxxxxx"></TextBox>
<TextBox Name="txt2" Text="xxxxxxxxx"></TextBox>
<TextBox Name="txt3" Text="xxxxxxxxxx"></TextBox>
</StackPanel>
And then the Click event for the Clear button will be like the following:
然后清除按钮的 Click 事件将如下所示:
private void btnClear_Click(object sender, RoutedEventArgs e)
{
foreach (Control ctl in containerCanvas.Children)
{
if (ctl.GetType() == typeof(CheckBox))
((CheckBox)ctl).IsChecked = false;
if (ctl.GetType() == typeof(TextBox))
((TextBox)ctl).Text = String.Empty;
}
}
回答by Karthik Elumalai
Hi below is code used in my real time project.
嗨,下面是我的实时项目中使用的代码。
The below code will help you to identify all the child controls from the Parent control and do the action as we wish. Here i have used panel as a parent control and cleared all the text box control value with one shot by using simple method.
下面的代码将帮助您从父控件中识别所有子控件并按照我们的意愿执行操作。在这里,我使用面板作为父控件,并使用简单的方法一键清除所有文本框控件值。
Sample code
示例代码
public void SetTextBoxToNull(Panel pnlobjid)
{
try
{
foreach(Control cntrl in pnlobjid.Controls)
{
if (cntrl is TextBox)
{
TextBox txtbox = cntrl as TextBox;
txtbox.Text = "";
}
}
}
catch (Exception ex)
{
string test = ex.Message;
}
}
Hope the above code will give some guidance for you,Please let me know thoughts.
希望上面的代码能给你一些指导,请让我知道想法。
Thanks
谢谢
回答by Kamil Le
I've looked answers on the same question :) Thanks for solutions. Now in my program working similar code:
我看过同一问题的答案:) 感谢您的解决方案。现在在我的程序中运行类似的代码:
void clearTextBox(Grid gridName)
{
foreach (Control txtBox in gridName.Children)
{
if (txtBox.GetType() == typeof(TextBox))
((TextBox)txtBox).Text = string.Empty;
if (txtBox.GetType() == typeof(PasswordBox))
((PasswordBox)txtBox).Password = string.Empty;
}
}

