Comment récupérer l'index d'un tableau de combobox sur un event Click

Résolu
fredlefou - 30 nov. 2012 à 03:10
 fredlefou - 1 déc. 2012 à 06:44
Bonjour à tous,

J'ai créer un tableau de combobox
private ComboBox[,] my_combobox;

public void Build_mycombo()
{ ...
for(int x = 0; x < 9; x++){
for(int y = 0; y < 9; y++){
comboBox.Items.AddRange(items);
comboBox.DropDownStyle = ComboBoxStyle.DropDown;
comboBox.Location = new Point(x * 40 + 100, y * 40 + 100);
comboBox.Size = new Size(30, 30);
comboBox.SelectedIndex = 0;
comboBox.MaxDropDownItems = 10;
comboBox.Visible = true;
this.Controls.Add(comboBox);
my_combobox[x, y] = comboBox;
this.my_combobox[x, y].Click += new System.EventHandle this.my_combobox_Click);
}
}
}
...

InitializeComponent();
my_combobox = new ComboBox[9,9];
...

Private void my_combobox(objet sender, System.EventArgs e)
{
????? ici comment je fais pour savoir l'index (x,y)de my_combobox que je vien de cliqué.}

Genre...[2,6] x=2 y=6


Merci à l'avance

FredLeFou

2 réponses

ed73 Messages postés 276 Date d'inscription lundi 8 septembre 2008 Statut Membre Dernière intervention 15 avril 2013 2
30 nov. 2012 à 09:24
Bonjour,

la variable sender te donne l'objet qui a déclenché l'évènement, tu peux donc t'en servir. Mais pour cela il faut que chaque combobox ait une propriété qui permette de la différencier des autres, voire de te donner directement les index.

Par exemple à l’initialisation :

comboBox.Tag = x.ToString() + ";" + y.ToString;

et dans l'évènement :

string[] xy = (string)(((ComboBox)sender).Tag).Split(';');
int x int.Parse(xy[0]), y int.Parse(xy[1]);
3
Merci de prendre le temp de répondre!
Celà fonctionne très bien. voici je que j'ai écrit.

...
comboBox.Tag = x.ToString() + ";" + y.ToString();
...
private void Mycombobox_Click(object sender, System.EventArgs e)
{
string tmp = (string)(((ComboBox)sender).Tag);
int x=0;
int y=0;
if (tmp != null)
{
string[] xy = (string[])(tmp.Split(new Char[] { ';' }));
y = int.Parse(xy[1]);
x = int.Parse(xy[0]);
MessageBox.Show(x.ToString() + y.ToString());
}
}

NB. Petite correction...
string[] xy = (string[])(tmp.Split(new Char[] { ';' }));

Résolu

Encore Merci!

FredLeFou
3
Rejoignez-nous