Vielleicht so .....
Public Class DynamicControlDemo2
Dim WithEvents ControlCountButton As New Button With _
{.Parent = Me, .Width = 200, .Text = "Zahl der Textboxen"}
Dim WithEvents BoxFunctionCombo As New ComboBox With _
{.Parent = Me, .Top = 50, _
.DropDownStyle = ComboBoxStyle.DropDownList}
Dim BoxFunctionCombo_SelectedIndex As Integer
Private Sub DynamicControlDemo2_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
Me.Size = New Size(300, 600)
With BoxFunctionCombo
.Items.AddRange _
({"nur Ziffern", _
"nur Buchstaben (klein)", _
"nur Buchstaben (groß)"})
.SelectedIndex = 0
BoxFunctionCombo_SelectedIndex = -1
End With
End Sub
Private Sub ControlCountButton_Click(sender As Object, _
e As System.EventArgs) Handles ControlCountButton.Click
Dim inp As String = InputBox("Wie viele Textboxen?", Me.Name, " 3")
Dim c As Integer
If Not Integer.TryParse(inp, c) Then Exit Sub
If c < 1 Or c > 5 Then Exit Sub
ClearBoxes()
For i As Integer = 0 To c - 1
Dim tbo As New TextBox With _
{.Parent = Me, .Top = 50 * (i + 3), .Width = 200}
Next i
BoxFunctionCombo_SelectedIndexChanged(Me, EventArgs.Empty)
End Sub
Private Sub NurZiffern(ByVal sender As Object, _
e As KeyPressEventArgs)
Dim ziffern As String = "1234567890"
If Asc(e.KeyChar) = 8 Then Exit Sub
If Not ziffern.Contains(e.KeyChar) Then e.Handled = True
End Sub
Private Sub NurBuchstabenKlein(ByVal sender As Object, _
e As KeyPressEventArgs)
Dim buchstaben As String = "abcdefghijklmnopqrstuvwxyz"
If Asc(e.KeyChar) = 8 Then Exit Sub
If Not buchstaben.Contains(e.KeyChar) Then e.Handled = True
End Sub
Private Sub NurBuchstabenGross(ByVal sender As Object, _
e As KeyPressEventArgs)
Dim buchstaben As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
If Asc(e.KeyChar) = 8 Then Exit Sub
If Not buchstaben.Contains(e.KeyChar) Then e.Handled = True
End Sub
Private Sub ClearBoxes()
For i As Integer = Me.Controls.Count - 1 To 0 Step -1
If TypeOf Me.Controls(i) Is TextBox Then
Me.Controls.RemoveAt(i)
End If
Next i
End Sub
Private Sub BoxFunctionCombo_SelectedIndexChanged(sender As Object, _
e As System.EventArgs) Handles BoxFunctionCombo.SelectedIndexChanged
With BoxFunctionCombo
For Each ctl As Control In Me.Controls
If TypeOf ctl Is TextBox Then
Select Case BoxFunctionCombo_SelectedIndex
Case 0
RemoveHandler ctl.KeyPress, _
AddressOf NurZiffern
Case 1
RemoveHandler ctl.KeyPress, _
AddressOf NurBuchstabenKlein
Case 2
RemoveHandler ctl.KeyPress, _
AddressOf NurBuchstabenGross
End Select
Select .SelectedIndex
Case 0
AddHandler ctl.KeyPress, _
AddressOf NurZiffern
Case 1
AddHandler ctl.KeyPress, _
AddressOf NurBuchstabenKlein
Case 2
AddHandler ctl.KeyPress, _
AddressOf NurBuchstabenGross
End Select
End If
Next ctl
BoxFunctionCombo_SelectedIndex = .SelectedIndex
End With
End Sub
End Class |