robalexclark

Hi all,

I'm wondering whether any of you people can help me out with this problem that I have with a user control that I have been trying to get working.

The first part of the user control is a flat circular button ( called SampleButton) that toggles colour when clicked. This control has an OnClick method as follows

protected override void OnClick(EventArgs e)
{
base.OnClick(e);

(code to change colour and call invalidate....)
}

Then, from this I am creating a matrix of these buttons in another user control that inherits from SampleButton (which in turn inherits from UserControl), e.g. a 3 x 3 matrix, like so:

O O O

O O O

O O O

I do this using a couple of for loops (the number of SampleButtons has to be dynamic), a bit like:

for (int i = 0; i <= 2; i++)
{
for (int j = 0; j <= 2; j++)
{
SampleButton samBtn = new SampleButton();
samBtn.Location = new Point(i, j);

this.Controls.Add(samBtn);

}
}
}

In this control I have an OnClick method:

protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("The Sample Button Array has been clicked");
}

The problem is that although when I click on the array of sample buttons, their colour changes (so the first OnClick method is running ok), the messagebox never appears (the method is never entered).

Why does this second OnClick method never get called

Thanks,

Rob



Re: Windows Forms General Complex user control not responding to click events

nobugz

You'll need to let SampleButton generate an event so that the UserControl with the grid of buttons can be notified that it saw a click. Having that UserControl inherit SampeButton is not correct BTW. Here's an example:

public partial class SampleButton : UserControl {
public event EventHandler SampleClick;
public SampleButton() {
InitializeComponent();
}
protected override void OnClick(EventArgs e) {
base.OnClick(e);
// Do your stuff
//...
if (SampleClick != null) SampleClick(this, EventArgs.Empty);
}
}

public partial class ButtonArray : UserControl {
public ButtonArray() {
InitializeComponent();
sampleButton1.SampleClick += Sample_Click;
sampleButton2.SampleClick += Sample_Click;
// etc..
}
private void Sample_Click(object sender, EventArgs e) {
// One of the sample buttons was clicked, you could generate a Click event:
this.OnClick(e);
}
}






Re: Windows Forms General Complex user control not responding to click events

robalexclark

Thanks for your reply. I've just implemented it on SampleButton and it works!

I think I understand why ButtonArray should inherit from UserControl rather than SampleArray - I initially did it because I thought that I could somehow get to the information in SampleButton from the form if I inherited it (wrong of course)

I have information attached to each of my buttons in the Tag property of the button. I have created my own SampleButtonEventArgs class to allow this Tag information to be accessed in the event. I need to get this Tag information into my form (where the ButtonArray is present):

SampleButton --> ButtonArray --> Form

At present I've managed to raise the event from SampleButton and handle it in ButtonArray. To get the information into my form do I need to do the same steps here I.e. raise the event from ButtonArray and handle it in my main form What is the best practise in these situations

Thanks agaim.

Rob





Re: Windows Forms General Complex user control not responding to click events

nobugz

Yes, do it the same way. Don't use the Click event, it will also fire when another part of the control is clicked.