C# User Controls - setting default properties

G

Guest

I have built a working user control. However, to make it work, I always have
to set certian properties using the properties sheet for the control when
using it on other forms. I want to be able to set default values for most
properties so the control will work as-is without requiring the developer to
set them.

I notice in the compment initialization code create when I drop the control
on a form that all the properties are set to null or other appropriate
'empty' values. How can I modify the control so these initial values are
correct?

Thanks!
 
D

DalePres

private bool myBoolProperty = false;

public bool MyBoolProperty
{
get { return myBoolProperty; }
set { myBoolProperty = value; }
}

HTH

DalePres
MCAD, MCDBA, MCSE
 
G

Guest

That is how the properties are implemented. The problem is not that I can't
see them on the new project. The problem is that they are being assigned null
values by the IDE. So I am guessing that I'm not doing something correctly in
the control itself to propagate the "default" values. Here are some code
snipets from the control.

public class ProgressControl1 : System.Windows.Forms.UserControl
{
private string _spinLocation = "L" ; // 'L'eft or 'R'ight
private string _pcntLocation = "R" ; // 'L'eft or 'R'ight
..
..
}
..
..
public ProgressControl1()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call

}

..
..
..
public string PBSpinLocation
{
get {return _spinLocation;}
set {_spinLocation = value;}
}
public string PBPcntLocation
{
get {return _pcntLocation;}
set {_pcntLocation = value;}
}
..
..

Now here is what the IDE does when I drop this control on a new project form:

private void InitializeComponent()
{
..
..
//
// progressControl11
//
this.progressControl11.AccessibleRole =
System.Windows.Forms.AccessibleRole.None;
this.progressControl11.BackColor = System.Drawing.Color.Plum;
this.progressControl11.Location = new System.Drawing.Point(24, 8);
this.progressControl11.Name = "progressControl11";
this.progressControl11.PBarFillColor = System.Drawing.Color.Empty;
this.progressControl11.PBAutoMax = false;
this.progressControl11.PBPcntBackColor = System.Drawing.Color.Empty;
this.progressControl11.PBPcntDisplay = true;
this.progressControl11.PBPcntFont = null;
this.progressControl11.PBPcntFontColor = System.Drawing.Color.Empty;
this.progressControl11.PBPcntLocation = null;
this.progressControl11.PBSpinBackColor = System.Drawing.Color.Empty;
this.progressControl11.PBSpinDisplay = true;
this.progressControl11.PBSpinFont = null;
this.progressControl11.PBSpinFontColor = System.Drawing.Color.Empty;
this.progressControl11.PBSpinLocation = null;
this.progressControl11.Size = new System.Drawing.Size(280, 96);
this.progressControl11.TabIndex = 0;
//
..
..
..

So you see how it forces most of the public properties of the control to
null or empty states? This is my problem!!!

HTH!

Count
 
D

DalePres

What you're looking for is a custom ControDesigner for your control which
can, among a lot of other things, create the default properties when your
control is dropped onto a form.

Look up the System.Windows.Forms.Design.ControlDesigner class and override
the OnSetComponentDefaults() method.

HTH

DalePres
MCAD, MCDBA, MCSE
 

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