简体   繁体   English

VB.NET对于自定义控件的每个异常

[英]VB.NET For each exception on custom controls

in VB.NET i have 2 custom controls, one is a TextBox and second one is a ComboBox. 在VB.NET中我有2个自定义控件,一个是TextBox,第二个是ComboBox。 These have custom values like Bool _IsHidden and are added on runtime to a form. 它们具有Bool _IsHidden等自定义值,并在运行时添加到表单中。

Now, at some point in the code I want to check if the _IsHidden is set to True or False and display that information. 现在,在代码中的某个时刻,我想检查_IsHidden是否设置为True或False并显示该信息。 Since the user can edit this values when creating the control these are not set on creation. 由于用户可以在创建控件时编辑此值,因此在创建时不会设置这些值。

So what I tried is: 所以我尝试的是:

(all of this is on MDI Forms) (所有这些都在MDI表格上)

For Each frm as CustomForm in Main.MdiChildren
If frm.MyName = calledBy Then 'this part is just to know which form called the form to create the object
For Each cntrl as CustomTextBox in frm.Controls
'DO Something
Next
End if
Next

Now.. if the first control is a custom ComboBox it thorws an error since it sees that it does not match the custom TextBox control.. 现在..如果第一个控件是自定义ComboBox,它会发出错误,因为它看到它与自定义TextBox控件不匹配。

how do i get around this? 我怎么绕过这个? By my understanding it should just go through all of the controls on the said form and just check those who match CustomTextBox control ? 根据我的理解,它应该只是通过所述表格上的所有控件,只检查那些匹配CustomTextBox控件?

Thank you 谢谢

For Each x As T In collection does not filter your collection items to those of type T . For Each x As T In collection不会您的集合项过滤为T类型的集合。 It tries to convert every item in collection to T and throws an exception if that fails. 它尝试将collection 每个项目转换为T ,如果失败则抛出异常。

Thus, you have the following options: 因此,您有以下选择:

  1. Do the check yourself, for example, using the code provided by RB . 例如,使用RB提供的代码进行检查。

  2. Alternatively, you could filter your list first , and then loop through the items. 或者,您可以通过项目第一过滤你的列表,然后循环。 Here, LINQ can help: 在这里,LINQ可以帮助:

     For Each cntrl In frm.Controls.OfType(Of CustomTextBox)() ... ' Do this for all CustomTextBoxes Next For Each cntrl In frm.Controls.OfType(Of CustomComboBox)() ... ' Do this for all CustomComboBoxes Next 

    You don't need the As CustomTextBox clause here, since frm.Controls.OfType(Of CustomTextBox)() returns an IEnumerable(Of CustomTextBox) , so For Each can infer by itself that cntrl must be of type CustomTextBox . 这里不需要As CustomTextBox子句,因为frm.Controls.OfType(Of CustomTextBox)()返回一个IEnumerable(Of CustomTextBox) ,因此For Each可以自己推断出cntrl必须是CustomTextBox类型。

By my understanding it should just go through all of the controls on the said form and just check those who match CustomTextBox control ? 根据我的理解,它应该只是通过所述表格上的所有控件,只检查那些匹配CustomTextBox控件?

That's not correct I'm afraid. 这是不正确的我害怕。 You'll need to implement that check yourself, eg: 您需要自己实施该检查,例如:

For Each cntrl as object in frm.Controls
    If TypeOf cntrl Is CustomTextBox Then
        With CType(cntrl, CustomTextBox)
            .DoSomethingWithControl()
            .DoSomethingElseWithControl()
        End With
    End If 
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM