我有大约7 textboxes在一个RectangleShapes中,它附带了Microsoft.VisualBasic.PowerPacks dll,我想检索它并应用一些验证。下面的代码从表单中检索所有的textboxes,而这些都不是预期的。有谁知道如何只检索那些在textboxes中的RectangleShape
Dim empty = Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0)
'empty will fetch all the textboxes inside form'
If empty.Any Then
MessageBox.Show("Some of the fields are empty.!")
Exit Sub
End If我尝试了这个无效的Me.RectangleShape1.Controls,但是我没有其他的想法去获取它!!
欢迎任何建议或想法。下面是textbox for Add Service的图像,它位于RectangleShape1之外

发布于 2015-08-08 12:36:31
由于形状没有控件,所以必须检查每个文本框的位置,它们是否位于矩形内。虽然有点乱七八糟,但会奏效的。
这是我的解决办法:
Dim txts As New List(Of TextBox)
Dim x1 = RectangleShape1.Left
Dim y1 = RectangleShape1.Top
Dim x2 = RectangleShape1.Left + RectangleShape1.ClientRectangle.Width
Dim y2 = RectangleShape1.Top + RectangleShape1.ClientRectangle.Height
For Each Control In Me.Controls
If TypeOf Control Is TextBox Then
Dim txt As TextBox = Control
Dim tx = txt.Left, ty = txt.Top
If tx >= x1 And tx <= x2 And ty >= y1 And ty <= y2 Then
txts.Add(txt)
End If
End If
Next
Dim empty = txts.Where(Function(txt) txt.Text.Length = 0)
If empty.Any Then MsgBox("Some field(s) are empty")发布于 2015-08-08 12:43:06
我发现每个textbox都有一个名为textbox的属性,并为此设置了一个值,在检索时我这样做了:
Dim empty = Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0
And txt.AccessibleDescription = "JobControls")
If empty.Any Then
MessageBox.Show("Some of the fields are empty.!")
Exit Sub
End If希望有人觉得有用
https://stackoverflow.com/questions/31892738
复制相似问题