Recuperer l'etat d'un checkbox generer dynamiquement

Résolu
ncode Messages postés 4 Date d'inscription lundi 27 septembre 2004 Statut Membre Dernière intervention 9 septembre 2006 - 10 avril 2006 à 15:22
ncode Messages postés 4 Date d'inscription lundi 27 septembre 2004 Statut Membre Derniè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....)



int i;


labelCat.Visible = true;


ArrayList ArrayCat = new ArrayList();




SqlDataReader Dr = SqlDa.ExecuteReader();




while (Dr.Read())

{


if (!Dr.Equals(""))


{





ArrayCat.Add(Dr.GetString(0));





}

}

Dr.Close();






for (i = 0; i <= comptCat-1; i++)

{


CheckBox cb = new CheckBox();





cb.Location = new Point (600 , 200+(i*20));


cb.Name = " checkBox"+i;


cb.Text =
ArrayCat[i].ToString();






// ajout au form


TabEnvoieMail.Controls.Add(cb);


//MessageBox.Show(cb.CheckState.ToString());





}

2 réponses

Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
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 );
};
}


this.Controls.AddRange( boxes );
}
}
3
ncode Messages postés 4 Date d'inscription lundi 27 septembre 2004 Statut Membre Dernière intervention 9 septembre 2006
10 avril 2006 à 19:36
Merci beaucoup Lunitore :)



Ncode
0
Rejoignez-nous