Click event not firing for custom UserControl on a form

心已入冬 提交于 2019-12-12 01:31:17

问题


I have created a UserControl called Toggle, this is my code for it

[DefaultEvent("Click")]
public partial class Toggle : UserControl {

    public bool ToggleStatus { get { return toggleStatus; } }
    private bool toggleStatus { get; set; }

    public Toggle() {
        InitializeComponent();
        toggleStatus = true;
    }

    private void toggleClick(object sender, EventArgs e) {
        if (toggleStatus) { // currently set as "true" or "on"
            this.lblSwitch.Dock = DockStyle.Right;                
            this.pnlBackground.BackColor = System.Drawing.Color.Red;
            toggleStatus = false;
        } else { // currently set as "false" or "off"
            this.lblSwitch.Dock = DockStyle.Left;                
            this.pnlBackground.BackColor = System.Drawing.Color.Green;
            toggleStatus = true;
        }
    }
}

The toggleClick method is tied to the click event of controls within the UserControl; this fires off just fine.

However, when I put my Toggle control on a form and attempt to tie an event to the click of it, it won't fire off.

    private void toggleSoundClick(object sender, EventArgs e) {
        soundToggle = !soundToggle;
    }

I've made sure that the proper method is tied to the click event in my Designer.cs file of both my UserControl and my form

UserControl:

this.lblSwitch.Click += new System.EventHandler(this.toggleClick);
this.pnlBackground.Click += new System.EventHandler(this.toggleClick);

(I have it tied to two controls on my Toggle since I want it to fire no matter where you click on the control)

Form:

this.tglSound.Click += new System.EventHandler(this.toggleSoundClick);

The expected behavior for the UserControl is to fire off toggleClick (which it does) then the form should fire off toggleSoundClick (which it doesn't). I have seen this behavior work fine for other UserControls I have designed and used in this same project.

To clarify:

I have a UserControl called ServerDisplay. I have a method tied to the click event of the background panel of ServerDisplay (in the code for ServerDisplay) that shows a random MessageBox:

    private void ServerDisplay_Click(object sender, EventArgs e) {
        MessageBox.Show("test");
    }

Then, I have a ServerDisplay control contained within my form. I have a method tied to the click event of it as well (in the code for my form)

    private void serverDisplayClick(object sender, EventArgs e) {
        if (loaded) {
            ServerDisplay display = (ServerDisplay)sender;
            this.lblLastServer.Text = "Last server joined was " + display.Server.Name + " at " + DateTime.Now.ToString("h:mm tt");
            centerControl(this.lblLastServer);
        }
    } 

When I click on the ServerDisplay control in my form, it shows the MessageBox (code from within ServerDisplay), then updates the label I specified in the code (code from form). This is the intended behavior, but it is not working for my other UserControl.


回答1:


I finally figured it out! The way I had the control set up, I had the control itself, a panel filling up the entire background (I used this for the color), and then another panel inside the first panel to act as the "switch".

When I got rid of the first panel and just used the background of the control for the color and a small panel for the switch, it works when I click the background, but not when I click the "switch" panel. I guess this opens up more questions that I'll have to ask separately from this one, but at least I got my answer.



来源:https://stackoverflow.com/questions/24049013/click-event-not-firing-for-custom-usercontrol-on-a-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!