你的codes有个性吗?
你的窗口精彩吗?
你的程式与众不同吗?
那么来吧,
这里,你可以show出你的个性,贴出你的精彩。
VB.NET经典代码show(主推控件重写,XP style[不要系统支持])
请各位大虾show出个性代码,供社区研究学习。
长度不限,put者有分,希望大家涌跃参与
show.........
---------------------------------------------------------------
很简单的一个,贻笑大方,输入非数值就亮红灯,并提示
Public Class RegExTextBox
Inherits Windows.Forms.TextBox
Public Sub RegExTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Validating
Try
If Not IsNumeric((Trim(Me.Text))) Then
ErrorProvider1.SetError(Me, "输入错误,只能是数值型!")
e.Cancel = True
Me.Select(0, Me.Text.Length)
End If
Catch ex As Exception
End Try
end sub
Private Sub RegExTextBox_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Validated
ErrorProvider1.SetError(Me, "")
End Sub
---------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
namespace SuperControls
{
///
1<summary>
2/// ComboBoxWithIcon 的摘要说明。
3/// </summary>
public class ComboBoxWithIcon : ComboBox
{
///
1<summary>
2/// 必需的设计器变量。
3/// </summary>
private System.ComponentModel.Container components = null;
public ComboBoxWithIcon(System.ComponentModel.IContainer container)
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
container.Add(this);
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public ComboBoxWithIcon()
{
///
/// Windows.Forms 类撰写设计器支持所必需的
///
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
///
1<summary>
2/// 清理所有正在使用的资源。
3/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region 组件设计器生成的代码
///
1<summary>
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改
3/// 此方法的内容。
4/// </summary>
private void InitializeComponent()
{
//
// ComboBoxWithIcon
//
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
}
#endregion
#region "内部变量"
private ImageList m_imgList;
private Color m_clrSelected=Color.SteelBlue;
#endregion
#region "属性"
[Category("自定义属性")]
[Description("图像列表控件")]
public ImageList ImageList
{
get
{
return m_imgList;
}
set
{
m_imgList=value;
}
}
[Category("自定义属性")]
[Description("选择时的背景颜色")]
public Color SelectedColor
{
get
{
return m_clrSelected;
}
set
{
m_clrSelected=value;
}
}
#endregion
#region "重写方法"
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
if(m_imgList==null ¦ ¦ this.Enabled==false)
return;
try
{
Graphics g = e.Graphics ;
Rectangle r = e.Bounds ;
Size imageSize = m_imgList.ImageSize;
if ( e.Index >= 0 )
{
string s = this.Items[e.Index].ToString();
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Near;
Color backColor,foreColor;
if ( (e.State & DrawItemState.Selected) == DrawItemState.Selected )
{
backColor=m_clrSelected;
foreColor=Color.White;
}
else
{
backColor=this.BackColor;
foreColor=Color.Black;
}
//画条目背景
e.Graphics.FillRectangle(new SolidBrush(backColor) , r);
//绘制图像
if(m_imgList.Images.Count>e.Index)
m_imgList.Draw(e.Graphics, r.Left, r.Top+(r.Height-imageSize.Height)/2,e.Index);
else
m_imgList.Draw(e.Graphics, r.Left, r.Top+(r.Height-imageSize.Height)/2,0);
//显示字符串
e.Graphics.DrawString( s , this.Font , new SolidBrush(foreColor), r.Left+imageSize.Width ,r.Top+2);
//显示取得焦点时的虚线框
e.DrawFocusRectangle();
}
}
catch
{
}
base.OnDrawItem (e);
}
#endregion
}
}
---------------------------------------------------------------
我也来一个实用的,大家需要就看看。
功能:在指定的区域花一个矩形,鼠标拖动显示矩形框的。
测试方法:新建一个vb.net的windows application 工程,放一个panel控件,复制如下代码。可以测试了。必要的时候,把csdn提供的标点符号替换成半角或删除空格换行
Private StartPoint, EndPoint As New Point '保存鼠标移动的起始点和结束点
Private Sub Panel1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown '鼠标按下记录起始点
StartPoint.X = e.X
StartPoint.Y = e.Y
End Sub
Private Sub Panel1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If Not e.Button = MouseButtons.Left Then Exit Sub '鼠标移动,如果左键按下,则花一个临时的图形表示用户当前鼠标框选范围
Panel1.Refresh()
EndPoint.X = e.X
EndPoint.Y = e.Y
Dim x1, x2, y1, y2 As Int16
If EndPoint.X < StartPoint.X Then
x1 = EndPoint.X
x2 = StartPoint.X
Else
x2 = EndPoint.X
x1 = StartPoint.X
End If
If EndPoint.Y < StartPoint.Y Then
y1 = EndPoint.Y
y2 = StartPoint.Y
Else
y2 = EndPoint.Y
y1 = StartPoint.Y
End If
Dim g As Graphics
Dim pen As New Pen(Color.Black)
Dim w As Int16 = x2 - x1
Dim h As Int16 = y2 - y1
Dim rect As New Rectangle(x1, y1, w, h)
g = Panel1.CreateGraphics()
g.DrawRectangle(pen, rect)
End Sub
Private Sub Panel1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp '例子松开鼠标会花一个以短边为边长的正方形
Me.Refresh() '因为用的是panel,区域不大,可以用refresh刷新掉之前的图形再花新的
'如果区域大,可以参考区域更新
EndPoint.X = e.X '保存结束点
EndPoint.Y = e.Y
If EndPoint.X < StartPoint.X Then '计算大小,保证绘制矩形的长宽是正数
Dim t As Int16
t = StartPoint.X
StartPoint.X = EndPoint.X
EndPoint.X = t
End If
If EndPoint.Y < StartPoint.Y Then
Dim t As Int16
t = StartPoint.Y
StartPoint.Y = EndPoint.Y
EndPoint.Y = t
End If
Dim g As Graphics '规则的GDI+绘制矩形的步骤
Dim pen As New Pen(Color.Black)
Dim w As Int16 = EndPoint.X - StartPoint.X
Dim h As Int16 = EndPoint.Y - StartPoint.Y
Dim rect As New Rectangle(StartPoint.X, StartPoint.Y, w, h)
If w > h Then
rect = New Rectangle(StartPoint.X, StartPoint.Y, h, h)
Else
rect = New Rectangle(StartPoint.X, StartPoint.Y, w, w)
End If
g = Panel1.CreateGraphics()
g.DrawRectangle(pen, rect)
GR.Ephemeris(Panel1, g, True, rect)
End Sub
---------------------------------------------------------------
简单一点的控件重写:
在Textbox产生gotfocus事件时先执行背景变色,再发送gotfocus事件给控件用户。
主要学习Overrides子句以及认识基类的事件生成的方法
Public Class Tr_text
Inherits System.Windows.Forms.TextBox
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()