0

This problem is related to the creation and use of a user control in WinForms. At first, when I found online tutorials, I created a user control library. The first attempt was to modify a checkbox control. Once compiled, it would show in the tool box and when dragged onto a form and double-click, the default event was "CheckChanged". I didn't need to add any further code to the user control itself. No extra default handler, attributes or so.

Then I added another, very similar control to the library, and this already caused problems. I discarded the lib and did this:

  1. Created a new WinForms user control in my project.
  2. Edited it in the designer like I wanted to have it.
  3. Built it, it showed up in the toolbox. Dragged onto my form, it looks as expected.
  4. Double-click now creates "load" event code. When wanting to change the event in the code of the form, the event CheckChanged isn't available.

So far I couldn't find out what went wrong between the first attempt and the others and why it doesn't work any more. Any ideas?

2
  • I remember the difference now: the first checkbox I didn't create as a user control, but edited in the form itself. Because of reusability, I wanted to to have it a user control. I suppose that when creating the user control the same way I modified the checkbox would have the same result. Commented Oct 31 at 10:16
  • Please edit your post if you have some extra info for us, don't hide it down here in the comments :-) Commented Nov 1 at 7:07

2 Answers 2

1

Sorry, found the solution myself. The event would have to be added to the user control itself, not the form code.

What was the goal? To create a toggle switch button from a checkbox control. This is the button. It should change its background image when clicked, so it would look like this.

The code to toggle the image is simple:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
   if (checkBox1.Checked)
   {
      checkBox1.BackgroundImage = TestWinForms.Properties.Resources.ToggleSwitch1_On;
   }
   else
   {
      checkBox1.BackgroundImage = TestWinForms.Properties.Resources.ToggleSwitch1_Off;
   }
}

Putting this into the forms' code wouldn't work, but in the user control's code it works.

Sign up to request clarification or add additional context in comments.

9 Comments

If you found the answer yourself, post the actual solution, including examples, and explanation of what you did wrong etc. Currently this answer is unlikely to help anyone else.
In my initial post I mentioned that I'm new to this forum, but a moderator cut this off. I learned this is slack, though. I'm not sure what to post, code snippets or entire files?
I know SO can unfortunately be a bit harsh to new posters. You want to post enough code that readers can understand the problem or solution, and nothing more. Ideally this would be a minimal reproducible example, but that is often not practical. It is usually also a good idea to include what the overall goal is in your question, as well as what you have tried, the result, and what you expected.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
If the goal is to change the appearance of a checkbox, you may consider overriding the drawing of a regular checkbox, either by deriving from Checkbox and overriding paint, or by adding a handler for the paint event. You may also consider switching to WPF, since configuring appearance is generally easier in WPF than winforms.
I tried WPF at first, but didn't succeed to access the control from the element host. I can only learn by either trying myself for ages or find online tutorials, but I couldn't find one.
WPF is mostly designed around MVVM and databindings. I.e. you set the DataContext of your WPF control to a "view Model" object and use databindings to bind properties on your view model to properties of your UI controls. But there is a bit of a learning curve with WPF, it is probably a good idea to follow a few tutorials to get a hang of the concepts. And trying to combine WPF and winforms will be difficult unless you know both fairly well.
You can still edit your answer (and your question) even while the post is marked as closed. Closed posts don't have to be closed forever, either. If improved they can be re-opened. Take a look around the help centre for more info
I had to wait to days before I could mark my post as solution. So far I see the problem described, the solution, some code. Don't see why the system is still considering it as not OK. SO is so hard to use.
0

My guess would be that an attribute is used to determine what the designer should do when you double click something. And looking in the documentation we can find a DefaultEventAttribute that sounds like a good candidate.

Some more searching also turns up How to set the defaul event for a user control that might be relevant.

In either case you may need to create a own custom event. This may forward the event registration to your checkbox.

[DefaultEvent("CheckedChanged")]
public class MyClass : UserControl
{
   private Checkbox myCheckbox
   
   public event EventHandler? CheckedChanged{
        add => myCheckbox.CheckedChanged += value;
        remove => myCheckbox.CheckedChanged -= value;
   }
}

1 Comment

Thanks. For switching the appearance of the that toggle switch (see my solution below) it sufficed to go without the extra event handler, but I will keep yours in mind when doing further stuff.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.