Problem with Custom Button (.net CF 2.0)

N

Norbert

I d'like to create a custom Button with two lines of text
Where the second one has a smaller font.

I've done following steps:
Added a new Item (Custom Control) to my Projekt

After this, i changed the parent class of the control to BUTTON
Code of this class:
/////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PPC_TEST_APP
{
public partial class CustomControl1 : Button
{
public CustomControl1()
{
InitializeComponent();
}

protected override void OnPaint(PaintEventArgs pe)
{
// TODO: Add custom paint code here
SizeF textsize = pe.Graphics.MeasureString(Text,
this.Font);
Font myFont = new Font(this.Font.Name, (float)7.0,
this.Font.Style);

Brush myBrush = new SolidBrush (this.ForeColor);
RectangleF myRect= new
RectangleF((textsize.Height),0,this.Width,this.Height-textsize.Height);
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;

// Calling the base class OnPaint
//base.OnPaint(pe);
pe.Graphics.DrawString("Hello World", myFont, myBrush,
myRect, sf);
}
}
}
/////////////////////////////////////////////////////

After adding this to a Form (drag & drop through designer)
I get a wired behavior.
The OnPaint Method never gets called (tried to debug)

Somebody knows why?
Isn't it possible to derive from a Button?
Or must I create a nwe Button class from the scratch?

regards
Norbert
 
P

Peter Foot [MVP]

Since the Button class is a wrapper for the native button control, and you
want to do custom drawing, derive from Control instead, your paint code
should then get called as expected.

Peter
 
N

Norbert

Hello

I Implemented a Version of my two Text Line Button
its working so far, but there is still a problem
In a Form I registering the MyExButton.Click Event
with this method:
private void MyExButton1_Click(object sender, EventArgs e)
{
this.Close();
}

I want to close the Form when the Button is clicked.
But if i do this, i get an ObjectDisposing Exception in the
OnMouseUp Method

Somebody can tell me an workaround for this?
or must I also override the Dispose Method?

Norbert

Ps.:
The Code of my Custom Button Class:

//////////////////////////////////////////////////////////////7
using System;
using System.Drawing;
using System.Windows.Forms;

namespace PPC_TEST_APP
{
public partial class MyExButton : UserControl
{
protected Color backupBackgroundColor;
protected Color backupForgroundColor;
protected Font subFont;
protected string sub_text;
protected string main_text;

protected Rectangle sub_rect;
protected Rectangle main_rect;

public MyExButton()
{
subFont = this.Font;
InitializeComponent();
main_rect = new Rectangle(0,0,this.Width,1);
sub_rect = new Rectangle(0,0,this.Width,1);
}

protected override void OnPaint(PaintEventArgs e)
{
Pen mypen = new Pen(base.ForeColor);
Rectangle myrect = this.ClientRectangle;
myrect.Height -= 1;
myrect.Width -= 1;
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
e.Graphics.DrawRectangle(mypen, myrect);
e.Graphics.DrawString(main_text, this.Font, new
SolidBrush(ForeColor), main_rect, sf);
e.Graphics.DrawString(sub_text, subFont, new
SolidBrush(ForeColor), sub_rect, sf);
}
public override Font Font
{
get
{
return base.Font;
}
set
{
base.Font = value;
RecalcMainRect();
}
}
public override Color BackColor
{
get
{
return base.BackColor;
}
set
{
base.BackColor = value;
}
}
public override Color ForeColor
{
get
{
return base.ForeColor;
}
set
{
base.ForeColor = value;
}
}

public string MainText
{
get { return main_text; }
set { main_text = value;
RecalcMainRect();
Invalidate();
}
}
public string SubText
{
get { return sub_text; }
set { sub_text = value;
RecalcSubRect();
}
}

public Font SubFont
{
get { return subFont; }
set
{
subFont = value;
RecalcSubRect();
Invalidate();
}
}

private void RecalcMainRect()
{
Graphics g = CreateGraphics();
SizeF main_size = g.MeasureString(main_text, this.Font);
main_rect.Width = this.Width;
main_rect.Height = (int)(main_size.Height + 0.5f);
}
private void RecalcSubRect()
{
Graphics g = CreateGraphics();
SizeF sub_size = g.MeasureString(sub_text, subFont);
sub_rect.Height = (int)(sub_size.Height + 0.5f);
sub_rect.Width = this.Width;
sub_rect.Y = this.Height - sub_rect.Height;
}

protected override void OnResize(EventArgs e)
{
RecalcSubRect();
RecalcMainRect();
base.OnResize(e);
}

protected override void OnMouseDown(MouseEventArgs e)
{
//Switch Colors..
this.backupBackgroundColor = this.BackColor;
backupForgroundColor = this.ForeColor;
base.BackColor = SystemColors.ControlDarkDark;
base.ForeColor = SystemColors.HighlightText;
Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.BackColor = this.backupBackgroundColor;
base.ForeColor = backupForgroundColor;

Invalidate();
}
}
}
//////////////////////////////////////////////////////////////7
 
N

Norbert

I stumbeld upon a problem:

I created this Usercontrol in a PPC Project
There it works just fine,
But i copied the file to another project (My actual PPC Application)

There i get in the designer only a box with the Class Name
The Contet isn't drawn.

If I start the application I everything seems ok (its drawn right,

What I've missing?
I tried to find some differnces, but there are none.

hopefully somebody can help
Norbert
 

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