0
votes

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
How are you wiring up the events? In code or in the designer?phoog
In the designer (in both the Event settings [lightning icon in properties pane] and by double checking the Designer.csBen Black
Please post the line in the designer that actually subscribes your event to your toggleSoundClick. It's not uncommon to accidentally subscribe to the wrong control or even the form when doing it from the designer.TyCobb
Edited, it's above To clarifyBen Black
You showed the toggleSeedClick which isn't your toggleSoundClick.TyCobb

1 Answers

0
votes

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.