Peter said:
Very difficult to say without a concise-but-complete code example. And
if you were able to produce that, you'd probably already have your answer.
Does the project even compile? If not, then you are probably missing an
assembly reference to the project containing the custom control. If it
does (as it seems from your question), then since the custom control
works fine in a test project, there is probably something wrong with the
code that references the custom control. For example, perhaps in the
Designer file for the control (*.Designer.cs, for C# projects).
I'm sure in the end it will be something reasonably easy to fix, once
you identify the problem. But unfortunately, it's not the kind of
problem that's easy to fix without having full access to the project
code and a reproducible case.
Hi!
Thanks for reply.
Code does compile and works in both projects. AFAIK it is not problem
with designer file, since control shows in run time.
It's not problem to provide custom control source code, since it's
modified Microsoft sample (ImageButton), and actually, I'm glad to share
it. However, I can't provide application source code, and I don't realy
think that problem is in source code.
So, here are some facts:
-Custom control works in both projects at runtime.
-Custom control doesn't show in "old" project in form designer.
-When you copy "old" form with custom control in it, into new project,
cusom control does show in form designer.
Therefore I believe problem is in solution or project.... any sugestions?
Some details: Code is C#, framework 3.5, VS2008, device is WindowsCE.
Best Regards!
Tomy
Custom control code:
using System;
//using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
namespace WavePortWCE
{
public partial class ctlImageButton : Control
{
private Image image;
private ImageList imageList;
private int imageListIndex;
private bool bPushed;
private Bitmap m_bmpOffscreen;
public struct TBtnColor
{
public struct TBorderColor
{
public Color UL;
public Color DR;
public Color UL1;
public Color DR1;
}
public TBorderColor Border;
}
private TBtnColor btnColor;
public TBtnColor ButtonColor
{
get
{
return btnColor;
}
set
{
btnColor = value;
}
}
public struct TPositions
{
public int Text;
public int Image;
}
private TPositions Positions;
const int cLeft = -1;
const int cMiddle = 0;
const int cRight = 1;
const int cAllign = 2;
public TPositions Position
{
get
{
return Positions;
}
set
{
Positions = value;
}
}
public Image Image
{
get
{
return image;
}
set
{
image = value;
}
}
public ImageList ImageList
{
get
{
return imageList;
}
set
{
imageList = value;
}
}
public int ImageIndex
{
get
{
return imageListIndex;
}
set
{
imageListIndex = value;
}
}
private System.Drawing.Color RGB(byte r, byte g, byte b)
{
return System.Drawing.Color.FromArgb(((int)(((byte)(r)))),
((int)(((byte)(g)))), ((int)(((byte)(b)))));
}
public ctlImageButton()
{
InitializeComponent();
bPushed = false;
//default minimal size
this.Size = new Size(21, 21);
btnColor.Border.UL = RGB(0xDD, 0xDD, 0xDD);
btnColor.Border.DR = RGB(0x40, 0x40, 0x40);
btnColor.Border.UL1 = RGB(0xEE, 0xEE, 0xEE);
btnColor.Border.DR1 = RGB(0x70, 0x70, 0x70);
Positions.Image = cLeft;
Positions.Text = cAllign;
}
private void DrawBtnLines(Graphics Grph, int CornerOffset, int
BorderOffset, Color btnBorderColorUL, Color btnBorderColorDR)
{
Pen ULPen = new Pen(btnBorderColorUL, 1);
Pen DRPen = new Pen(btnBorderColorDR, 1);
if (bPushed)
{
ULPen.Color = btnBorderColorDR;
DRPen.Color = btnBorderColorUL;
}
Grph.DrawLine(ULPen, CornerOffset + BorderOffset,
BorderOffset, Width - CornerOffset - BorderOffset - 1, BorderOffset);
Grph.DrawLine(ULPen, BorderOffset, CornerOffset +
BorderOffset, CornerOffset, BorderOffset);
Grph.DrawLine(ULPen, BorderOffset, CornerOffset +
BorderOffset, BorderOffset, Height - CornerOffset - BorderOffset - 1);
Grph.DrawLine(DRPen, Width - CornerOffset - BorderOffset -
1, BorderOffset, Width - BorderOffset - 1, CornerOffset + BorderOffset);
Grph.DrawLine(DRPen, BorderOffset, Height - CornerOffset -
BorderOffset - 1, CornerOffset + BorderOffset, Height - BorderOffset - 1);
Grph.DrawLine(DRPen, Width - BorderOffset - 1, CornerOffset
+ BorderOffset, Width - BorderOffset - 1, Height - CornerOffset -
BorderOffset - 1);
Grph.DrawLine(DRPen, Width - BorderOffset - 1, Height -
CornerOffset - BorderOffset - 1, Width - CornerOffset - BorderOffset -
1, Height - BorderOffset - 1);
Grph.DrawLine(DRPen, CornerOffset + BorderOffset, Height -
BorderOffset - 1, Width - CornerOffset - BorderOffset - 1, Height -
BorderOffset - 1);
}
private Color DisableColor(Color pColor)
{
int pColorVal;
pColorVal = (pColor.R + pColor.G + pColor.B) / 3;
return RGB((byte)pColorVal, (byte)pColorVal, (byte)pColorVal);
}
private Bitmap DisableImage(Image pImage)
{
Bitmap pBmp = new Bitmap(pImage);
int x;
int y;
Color OldColor;
Color NewColor;
if (!Enabled)
{
for (y = 0; y < pBmp.Height; y++)
{
for (x = 0; x < pBmp.Width; x++)
{
OldColor = pBmp.GetPixel(x, y);
NewColor = DisableColor(OldColor);
pBmp.SetPixel(x, y, NewColor);
}
}
}
return pBmp;
}
private Color DisableText(Color pColor)
{
if (Enabled) return pColor;
return Color.Gray;
}
protected override void
OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics gxOff; //Offscreen graphics
Rectangle imgRect; //image rectangle
Brush backBrush; //brush for filling a backcolor
int imageLeft;
int imageTop;
int imageWidth;
int textLeft;
int textTop;
int Offset = 0;
if (bPushed) Offset = 1;
if (m_bmpOffscreen == null) //Bitmap for doublebuffering
{
m_bmpOffscreen = new Bitmap(ClientSize.Width,
ClientSize.Height);
}
gxOff = Graphics.FromImage(m_bmpOffscreen);
gxOff.Clear(this.BackColor);
if (!bPushed)
backBrush = new SolidBrush(Parent.BackColor);
else //change the background when it's pressed
backBrush = new SolidBrush(Color.LightGray);
gxOff.FillRectangle(backBrush, this.ClientRectangle);
imageWidth = 0;
imageLeft = 3;
if (imageList != null)
{
if (imageList.Images[imageListIndex] != null)
{
image = imageList.Images[imageListIndex];
}
}
if (image != null)
{
imageLeft = 3 + (this.Width - image.Width) / 2;
imageTop = (this.Height - image.Height) / 2;
imageWidth = image.Width;
switch (Positions.Image)
{
case cLeft:
imageLeft = 3;
break;
case cMiddle:
imageLeft = 3 + (this.Width - image.Width) / 2;
break;
case cRight:
imageLeft = this.Width - image.Width - 3;
break;
}
imgRect = new Rectangle(imageLeft + Offset, imageTop +
Offset, image.Width, image.Height);
//Set transparent key
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(BackgroundImageColor(image),
BackgroundImageColor(image));
//Draw image
//gxOff.DrawImage(image, imgRect, 0, 0, image.Width,
image.Height, GraphicsUnit.Pixel, imageAttr);
gxOff.DrawImage(DisableImage(image), imgRect, 0, 0,
image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
}
//Text
SizeF TextSize = gxOff.MeasureString(Text, Font);
textLeft = (this.Width - (int)TextSize.Width) / 2;
textTop = (this.Height - (int)TextSize.Height) / 2;
switch (Positions.Text)
{
case cLeft:
textLeft = 0;
break;
case cMiddle:
textLeft = (this.Height - (int)TextSize.Height) / 2;
break;
case cRight:
textLeft = this.Width - (int)TextSize.Width;
break;
case cAllign:
textLeft = imageLeft + imageWidth;
break;
}
gxOff.DrawString(Text, Font, new
SolidBrush(DisableText(ForeColor)), textLeft + Offset, textTop + Offset);
//gxOff.DrawRectangle(BorderPen, 0, 0, Width - 1, Height - 1);
DrawBtnLines(gxOff, 1, 1, btnColor.Border.UL1,
btnColor.Border.DR1);
DrawBtnLines(gxOff, 2, 0, btnColor.Border.UL,
btnColor.Border.DR);
//Draw from the memory bitmap
e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
base.OnPaint(e);
}
protected override void
OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
{
//Do nothing
}
protected override void
OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
bPushed = true;
this.Invalidate();
}
protected override void
OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
bPushed = false;
this.Invalidate();
}
private Color BackgroundImageColor(Image image)
{
Bitmap bmp = new Bitmap(image);
return bmp.GetPixel(0, 0);
}
}
}