Checking the Tags of each TextBox in an array

吃可爱长大的小学妹 提交于 2021-02-09 08:17:48

问题


How do I check the Tag property of all the TextBox controls in an array?
I want something like this:

If textBox.Tag And textbox2.Tag And textbox21.Tag And
   textbox22.Tag And textbox23.Tag And textbox24.Tag = "2" Then

This is my array of TextBoxes:

Dim allTextboxes() As TextBox = {textBox, narNaslov, narPersona, narDani, narPersona2,
                                 kupIme, kupAdresa, kupKontakt, uvBroj, uvDatum, uvIznos,
                                 uvAvans, uvRok, uvNacin, datumTbox} 

回答1:


You can use the LINQ All()-Method

If allTextBoxes.All(Function(t) t.Tag.ToString = "2") Then
    'All Tags are "2"
End If

To avoid a NullReferenceException, if one of the textboxes is Nothing you can add an additional check:

If allTextBoxes.All(Function(t) t IsNot Nothing AndAlso t.Tag.ToString = "2") Then
    'All Tags are "2"
End If

Or you can use the Null-conditional operator (Visual Basic v. 14 or greater)

If allTextBoxes.All(Function(t) t?.Tag.ToString = "2") Then
    'All Tags are "2"
End If



回答2:


You can use the LINQ's All extension method to verify whether all the Controls in the array have the same Tag.
You could use the Any to verify whether at least one of the Controls in the array has its Tag set to the specified value.

If you have Option Strict On, to avoid a NullReferenceException, since the Tag property is of type Object and can be null (nothing), you can use the Is operator to perform the check:
(a Control in the array could be nothing, too, at some point, so we could use txt?.Tag Is (...). In this case, even when one of TextBoxes is nothing for some reason, if the other TextBoxes all have the same Tag value, the method will return True)

Dim result As Boolean = allTextboxes.All(Function(txt) txt?.Tag Is "2")

If result is True then all the TextBoxes.Tag property is set to the value used in the comparison.

This comparison will return True when:

  • The string is assigned using the Property Grid in the Designer
  • In code, with a direct value assignment: Control.Tag = "2",
  • In code, using a Field or a Local variable:

    Private TagValue As String = "2" 
    [Control].Tag = TagValue
    
    [Control]?.Tag Is "2"       => True
    [Control]?.Tag Is TagValue  => True 
    

As noted by Visual Vincent, the Is operator doesn't perform a value comparison; it determines whether two Objects refer to the same Object (in this case, the string is created programmatically, so the comparison is performed on the string reference in the intern pool table).
This could be a desired outcome or it may be not.

Setting:

Dim TagValue As String = New String("2"c, 1) 
[Control].Tag = TagValue 

[Control].Tag Is "2"           => False
[Control].Tag Is TagValue      => True

[Control].Tag.Equals("2")      => True
[Control].Tag.Equals(TagValue) => True

If/when a strict value comparison is required, the previous method can be changed in:

Dim result As Boolean = allTextboxes.All(Function(txt) txt.Tag.Equals("2")

It will return True in both cases: the Tag value is compared to determine if the two objects are equal.
It will also be True, in this case, even if one or more of the Controls in the array is/are disposed of or otherwise null and it's Option Strict-compliant.

However, a NullReferenceException will be raised if one of the Tag properties is set to nothing.
In this case a double check is required:

Dim result As Boolean = allTextboxes.
    All(Function(txt) txt.Tag IsNot Nothing AndAlso txt.Tag.Equals("2"))


来源:https://stackoverflow.com/questions/53867555/checking-the-tags-of-each-textbox-in-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!