C# 是一个列表<&燃气轮机;可能的变量数量?
这可能是一个长期的尝试,但我正在努力减少我正在工作的程序中的重复,并且遇到了一个障碍。从下面的ClearTextBoxes()方法中可以看出,我有一段非常重复的代码,为了简洁起见,我更愿意将其放在foreach循环中。(最初,C# 是一个列表<&燃气轮机;可能的变量数量?,c#,list,variables,object,foreach,C#,List,Variables,Object,Foreach,这可能是一个长期的尝试,但我正在努力减少我正在工作的程序中的重复,并且遇到了一个障碍。从下面的ClearTextBoxes()方法中可以看出,我有一段非常重复的代码,为了简洁起见,我更愿意将其放在foreach循环中。(最初,foreach(CustomBox中的对象框)循环不存在)。我试着用下面的列表来做这件事,但是没有用。我不确定这是不可能做到的,还是我只是做错了。如果您能提供任何帮助,我将不胜感激。如果无法做到这一点,那么我如何缩小此代码块 谢谢 List<object> cu
foreach(CustomBox中的对象框)
循环不存在)。我试着用下面的列表来做这件事,但是没有用。我不确定这是不可能做到的,还是我只是做错了。如果您能提供任何帮助,我将不胜感激。如果无法做到这一点,那么我如何缩小此代码块
谢谢
List<object> customBoxes = new List<object>();
customBoxes.AddRange(new[] { "TextBox", "DateBox", "DigitBox", "PhoneBox", "WaterTextBox" });
public void ClearTextBoxes()
{
ChildControls ccChildren = new ChildControls();
foreach (object o in ccChildren.GetChildren(rvraDockPanel, 2))
{
foreach (object box in customBoxes)
{
if (o.GetType() == typeof(TextBox))
{
TextBox txt = (TextBox)o;
txt.Text = "";
}
if (o.GetType() == typeof(DigitBox))
{
DigitBox digit = (DigitBox)o;
digit.Text = "";
}
if (o.GetType() == typeof(PhoneBox))
{
PhoneBox phone = (PhoneBox)o;
phone.Text = "";
}
if (o.GetType() == typeof(DateBox))
{
DateBox date = (DateBox)o;
date.Text = "";
}
if (o.GetType() == typeof(WatermarkTextBox))
{
WatermarkTextBox water = (WatermarkTextBox)o;
water.Text = "";
}
}
}
}
List custombox=new List();
AddRange(新[]{“TextBox”、“DateBox”、“DigitBox”、“PhoneBox”、“WaterTextBox”});
公共无效ClearTextBox()
{
ChildControls ccChildren=新的ChildControls();
foreach(ccChildren.GetChildren中的对象o(rvraDockPanel,2))
{
foreach(自定义框中的对象框)
{
if(o.GetType()==typeof(TextBox))
{
TextBox txt=(TextBox)o;
txt.Text=“”;
}
if(o.GetType()==typeof(DigitBox))
{
数字框数字=(数字框)o;
数字。文本=”;
}
if(o.GetType()==typeof(PhoneBox))
{
电话盒电话=(电话盒)o;
phone.Text=“”;
}
if(o.GetType()==typeof(日期框))
{
日期框日期=(日期框)o;
日期。文本=”;
}
if(o.GetType()==typeof(水印文本框))
{
水印文本框水=(水印文本框)o;
水。Text=“”;
}
}
}
}
您可以以某种方式使用反射来模拟某些管道类型行为,但我不会选择这种解决方案,因为它既不性能也不美观
foreach (object box in customBoxes)
{
var boxType = box.GetType();
var textProperty = boxType.GetProperty("Text");
if (textProperty != null && textProperty.CanWrite)
{
textProperty.SetValue(box, "", null);
}
}
或者,您可以使用dynamic来实现相同的结果:
foreach (dynamic box in customBoxes)
{
box.Text = "";
}
方法是让您的自定义控件实现一个接口IWithTextProperty,它当然会公开text属性;
List<Type> customBoxes = new List<Type>();
customBoxes.AddRange(new[] { typeof(PhoneBox), typeof(DigitBox), ....." });
foreach (Control c in this.Controls)
{
if (customBoxes.Contains(c.GetType()))
{
c.Text = string.Empty;
}
}
AddRange(新[]{typeof(PhoneBox),typeof(DigitBox),…“});
foreach(此.Controls中的控件c)
{
if(custombox.Contains(c.GetType()))
{
c、 Text=string.Empty;
}
}
我将使用ClearText()方法创建一个
interface IClearable
{
public void ClearText();
}
然后可以从每个控件继承并应用该接口:
class ClearableDigitBox : DigitBox, IClearable
{
public void ClearText() {
Text = String.Empty;
}
}
// etc...
所以它只是:
var list = new List<IClearable>;
// ...
foreach (IClearable control in list) control.ClearText();
var list=新列表;
// ...
foreach(列表中的可复制控件)控件.ClearText();
不是所有的输入框都是控件对象的一部分吗
如果是,您希望清除控件中的所有文本
那么我可能会有一种方法,比如:
public void ClearText(List<Control> items)
{
foreach (Control control in items)
{
control.Text = string.Empty;
}
}
公共无效明文(列表项)
{
foreach(项目中的控制)
{
control.Text=string.Empty;
}
}
如果您只想定位特定类型的控件
public void ClearText(List<Control> items)
{
foreach (Control control in items)
{
if (control is TextBox)
((TextBox)control).Text = string.Empty;
else if (control is DigitBox)
((DigitBox)control).Text = string.Empty;
else
{ // Handle anything else.}
}
}
公共无效明文(列表项)
{
foreach(项目中的控制)
{
如果(控件为文本框)
((TextBox)控件).Text=string.Empty;
else if(控件为DigitBox)
((DigitBox)控件)。Text=string.Empty;
其他的
{//处理其他任何事情。}
}
}
对于到目前为止的几个回复,这是我为自定义框提供的类文件。NumberTextBox类是VS添加的默认代码段。我没有使用它,只是也没有删除它。除了DateBox(为了节省空间而折叠)之外类中,还有一个从DigitBox继承的PhoneBox类。DigitBox继承的WatermarkTextBox类位于WpfToolkit.Extended.dll中。这些类中唯一的真正区别是,每个类都添加了一个方法来允许/禁止按格式键(括号、句点、连字符等)
这个类基本上是由于我试图合并在web上找到的几个不同的代码片段而产生的,但是这些框的目的是启用水印,并且限制可以输入到这些框中的字符
public class NumberTextBox : Control
{
static NumberTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumberTextBox), new FrameworkPropertyMetadata(typeof(NumberTextBox)));
}
}
public class DigitBox : WatermarkTextBox, IClearable
{
#region Constructors
///<summary>
///The default constructor
/// </summary>
public DigitBox()
{
TextChanged += new TextChangedEventHandler(OnTextChanged);
KeyDown += new KeyEventHandler(OnKeyDown);
PreviewKeyDown += new KeyEventHandler(OnPreviewDown);
}
#endregion
#region Properties
new public String Text
{
get { return base.Text; }
set
{
base.Text = LeaveOnlyNumbers(value);
}
}
#endregion
#region Functions
public bool IsNumberKey(Key inKey)
{
if (inKey < Key.D0 || inKey > Key.D9)
{
if (inKey < Key.NumPad0 || inKey > Key.NumPad9)
{
return false;
}
}
return true;
}
public bool IsActionKey(Key inKey)
{
return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return;
}
public string LeaveOnlyNumbers(String inString)
{
String tmp = inString;
foreach (char c in inString.ToCharArray())
{
if (!IsDigit(c))
{
tmp = tmp.Replace(c.ToString(), "");
}
}
return tmp;
}
public bool IsSpaceKey(Key inKey)
{
if (inKey == Key.Space)
{
return true;
}
return false;
}
public bool IsDigit(char c)
{
return (c >= '0' || c <='9');
}
#endregion
#region Event Functions
protected virtual void OnKeyDown(object sender, KeyEventArgs e)
{
e.Handled = !IsNumberKey(e.Key) && !IsActionKey(e.Key) && !IsSpaceKey(e.Key);
}
protected virtual void OnTextChanged(object sender, TextChangedEventArgs e)
{
base.Text = LeaveOnlyNumbers(Text);
}
protected virtual void OnPreviewDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
}
}
#endregion
}
public class DateBox : DigitBox
公共类编号文本框:控件
{
静态NumberTextBox()
{
OverrideMetadata(typeof(NumberTextBox),new FrameworkPropertyMetadata(typeof(NumberTextBox));
}
}
公共类数字框:水印文本框
{
#区域构造函数
///
///默认构造函数
///
公共数字框()
{
TextChanged+=新的textchangedventhadler(OnTextChanged);
KeyDown+=新的KeyEventHandler(OnKeyDown);
PreviewKeyDown+=新的KeyEventHandler(OnPreviewDown);
}
#端区
#区域属性
新的公共字符串文本
{
获取{return base.Text;}
设置
{
base.Text=leaveOnlyNumber(值);
}
}
#端区
#区域功能
公共图书馆IsNumberKey(钥匙inKey)
{
如果(inKeyKey.D9)
{
if(inKeyKey.NumPad9)
{
返回false;
}
}
返回true;
}
公共布尔IsActionKey(Key inKey)
{
return inKey==Key.Delete | | inKey==Key.Back | | inKey==Key.Tab | | inKey==Key.return;
}
公共字符串leaveOnlyNumber(字符串插入)
{
字符串tmp=指令串;
foreach(inString.ToCharArray()中的字符c)
{
如果(!IsDigit(c))
{
tmp=tmp.Replace(c.ToString(),“”);
}
}
返回tmp;
}
公共b