Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Funny Control Behavior [modified]

Funny Control Behavior [modified]

Scheduled Pinned Locked Moved C#
csharpdebuggingquestion
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 2048894
    wrote on last edited by
    #1

    Create a C# Windows Application. Replace the non-designer class code in the Form with mine below. The code simply creates 10 Panels on the form, each with one button on it. I assign each button's parent panel to the button's "Tag" property. I assign one common click event handler to all the buttons. In the click event handler, I simply cast the button's "Tag" to a Panel, then do a Form.Controls.Remove() to remove the Panel from the Form's "Controls" collection. I also create a "debug" window so that I can see when the button click event handler fires. Run the application and start hitting the spacebar. Exactly as you would expect, as you "click" each button by hitting the spacebar, the respective panel is removed. But keep hitting the spacebar after no more panels are visible. The click event KEEPS ON FIRING. I can understand that the panel can still be alive (and not have been garbage collected) and still have the button in its' "Controls" collection. But how does the spacebar keypress get from the form (which is in focus and has Controls.Count = 0) to the panel (and on to the button) that is no longer in its "Controls" collection???

    public partial class Form1 : Form
    {
    	private Form debugForm;
    	private TextBox debugTextBox;
    
    	public Form1()
    	{
    		InitializeComponent();
    	}
    
    	void b\_Click(object sender, EventArgs e)
    	{
    		// Remove panel from form.controls with button on panel is clicked
    		Button b = (Button)sender;
    		Panel p = (Panel)(b.Tag);
    		this.Controls.Remove(p);
    		debugTextBox.Text += "Click event fired for " + b.Name + " : this.Controls.Count = " +
    			this.Controls.Count.ToString() + "\\r\\n";
    	}
    
    	private void Form1\_FormClosing(object sender, FormClosingEventArgs e)
    	{
    		try
    		{
    			debugForm.Close();
    		}
    		catch { }
    	}
    
    	private void Form1\_Load(object sender, EventArgs e)
    	{
    		for (int i = 0; i < 10; i++)
    		{
    			Panel p = new Panel();
    			p.Left = 0;
    			p.Top = i \* 35;
    			p.Width = 300;
    			p.Height = 30;
    			Button b = new Button();
    			b.Left = 0;
    			b.Top = 0;
    			b.Width = 300;
    			b.Height = 30;
    			b.Name = b.Text = "Button" + i.ToString();
    			b.Click += new EventHandler(b\_Click);
    			b.Tag = p;
    			p.Controls.Add(b);
    			this.Controls.Add(p);
    		}
    
    		Rectangle rScreen = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
    		this.Left = 0;
    		this.Width = rScreen.Width / 2;
    		this.Top = 0;
    		this.Height = rScreen.Height;
    
    		debugForm = new Form();
    		debugForm.StartPosition = FormStartPo
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups