在控件 C# 中获取 c.SelectedItem
Get c.SelectedItem in Controls C#
我正在为我的项目制作一些验证功能,但我卡在了某些地方。
我想要一个函数来处理几个不同的控件和错误。
这是我的代码:
private void ValidateControls(Control c)
{
if (c is TextBox)
{
if (c.Text == "")
{
epNew.SetError(c, "Something");
}
}
else if (c is ComboBox)
{
// What now?
// if (c.SelectedItem == null) does not work
}
}
我这样称呼它:
private void txtNEAN_Validating(object sender, CancelEventArgs e)
{
ValidateControls(txtNEAN);
}
这适用于文本框。但如果我这样做:
private void cbbEMerk_Validating(object sender, CancelEventArgs e)
{
ValidateControls(cbbEMerk);
}
if (c.SelectedItem == null)
例如不起作用。
我怎样才能做到这一点?这可以使用吗?如果没有,什么是更好的选择?
我很想听听任何消息!
在这种情况下,您必须将 c
转换为 ComboBox
else if (c is ComboBox)
{
if (((ComboBox)c).SelectedItem == null)
}
顺便说一句,如果每个控件都做同样的事情,请不要为它们创建一个 _Validating
方法。您可以使用一个,或者一个 txtBox_Validating
用于文本框,一个 comboBox_Validating
用于组合框,等等。
尝试使用
((ComboBox)c).SelectedItem
相反。这告诉程序将 Control c 解析为 ComboBox。
作为替代方案,您可以使用 as
而不是 is
// Converts c to a ComboBox. If c is not a ComboBox, assigns null to cmbControl
ComboBox cmbControl = c as ComboBox;
if (cmbControl != null)
{
if (cmbControl.SelectedItem != null)
{
// Do stuff here
}
}
// Else it's not a ComboBox
了解使用 as 和 is 的安全转换也很好:
Because objects are polymorphic, it is possible for a variable of a base class type to hold a derived type. To access the derived type's method, it is necessary to cast the value back to the derived type. However, to attempt a simple cast in these cases creates the risk of throwing an InvalidCastException. That is why C# provides the is and as operators. You can use these operators to test whether a cast will succeed without causing an exception to be thrown. In general, the as operator is more efficient because it actually returns the cast value if the cast can be made successfully. The is operator returns only a Boolean value. It can therefore be used when you just want to determine an object's type but do not have to actually cast it.
你可以看到更多here
我正在为我的项目制作一些验证功能,但我卡在了某些地方。
我想要一个函数来处理几个不同的控件和错误。
这是我的代码:
private void ValidateControls(Control c)
{
if (c is TextBox)
{
if (c.Text == "")
{
epNew.SetError(c, "Something");
}
}
else if (c is ComboBox)
{
// What now?
// if (c.SelectedItem == null) does not work
}
}
我这样称呼它:
private void txtNEAN_Validating(object sender, CancelEventArgs e)
{
ValidateControls(txtNEAN);
}
这适用于文本框。但如果我这样做:
private void cbbEMerk_Validating(object sender, CancelEventArgs e)
{
ValidateControls(cbbEMerk);
}
if (c.SelectedItem == null)
例如不起作用。
我怎样才能做到这一点?这可以使用吗?如果没有,什么是更好的选择?
我很想听听任何消息!
在这种情况下,您必须将 c
转换为 ComboBox
else if (c is ComboBox)
{
if (((ComboBox)c).SelectedItem == null)
}
顺便说一句,如果每个控件都做同样的事情,请不要为它们创建一个 _Validating
方法。您可以使用一个,或者一个 txtBox_Validating
用于文本框,一个 comboBox_Validating
用于组合框,等等。
尝试使用
((ComboBox)c).SelectedItem
相反。这告诉程序将 Control c 解析为 ComboBox。
作为替代方案,您可以使用 as
is
// Converts c to a ComboBox. If c is not a ComboBox, assigns null to cmbControl
ComboBox cmbControl = c as ComboBox;
if (cmbControl != null)
{
if (cmbControl.SelectedItem != null)
{
// Do stuff here
}
}
// Else it's not a ComboBox
了解使用 as 和 is 的安全转换也很好:
Because objects are polymorphic, it is possible for a variable of a base class type to hold a derived type. To access the derived type's method, it is necessary to cast the value back to the derived type. However, to attempt a simple cast in these cases creates the risk of throwing an InvalidCastException. That is why C# provides the is and as operators. You can use these operators to test whether a cast will succeed without causing an exception to be thrown. In general, the as operator is more efficient because it actually returns the cast value if the cast can be made successfully. The is operator returns only a Boolean value. It can therefore be used when you just want to determine an object's type but do not have to actually cast it.
你可以看到更多here