How to get GroupBox as ControlCollection??

G

Guest

Hi

I want to make a general function for change events of the TextBoxes, ComboBoxes, Date Picker Controls etc. The problems is that when there exists controls in the GroupBox I recursively call the function. The function is as follows

Dim objCTL As Control, objRadio As RadioButton, objCheck As CheckBo
Tr
For Each objCTL In c
Select Case objCTL.GetType.ToStrin
Case "System.Windows.Forms.TextBox
AddHandler objCTL.TextChanged, AddressOf Func
Case "System.Windows.Forms.RadioButton
objRadio = objCT
AddHandler objRadio.CheckedChanged, AddressOf Func
Case "System.Windows.Forms.GroupBox"
Call EventRaiser(objCTL.Controls) ' It gives error here that the specific cast is not vaild
End Selec
Nex
Catch ex As Exceptio

End Tr

As suggested the error occurs at the Recursive call from the GroupBox case. Eventhough the Parameter passed is ControlCollection

Can anybody give me solution to this??

Thanks

Shalin Parmar
 
W

Wiktor Zychla

I want to make a general function for change events of the TextBoxes,
ComboBoxes, Date Picker Controls etc. The problems is that when there exists
controls in the GroupBox I recursively call the function. The function is as
follows:

I personally do not use VB.NET but I suggest that you should slightly
redeclare your method - let the 'object' be the parameter, not the
ControlCollection. Now you can check the control type and call the method
recursively. Below an example from my C# application (exactly the same
functionality you need):

Regards,
Wiktor Zychla
public static void AddChangeHandler( object o, EventHandler hChange )

{

if ( o is Form )

foreach ( Control c in ((Form)o).Controls )

AddChangeHandler( c, hChange );

if ( o is GroupBox )

foreach ( Control c in ((GroupBox)o).Controls )

AddChangeHandler( c, hChange );

if ( o is Panel )

foreach ( Control c in ((Panel)o).Controls )

AddChangeHandler( c, hChange );

if ( o is TabPage )

foreach ( Control c in ((TabPage)o).Controls )

AddChangeHandler( c, hChange );

if ( o is TabControl )

foreach( TabPage tp in ((TabControl)o).TabPages )

AddChangeHandler( tp, hChange );


// simple controls

if ( o is TextBox )

{

TextBox oT = o as TextBox;

if ( oT.ReadOnly == false )

oT.TextChanged += new EventHandler( hChange );

}

if ( o is CheckBox ) ((CheckBox)o).CheckStateChanged += new EventHandler(
hChange );

if ( o is RichTextBox ) ((RichTextBox)o).TextChanged += new EventHandler(
hChange );

if ( o is DateTimePicker ) ((DateTimePicker)o).ValueChanged += new
EventHandler( hChange );

if ( o is NumericUpDown ) ((NumericUpDown)o).ValueChanged += new
EventHandler( hChange );

}
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Shalin,

Can you give the the prototype of the function EventRaiser

--

Stoitcho Goutsev (100) [C# MVP]


Shalin Parmar said:
Hi,

I want to make a general function for change events of the TextBoxes,
ComboBoxes, Date Picker Controls etc. The problems is that when there exists
controls in the GroupBox I recursively call the function. The function is as
follows:
Dim objCTL As Control, objRadio As RadioButton, objCheck As CheckBox
Try
For Each objCTL In cc
Select Case objCTL.GetType.ToString
Case "System.Windows.Forms.TextBox"
AddHandler objCTL.TextChanged, AddressOf Func1
Case "System.Windows.Forms.RadioButton"
objRadio = objCTL
AddHandler objRadio.CheckedChanged, AddressOf Func1
Case "System.Windows.Forms.GroupBox"
Call EventRaiser(objCTL.Controls) ' It gives
error here that the specific cast is not vaild.
End Select
Next
Catch ex As Exception

End Try

As suggested the error occurs at the Recursive call from the GroupBox
case. Eventhough the Parameter passed is ControlCollection.
 
Top