473,395 Members | 1,783 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

}

Nov 17 '05 #1
5 6705
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 ***
Nov 17 '05 #2
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. :)

Nov 17 '05 #3
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:
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 ***

Nov 17 '05 #4
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:
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;
}
}

}

Nov 17 '05 #5
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:
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;
}
}

}

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Paul | last post by:
Hello everyone: I am developing a VB.Net Windows Application and I am now ready to create the deployment project for it. This application needs to be installable on a different number of users...
1
by: Robert Neville | last post by:
I am having some trouble with some old code revolving around custom form navigation buttons. My main form has a sub-form with these custom navigation buttons. In other words, the code should be...
3
by: KJ | last post by:
I have seen examples on how to set the default button on a ASPX page with asp server controls Sub Page_Load() Page.RegisterHiddenField("__EVENTTARGET", "btnSearch") End Sub But that does...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
0
by: Terp | last post by:
Hello all, Here is what I need to do: I need a custom text control of some sort that has the following characteristics: 1. Displays plain text. 2. Never shows scrollbars. 3. Only displays...
2
by: Rolf Welskes | last post by:
Hello, I have writen a simple aspnet control MyCtrl and want to use it as follows: (c01 may be the tag-prefix): <c01:MyCtrl>this is a simple text</c01:MyCtrl>. The control-code:
1
by: JJ | last post by:
What trouble I'm having setting default buttons on a page (with a master page). I got around doing it on a page basis by calling the SetWizardDefaultButton() routine from page load. Notice how...
1
by: rn5a | last post by:
I have created a custom control button which when clicked displays a message in the JavaScript alert dialog. I could successfully compile the VB class file into a DLL & also could add it to the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.