a,b,cのテキストボックスが3つあります。
入力する値の大きさは下記が条件です
a>b>c
テキストボックスに入力時に3つの関係が不適切ならメッセージを出すようにしたいです。
例えばb>aなら下記みたいなイメージです
If Val(TextBox2.Text) > Val(TextBox1.Text) Then
MsgBox("bはaより小さいです")
これをc>b>aで、まとめて比較し、できるようにしたいです
あとコードが長いので短くもしたいです。
現状はこのようになっています。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each q In Me.Controls.OfType(Of TextBox)()
AddHandler q.Leave, AddressOf TextBoxes_Leave
Next
End Sub
Private Sub TextBoxes_Leave(sender As TextBox, e As EventArgs)
Dim d As Double
If Double.TryParse(sender.Text, d) AndAlso d > 0 Then sender.ResetBackColor() If TextBox2.TextLength <> 0 AndAlso TextBox1.TextLength <> 0 Then If Val(TextBox2.Text) > Val(TextBox1.Text) Then MsgBox("bはaより小さいです") sender.BackColor = Color.Red sender.Focus() End If ElseIf TextBox3.TextLength <> 0 AndAlso TextBox2.TextLength <> 0 Then If Val(TextBox3.Text) > Val(TextBox2.Text) Then MsgBox("cはbより小さいです") sender.BackColor = Color.Red sender.Focus() ElseIf TextBox3.TextLength <> 0 AndAlso TextBox1.TextLength <> 0 Then If Val(TextBox3.Text) > Val(TextBox1.Text) Then MsgBox("cはaより小さいです") sender.BackColor = Color.Red sender.Focus() End If End If End If End If End Sub
補足情報(FW/ツールのバージョンなど)
visualsudaio2017