Failed to create object of type "System.Windows.Forms.Button" for input> "System.Windows.Forms.TextBox"
I wrote a function that empties all TextBox in my form:
Private Sub effacer() For Each t As TextBox In Me.Controls t.Text = Nothing Next End Sub But I had this problem:
Cannot pass an object of type 'System.Windows.Forms.Button' to input of type 'System.Windows.Forms.TextBox'.
I tried adding this If TypeOf t Is TextBox Then , but I had the same problem
Controls contains all formBoxes controls.
Instead, you can use Enumerable.OfType to find and discard all TextBoxes :
For Each txt As TextBox In Me.Controls.OfType(Of TextBox)() txt.Text = "" Next If you want to do the same in the old school style:
For Each ctrl As Object In Me.Controls If TypeOf ctrl Is TextBox DirectCast(ctrl, TextBox).Text = "" End If Next For Each t As TextBox In Me.Controls This line right here is trying to pass a TextBox to each control.
You need to change this to As Control or use Me.Controls.OfType(Of TextBox)() to filter the collection before iterating.
Here is a line of code that will clear all radioButtons from a groupBox that is attached to button_click:
groupBoxName.Controls.OfType<RadioButton>().ToList().ForEach(p => p.Checked = false); Use the appropriate changes to adapt them to your needs.