Connecting Tech Pros Worldwide Help | Site Map

How to specify default text for a custom button control.

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 17th, 2005, 02:17 AM
Vern
Guest
 
Posts: n/a
Default How to specify default text for a custom button control.

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;
}
}

}


  #2  
Old November 17th, 2005, 02:17 AM
Maqsood Ahmed
Guest
 
Posts: n/a
Default Re: How to specify default text for a custom button control.

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

*** Sent via Developersdex http://www.developersdex.com ***
  #3  
Old November 17th, 2005, 02:17 AM
Bruce Wood
Guest
 
Posts: n/a
Default Re: How to specify default text for a custom button control.

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. :)

  #4  
Old November 17th, 2005, 02:20 AM
Vern
Guest
 
Posts: n/a
Default Re: How to specify default text for a custom button control.

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.

"Maqsood Ahmed" wrote:
[color=blue]
> 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
>
> *** Sent via Developersdex http://www.developersdex.com ***
>[/color]
  #5  
Old November 17th, 2005, 02:34 AM
Vern
Guest
 
Posts: n/a
Default RE: How to specify default text for a custom button control.

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.ButtonExitControl Designer))]
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(DesignerSerializat ionVisibility.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";
}
}
}


"Vern" wrote:
[color=blue]
> 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;
> }
> }
>
> }
>[/color]
  #6  
Old November 17th, 2005, 02:34 AM
Vern
Guest
 
Posts: n/a
Default RE: How to specify default text for a custom button control.

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.ButtonExitControl Designer))]
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(DesignerSerializat ionVisibility.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";
}
}
}




"Vern" wrote:
[color=blue]
> 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;
> }
> }
>
> }
>[/color]
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.