我使用以下内容检查我的文本框是否为空。
Dim empty = Me.Controls.OfType(Of TextBox)().Where(Function(txt) txt.Text.Length = 0)
If empty.Any Then
MessageBox.Show(String.Format("Please fill following textboxes: {0}",String.Join(",",empty.Select(Function(txt) txt.Name))))
End If
我想将空文本框的颜色更改为红色,但我不知道该怎么做。请有人帮我这样做。
答案 0 :(得分:2)
LINQ
库。
Imports System.Runtime.CompilerServices
的
<Extension()>
Public Module Extensions
<Extension()>
Public Function ForEach(Of T)(source As IEnumerable(Of T), action As Action(Of T)) As IEnumerable(Of T)
For Each item In source
action.Invoke(item)
Next
Return source
End Function
<Extension()>
Public Function ForAll(Of T)(source As IEnumerable(Of T), action As Action(Of IEnumerable(Of T))) As IEnumerable(Of T)
action.Invoke(source)
Return source
End Function
End Module
<强>用法强>
Me.Controls _
.OfType(Of TextBox) _
.ForEach( _
Sub(box)
box.BackColor = SystemColors.Window
End Sub) _
.Where(
Function(box)
Return box.Text.Length = 0
End Function) _
.ForEach(
Sub(box)
box.BackColor = Color.Red
End Sub)
.ForAll(
Sub(source)
If (source.Count() > 0) Then
MessageBox.Show( _
String.Format( _
"Please fill following textboxes: {0}", _
String.Join(", ", _
source.Select(Function(box) box.Name))))
End If
End Sub)
答案 1 :(得分:1)
这应该这样做
empty.ToList().ForEach(Function(txt) txt.BackColor = Drawing.Color.Red)