Custom Controls and setting default values of base propertys

  • Thread starter Thread starter TonyR
  • Start date Start date
T

TonyR

I have written a pictureButton control, but I cannot seem to set a default,
width, height and BackColor.
I am using Visual Studio 2005.
I thought you could set it in the constructor, but nothing seems to change,
the control is created as a 200x200 control
The code I have used is as follows...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace rcl.ppc.Controls
{

public partial class pictureButton : Control
{
public pictureButton()
{
InitializeComponent();
}


Bitmap image;

public Bitmap Image
{
get
{
return this.image;
}
set
{
this.image = value;
}
}

private ContentAlignment imageAlign = ContentAlignment.MiddleCenter;
public ContentAlignment ImageAlign
{
get
{
return imageAlign;
}
set
{
imageAlign = value;
this.Invalidate();
}
}


private ContentAlignment textAlign = ContentAlignment.MiddleCenter;
public ContentAlignment TextAlign
{
get
{
return textAlign;
}
set
{
textAlign = value;
this.Invalidate();
}
}

private Color disabledBackColor = Color.WhiteSmoke;

/// <value>Color.WhiteSmoke</value>
public Color DisabledBackColor
{
get
{
return disabledBackColor;
}
set
{
disabledBackColor = value;
//may need to invalidate
}
}

protected override void OnMouseDown(MouseEventArgs e)
{
this.Invalidate();
base.OnMouseDown(e);
}

protected override void OnMouseUp(MouseEventArgs e)
{
this.Invalidate();
base.OnMouseUp(e);
}

protected override void OnPaint(PaintEventArgs e)
{
Rectangle dstRect;
if (this.image != null)
{
ImageAttributes attr = new ImageAttributes();
attr.SetColorKey(this.image.GetPixel(0, 0), this.image.GetPixel(0,
0));
dstRect =
ContentAlignmentHelper.GetRectangle(this.ClientRectangle,this.image.Width,this.image.Height,imageAlign);
e.Graphics.DrawImage(this.image, dstRect, 0, 0, image.Width,
image.Height,
GraphicsUnit.Pixel, attr);
}

// Draw the text if there is any.
if (this.Text.Length > 0)
{
SizeF size = e.Graphics.MeasureString(this.Text, this.Font);
dstRect =
ContentAlignmentHelper.GetRectangle(this.ClientRectangle,Convert.ToInt32(size.Width),Convert.ToInt32(size.Height),textAlign);
// Center the text inside the client area of the PictureButton.
e.Graphics.DrawString(this.Text,
this.Font,
new SolidBrush(this.ForeColor),dstRect.X,dstRect.Y);
}

// Draw a border around the outside of the
// control to look like Pocket PC buttons.
e.Graphics.DrawRectangle(new Pen(Color.Black), 0, 0,
this.ClientSize.Width - 1, this.ClientSize.Height - 1);
if (Enabled == false)
{
e.Graphics.FillRectangle(new SolidBrush(disabledBackColor), 1, 1,
this.ClientSize.Width - 2, this.ClientSize.Height - 2);
}
base.OnPaint(e);
}

}
}
 
Thanks, that artical is helpfull, but if you put in entries for the base
class propertys they seem to be ignored
e.g.
<Property Name="Width">
<DefaultValue>
<Type>System.Int32</Type>
<Value>64</Value>
</DefaultValue>
</Property>

It also seems to ignore the category descriptions for the ones I have
created. I must be doing something wrong somewhere!
 
You need to make sure that there is a metadata assembly for the device
application platform type. In other words, the platform type (PocketPC,
Smartphone, WindowsCE) needs to match for the control project (that contains
the xmta file) and the device application project that you're using to test
the control. Assuming that you're creating a device application project
targeting PocketPC, check the control projects output directory on your dev
machine to see if you have an "asmmeta" with a name similar to
"MyControl.PocketPC.asmmeta.dll". When the designer loads a custom control
it checks to see if an "asmmeta" assembly exists for the project type of the
form being designed. There must be a platform match for the assembly that
contains the categories, descriptions, default values, etc. to be loaded.
Also, try the XML below. I know that this works for me.

<Property Name="Size">
<DefaultValue>
<Type>System.Drawing.Size, System.Drawing, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Type>
<Value>64, 20</Value>
</DefaultValue>
</Property>

<Property Name="CustomProperty">
<Category>Appearance</Category>
<Description>The description for the CustomProperty.</Description>
</Property>
 
Tim,

Thanks for your help. I recreated the project from scratch as well (that
made the categorys work) but it needed the extra info on the <Type> part for
the size to work.
The only problem is BackColor doesnt seem to work. I have tried...

<Property Name="BackColor">
<DefaultValue>
<Type>
System.Drawing.Color, System.Drawing, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
</Type>
<Value>System.Drawing.Color.AliceBlue</Value>
</DefaultValue>
</Property>

But this doesn't seem to work - I will check again as it is late unless
there is something else I need to do?

Tony
 
This is the BackColor in the way that it works for me...

<Property Name="BackColor">
<DefaultValue>
<Type>System.Drawing.Color, System.Drawing, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Type>
<Value>Black</Value>
</DefaultValue>
</Property>
 
Tim,

Thanks for that. It's working now.

Tony R

Tim Wilson said:
This is the BackColor in the way that it works for me...

<Property Name="BackColor">
<DefaultValue>
<Type>System.Drawing.Color, System.Drawing, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</Type>
<Value>Black</Value>
</DefaultValue>
</Property>
 
Back
Top