Windows Develop Bookmark and Share   
 index > Windows Forms Designer > help: Creating Custom ToolStripButton, implementing IButtonControl
 

help: Creating Custom ToolStripButton, implementing IButtonControl

Hello everyone.

i got pretty frustrated from the fact that toolstripbutton can't be the Accept/Cancel button of the form... after a long time of walking-around the problem (pretty "ugly" ways to do that), i decided to try and create a custom one to support this.

i pretty much got it all done, but it still doesn't work and i don't know why. (i can choose the control as the "Accept Button", but it does nothing)

here is the code: (the message boxes are for tracking purposes)

Code Snippet

public partial class CustomToolstripButton : ToolStripButton, IButtonControl

{

public CustomToolstripButton()

: base()

{

base.Click += new EventHandler(CustomToolstripButton_Click);

}

void CustomToolstripButton_Click(object sender, EventArgs e)

{

MessageBox.Show("CustomToolstripButton_Click");

OnClick(EventArgs.Empty);

}

#region IButtonControl Members

private DialogResult dialogResult;

DialogResult IButtonControl.DialogResult

{

get

{

return this.dialogResult;

}

set

{

if (!IsEnumValid(value, (int)value, 0, 7))

{

throw new InvalidEnumArgumentException("value", (int)value, typeof(DialogResult));

}

this.dialogResult = value;

}

}

public static bool IsEnumValid(Enum enumValue, int value, int minValue, int maxValue)

{

return ((value >= minValue) && (value <= maxValue));

}

void IButtonControl.NotifyDefault(bool value) { }

void IButtonControl.PerformClick()

{

MessageBox.Show("IButtonControl.PerformClick()");

OnClick(new EventArgs());

}

protected override void OnClick(EventArgs e)

{

MessageBox.Show("OnClick");

Form form = FindFormInternal();

if (form != null)

{

form.DialogResult = this.dialogResult;

}

base.OnClick(EventArgs.Empty);

}

internal Form FindFormInternal()

{

Control parentInternal = this.Owner as Control;

while ((parentInternal != null) && !(parentInternal is Form))

{

parentInternal = parentInternal.Parent;

}

return (Form)parentInternal;

}

#endregion

}

hope youcan help. thanks in adavance.

Nissim Trabelsy  Wednesday, May 21, 2008 6:32 AM

I'm having exactlythe same problem, Hope someone can help us.

Thanks in advance!

Benjamin Eidelman  Thursday, September 25, 2008 6:46 PM

You can use google to search for other answers

Custom Search

More Threads

• Changing culture problem
• Custom MenuItem
• How to Create Button which have ecliptic form
• DesignerHost: Can you set a control to be non-resizable?
• Bug in Forms Designer
• Programmatically Assign Unique Name to Controls
• Smart tags not show for custom control designer
• Fixing a change in the designer
• Raising events from extern form
• Is there a way to make a user control act like a containter control