Problem Implementing IButtonControl
I have a control which acts like a button in some regards, now to recieve information on when a control is the default button the SDK says to implement the IButtonControl which I did. The problem is that if I place three of my button controls on a form and then set the second one placed on the form as the default button (using the AcceptButton property on the parent form). Everything seems to work fine in the designer view but when I run my test application the button that was placed on the form first becomes the default button.
I placed trace code in the IButtonControl method NotifyDefault. NotifyDefault is called by the parent form when the control is being set as default or having default status removed from it. Anyway my NotifyDefault code looked like this
public void NotifyDefault(bool isDefault)
{
defaultButton=isDefault;
System.Diagnostics.Trace.WriteLine("The buttons caption is: "+this.Text);
System.Diagnostics.Trace.WriteLine("The value of isDefault is: "+ isDefault.ToString());
Invalidate();
}
defaultButton is a private bool that I use to determine how I should paint. This function simply sets it accordingly.
Anyway my trace results follow (Second Placed was made the Default button with AcceptButton)
The buttons caption is: Second Placed
The value of isDefault is: True
The buttons caption is: Second Placed
The value of isDefault is: False
The buttons caption is: First Placed
The value of isDefault is: True
As you can see it sets the appropriate button as default and then unsets it and makes the button first placed on the form the default button.
My control inherits from Control and also from IButtonControl.
Any help is greatly appreciated cause I dont know what I can try to fix this.
A few things, first off, if you create a class that inherits from System.Windows.Forms.Button does it exhibit the same bug?
Anothing thing to try, <a href="http://windowsforms.net/downloads/StateButtonBase.cs.txt">this sample</a> is a button base class that I use all the time when making button controls. Does this class also exhibit the bug?
Finally, if you're not seeing the bug with either of the above implementations, perhaps you could post your implemenation and I'll take a look.
thanks
- mike
First of all I would like to thank you for your reply Mike.
I derived from Button and the same thing happened, however when I place two regular Button components on the same form the same thing happens. Even creating a new solution and adding some buttons to the form and setting AcceptButton to one of the buttons doesn't work. The first button placed on the form is always the default one, what am I forgetting (You dont get this effect do you). I am using Windows XP Pro SP1, I am not Using Visual Styles (aka Windows XP themes).
Also when trying to determine the problem, I changed the Tab Index property so that choosen default button had the highest TabIndex. The buttons then functioned the way I expected.
To test this I placed two buttons on a form button1 and button2, then I placed a checkbox1 on the form. After this I placed another four buttons on the form. I set the forms AcceptButton to button2, this seemed to work in the designer. I then set a method to the OnClick event in button2 so that it would display a message box saying clicked.
I executed my program and pressed the spacebar and saw that button1 was the currently selected button. I exited the program and changed the TabIndex values of button1, button2 and checkbox1 so that checkbox1 was 0, button2 was 1 and button1 was 2. Executed my program and the checkbox responded to my key presses.
Seems to me that te form is setting the default control to whatever the control with the lowest TabIndex value is.
I am fairly sure that my implementation of IButtonControl is correct and I am not setting some property on the form correctly but here it is anyway.
void IButtonControl.NotifyDefault(bool isDefault)
{
defaultButton=isDefault;
//isDefault is a field declared as a private bool. This is used when painting the button to
//determine how the button is painted. If I am right this function should be called by the form.
Invalidate();
}
public void PerformClick()
{
OnClick(EventArgs.Empty);
}
[Browsable(true),DefaultValue(typeof(DialogResult), "DialogResult.None"),Description("Specifies the dialog result that this button will return when a form is closed by clicking it."),Category("Behavior")]
public DialogResult DialogResult
{
get
{
return dialogResult;
}
set
{
dialogResult=value;
}
}
Again any help is appreciated.