ncode
Messages postés4Date d'inscriptionlundi 27 septembre 2004StatutMembreDernière intervention 9 septembre 2006
-
10 avril 2006 à 15:22
ncode
Messages postés4Date d'inscriptionlundi 27 septembre 2004StatutMembreDernière intervention 9 septembre 2006
-
10 avril 2006 à 19:36
Bonjour à tous !
Mon gros probleme est dans le titre ;)
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....)
Lutinore
Messages postés3246Date d'inscriptionlundi 25 avril 2005StatutMembreDernière intervention27 octobre 201241 10 avril 2006 à 16:20
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 );
};
}