Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DefaultValue Attribute not working

Status
Not open for further replies.

keyser456

IS-IT--Management
Nov 21, 2003
73
US
I have a custom combobox and have the Text property overridden as listed below:

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

When I add one of these custom comboboxes to the designer the Text property is still listing the name of the control (ex. "customCombobox1" instead of "") What am I doing wrong? No exceptions or errors occur on complilation or execution.
 
You should have something like that:

public class MyTextBox : System.Windows.Forms.TextBox
{
public MyTextBox()
{
}
[DefaultValue("blablabla")]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}

Now use MyTextBox and will work :

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox MyTextBox1;
//
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.MyTextBox1 = new MyTextBox();
//
// textBox1
//
this.MyTextBox1.Location = new System.Drawing.Point(88, 104);
this.MyTextBox1.Name = "MytextBox1";
this.MyTextBox1.TabIndex = 0;
// ...
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.MyTextBox1});
//...
}
static void Main()
{
Application.Run(new Form1());
}
This will show the form with the "blablabla" in the textbox.
-obislavu-
 
Try using String.Empty instead of "" for your default value if you want it to start off empty.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Obislavu - I am using a ComboBox and have everything you listed. I listed the exactly same property declaration in my initial post. When I drop the combobox on the form it DOES NOT use an empty string as the initial text value. After much frustration I tried overriding the ResetText method. Interestingly enough, when I right click the Text property and click Reset it will insert whatever value I had listed in the DefaultValue attribute.

Chip - When I try:

[DefaultValue(String.Empty)]

It generates an error upon compilation stating:
An attribute argument must be a constant expression, typeof expression or array creation expression
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top