Results 1 to 5 of 5

Thread: (resolved) getting control name on click event

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member Sneeden's Avatar
    Join Date
    Oct 2001
    Location
    Sneedville
    Posts
    258

    (resolved) getting control name on click event

    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:

    Code:
    if(sender.Equals((object)controlName))
        //do something
    but there is no way I want to sit in a double nested loop to find which button was clicked.













    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());
    		}
    Last edited by Sneeden; Jul 19th, 2005 at 07:11 AM. Reason: resolved
    balls deep in bad code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width