F
Filipe Marcelino
Hi,
I'm trying to create a textbox inheriting from the standard textbox. I
would like to:
1. repaint the textbox border;
2. define a color for that border;
Till now I made this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace Library.Controls
{
public class TextBox : System.Windows.Forms.TextBox
{
private enum MouseState
{
Focused,
Hoover,
Normal
}
private Color borderColor = Color.FromArgb(128, 128, 128);
//Gray
private Color highlightColor = Color.FromArgb(10, 36, 106);
//Blue
private Color hoverColor = Color.Silver; //Silver
private MouseState m_MouseState = MouseState.Normal;
public TextBox()
{
this.MouseEnter += new EventHandler(OnMouseEnter);
this.MouseLeave += new EventHandler(OnMouseLeave);
this.MouseHover += new EventHandler(OnMouseHover);
this.Enter += new EventHandler(OnEnter);
this.Leave += new EventHandler(OnLeave);
this.BorderStyle = BorderStyle.FixedSingle;
SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString(Text, Font, new
SolidBrush(ForeColor), new PointF(0, 0));
switch (m_MouseState)
{
case MouseState.Focused:
e.Graphics.DrawRectangle(new
System.Drawing.Pen(highlightColor), 0, 0, Width - 1, Height - 1);
break;
case MouseState.Hoover:
e.Graphics.DrawRectangle(new
System.Drawing.Pen(highlightColor), 0, 0, Width - 1, Height - 1);
break;
case MouseState.Normal:
e.Graphics.DrawRectangle(new
System.Drawing.Pen(borderColor), 0, 0, Width - 1, Height - 1);
break;
}
}
#region Mouse Events
private void OnMouseEnter(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Hoover;
Invalidate();
}
private void OnMouseLeave(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Normal;
Invalidate();
}
private void OnMouseHover(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Hoover;
Invalidate();
}
#endregion
private void OnEnter(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Focused;
Invalidate();
}
private void OnLeave(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Normal;
Invalidate();
}
}
}
The problem is related with the text drawing. Somehow, when editing,
the textbox suddenlly uses other font then the default causing
missreading.
There's any way to solve this situation.
Thanks in advance for your attention,
Filipe Marcelino
I'm trying to create a textbox inheriting from the standard textbox. I
would like to:
1. repaint the textbox border;
2. define a color for that border;
Till now I made this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
namespace Library.Controls
{
public class TextBox : System.Windows.Forms.TextBox
{
private enum MouseState
{
Focused,
Hoover,
Normal
}
private Color borderColor = Color.FromArgb(128, 128, 128);
//Gray
private Color highlightColor = Color.FromArgb(10, 36, 106);
//Blue
private Color hoverColor = Color.Silver; //Silver
private MouseState m_MouseState = MouseState.Normal;
public TextBox()
{
this.MouseEnter += new EventHandler(OnMouseEnter);
this.MouseLeave += new EventHandler(OnMouseLeave);
this.MouseHover += new EventHandler(OnMouseHover);
this.Enter += new EventHandler(OnEnter);
this.Leave += new EventHandler(OnLeave);
this.BorderStyle = BorderStyle.FixedSingle;
SetStyle(ControlStyles.UserPaint, true);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString(Text, Font, new
SolidBrush(ForeColor), new PointF(0, 0));
switch (m_MouseState)
{
case MouseState.Focused:
e.Graphics.DrawRectangle(new
System.Drawing.Pen(highlightColor), 0, 0, Width - 1, Height - 1);
break;
case MouseState.Hoover:
e.Graphics.DrawRectangle(new
System.Drawing.Pen(highlightColor), 0, 0, Width - 1, Height - 1);
break;
case MouseState.Normal:
e.Graphics.DrawRectangle(new
System.Drawing.Pen(borderColor), 0, 0, Width - 1, Height - 1);
break;
}
}
#region Mouse Events
private void OnMouseEnter(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Hoover;
Invalidate();
}
private void OnMouseLeave(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Normal;
Invalidate();
}
private void OnMouseHover(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Hoover;
Invalidate();
}
#endregion
private void OnEnter(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Focused;
Invalidate();
}
private void OnLeave(object sender, System.EventArgs e)
{
m_MouseState = MouseState.Normal;
Invalidate();
}
}
}
The problem is related with the text drawing. Somehow, when editing,
the textbox suddenlly uses other font then the default causing
missreading.
There's any way to solve this situation.
Thanks in advance for your attention,
Filipe Marcelino