First of all.....sorry for the super long post. I don't like them either. Just trying to include information.
I have added several text boxes dynamically. They are a 2d array, btn[x,y].
I gave them all the same click event. The problem is that when one of them is clicked I need to know which one was clicked. I don't know how to extract the control name out of the arguments (Object sender, System.EventArgs e). I know that you can use:
but there is no way I want to sit in a double nested loop to find which button was clicked.Code:if(sender.Equals((object)controlName)) //do something
To help, I've included some code snippets from my project:
Here is the routine that creates the buttons:
Code:private void createSockets(int iX, int iY) { int x=0,y=0; p_socks=new Button[iX,iY]; try { for(x=0;x<iX;x++) { for(y=0;y<iY;y++) { //no need to create sockets again if(socketsCreated) return; //generate socket name string butName=x.ToString()+(char)(y+'A'); //add new buttons p_socks[x,y]=new Button(); this.Controls.Add(p_socks[x,y]); p_socks[x,y].Location = new System.Drawing.Point(10, 10); p_socks[x,y].Name = "socks"+x+y; p_socks[x,y].Size = new System.Drawing.Size(20, 20); p_socks[x,y].TabIndex = 20; p_socks[x,y].Text = ""; //"socks"+x+y; p_socks[x,y].BackColor=p_backColor; p_socks[x,y].Visible=true; p_socks[x,y].Click+=new System.EventHandler(sockClickHand); } } this.Refresh(); } catch(Exception err) { errhand(err); } }
And here is the click event:
Code:private void sockClickHand(Object sender, System.EventArgs e) { //this only displays the type of controls, not the name System.Windows.Forms.MessageBox.Show("You have clicked button " + Tag.ToString()); }




Reply With Quote