En faite j'ai generé des checkbox dynamiquement et ensuite je voudrai
recuperer ceux qui sont à l'etat checked pour ensuite pour recuperer
leur nom ( sport, actualité,etc...)
Merci beaucoup pour votre aide
PS: voici le code qui marche (mais peut etre tres beau....)
Salut, inscris tous tes CheckBox au même évènement, puis dans la fonction de rappel cast le sender pour savoir quel objet a généré l'évènement.
public partial class Form1 : Form
{
public Form1( )
{
InitializeComponent( );
string cnxString = @" ..."
string cmdString = @" ... ";
List< string > list = new List< string >( );
using ( SqlConnection cnx = new SqlConnection( cnxString ) )
{
cnx.Open( );
SqlCommand cmd = new SqlCommand( cmdString, cnx );
using ( SqlDataReader reader = cmd.ExecuteReader( ) )
{
while ( reader.Read( ) )
{
string s = reader[ 0 ].ToString( );
if ( s != String.Empty )
list.Add( s );
}
}
}
CheckBox[ ] boxes = new CheckBox[ list.Count ];
for ( int i = 0; i < list.Count; i++ )
{
boxes[ i ] = new CheckBox( );
boxes[ i ].Location = new Point( 10, 28 * i );
boxes[ i ].Text = ( string )list[ i ];
boxes[ i ].CheckStateChanged += delegate( object sd, EventArgs e )
{
CheckBox cb = ( CheckBox )sd;
MessageBox.Show( cb.Text + " is " + cb.CheckState );
};
}