How to specify default text for a custom button control.

G

Guest

I'd like the default text to show up in designer, and allow it to be changed
to something else. I was able to change some of the other properties, but
for some reason I'm having trouble with the text. Here's the code from my
latest attempt. I initially tried just setting this.Text = "E&xit" but that
didn't work. Any ideas?


public class ButtonExit : Button
{

private ToolTip btn_ToolTip;
private System.ComponentModel.IContainer components;
public ButtonExit() : base()
{
this.components = new System.ComponentModel.Container();
this.btn_ToolTip = new ToolTip(this.components);
this.btn_ToolTip.SetToolTip(this, "Use this button to exit the
application.");
this.Enabled = true ;
this.CausesValidation = false;
base.Text = "E&xit";
}


[DefaultValue("E&xit"),
Category("Appearance"),
Description("Default Button Text")]
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
}
}

}
 
M

Maqsood Ahmed

Hello,
I don't understand why are you trying to make a custom button class,
you can do what you want by using System.Windows.Forms.Button class.
Anyways, instead to doing this whole exercise just write,

this.Text = "E&xit";

in custom button's constructor. There is no need to override text
property since you are always getting base.Text.

HTH. Cheers :)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
B

Bruce Wood

I had a similar problem once, and I'm battling to remember exactly how
I fixed it. However, try this....

Try adding the method

private bool ShouldSerializeText()
{
return base.Text == "E&xit";
}

I remember resorting to this when the DefaultValue() attribute didn't
seem to be having the correct effect on an overridden method. For some
weird reason DefaultValue() didn't work, but ShouldSerializeXxx did, so
I just stuck with it.

Give it a try and see what happens. :)
 
G

Guest

That was the first thing I tried, and it didn't work for the text property.

The purpose is to create a standard "Exit" button with a message box asking
them if they really want to exit. Then, anytime I need an exit button, I can
just drop it on the form without having to change the properties that are
going to be the same for all of the exit buttons.
 
G

Guest

Here's what finally worked.

using System;
using System.Diagnostics; // Trace and Debug.Assert
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design;


namespace WS.Windows.Forms
{

public class ButtonExitControlDesigner :
System.Windows.Forms.Design.ControlDesigner
{
[Designer(typeof(WS.Windows.Forms.ButtonExitControlDesigner))]
public class ButtonExit : Button
{
private ToolTip btn_ToolTip;
private System.ComponentModel.IContainer components;
public ButtonExit() : base()
{
this.components = new System.ComponentModel.Container();
this.btn_ToolTip = new ToolTip(this.components);
this.btn_ToolTip.SetToolTip(this, "Use this button to exit the
application.");
this.CausesValidation = false;
this.BackColor = Color.WhiteSmoke;
}


[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
}
}

protected override void OnClick( EventArgs e )
{
if (MessageBox.Show("Do you really want to exit the application? \n
(Note: Unsaved data will not be saved)?", "Exit Warning",
MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
base.OnClick( e ) ;
Form form = (Form)this.FindForm();
form.Close();
}

}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
}

public override void OnSetComponentDefaults()
{
base.OnSetComponentDefaults();
Control.Text = "E&xit";
}
}
}
 
G

Guest

Here's what finally worked.

using System;
using System.Diagnostics; // Trace and Debug.Assert
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms.Design;


namespace WS.Windows.Forms
{

public class ButtonExitControlDesigner :
System.Windows.Forms.Design.ControlDesigner
{
[Designer(typeof(WS.Windows.Forms.ButtonExitControlDesigner))]
public class ButtonExit : Button
{
private ToolTip btn_ToolTip;
private System.ComponentModel.IContainer components;
public ButtonExit() : base()
{
this.components = new System.ComponentModel.Container();
this.btn_ToolTip = new ToolTip(this.components);
this.btn_ToolTip.SetToolTip(this, "Use this button to exit the
application.");
this.CausesValidation = false;
this.BackColor = Color.WhiteSmoke;
}


[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
}
}

protected override void OnClick( EventArgs e )
{
if (MessageBox.Show("Do you really want to exit the application? \n
(Note: Unsaved data will not be saved)?", "Exit Warning",
MessageBoxButtons.YesNo,MessageBoxIcon.Question) == DialogResult.Yes)
{
base.OnClick( e ) ;
Form form = (Form)this.FindForm();
form.Close();
}

}


protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
}

public override void OnSetComponentDefaults()
{
base.OnSetComponentDefaults();
Control.Text = "E&xit";
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top