0

I have a dynamically added UserControl:

var listItem = new ListItem(/* arguments */);
listItem.Click += SetListItemColor;

panel.Controls.Add(listItem); // "panel" is FlowLayoutPanel

SetListItemColor:

private void SetListItemColor(object sender, EventArgs e)
{
    var listItem = (ListItem)sender;
    if (listItem.BackColor == Color.LightGray)
    {
        listItem.BackColor = Color.White;
    }
    else
    {
        listItem.BackColor = Color.LightGray;
    }
}

No change to the color happens when I click on the UserControl. However, for test purpose, I tried to change the event to EnabledChangedand change the Enabled property, the color does change:

var listItem = new ListItem(/* arguments */);
listItem.Enabled = false;
listItem.EnabledChanged += SetListItemColor;
listItem.Enabled = true;

panel.Controls.Add(listItem);

What's the problem?

Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • You should add delegate and event handler. Try to this link : http://stackoverflow.com/questions/2920145/how-to-work-with-delegates-and-event-handler-for-user-control – Rai Vu Nov 10 '16 at 16:08
  • @VinhVu, Thanks for the time you take to help. Why would I need to create a custom event if there is a built-in one? Especially when other events **do** work. – Michael Haddad Nov 10 '16 at 16:10
  • I just tried it and it works. Are you docking your listItem to your panel to make sure you're actually clicking on the UserControl and not the panel? – Troy Mac1ure Nov 10 '16 at 16:12
  • @TroyMac1ure - could you show me how to do that? – Michael Haddad Nov 10 '16 at 16:14

1 Answers1

1

EDIT: Since docking doesn't work in a FlowLayoutPanel, suggest setting the size of your control to the size of the panel. Set the ListItem margins to empty as below to get maximum fill. For debugging set the backcolor different to make sure you can see it:

        var listItem = new ListItem(/* arguments */);
        listItem.BackColor = Color.Yellow; // Debugging only
        listItem.Margin = Padding.Empty;
        listItem.Size = panel.Size;
        listItem.Click += SetListItemColor;

Note that if the control is resized you will need to resize your ListItem again.

Troy Mac1ure
  • 637
  • 4
  • 9
  • Tried with `DockStyle.Bottom` and it shows in yellow, but nothing happens on click. – Michael Haddad Nov 10 '16 at 16:24
  • FlowLayoutPanel doesn't like Dock.Fill or even Anchoring a control to all sides. Try adding a button at design time and setting it to Dock.Fill. Not sure if this is a bug or has some reasoning behind it. – Troy Mac1ure Nov 10 '16 at 16:59
  • Edited code to get a maximum fill on a FlowLayoutPanel. – Troy Mac1ure Nov 10 '16 at 17:31
  • Thanks, It worked. The problem is that I want the panel to be 400px and the control 45px, because I add a lot of this control to the panel, and it has an `AutoScroll` property. – Michael Haddad Nov 10 '16 at 17:33
  • Ho! Now I get it. The problem is that **inside** the control there is a `TableLayoutPanel`. When I remove it, it works. So I need to create an event for the `TableLayoutPanel` and bubble the event up to the control. Thanks a lot! (edit your answer to contain what I just said and I'll mark it as answered). – Michael Haddad Nov 10 '16 at 17:37
  • 1
    Neither FlowLayoutPanel or UserControl adds a TableLayoutPanel by default that I can see so your issue is specific to your code and I can't adjust my answer without more info. Either way, I'm glad you got it working. – Troy Mac1ure Nov 10 '16 at 18:29