ComBobox、TextBox控件扩展(Enable=false,灰色)

ComBobox、TextBox控件扩展(Enable=false,灰色)
o_ComBobox_TextBox.gif

注:
1.ComBoboxReadOnly控件主要是在ComBobox基础上扩展了ReadOnly属性、OnReadOnlyChanged事件。
2.通过这两个控件可以熟悉控件开发,掌握自定义控件属性、事件的处理。
3.控件源码如下:

TextBoxEnable.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using System.Drawing;
None.gif
namespace pcd.Tools
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
public class TextBoxEnable : System.Windows.Forms.TextBox
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
InBlock.gif        
public TextBoxEnable()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.BorderStyle = BorderStyle.FixedSingle;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
//重写OnEnabledChanged   
InBlock.gif
        protected override void OnEnabledChanged(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (Enabled == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SetStyle(ControlStyles.UserPaint, 
true);             
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SetStyle(ControlStyles.UserPaint, 
false);
InBlock.gif                
this.Font = new Font("宋体", 9F);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.OnEnabledChanged(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//重写OnPaint   
InBlock.gif
        protected override void OnPaint(PaintEventArgs pe)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnPaint(pe);
InBlock.gif            
if (Enabled == false)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pe.Graphics.FillRectangle(
new SolidBrush(SystemColors.ControlLight),
InBlock.gif                pe.ClipRectangle);
InBlock.gif                
//文字描画   
InBlock.gif
                float x = 0, y = 0;
InBlock.gif                Size s 
= pe.Graphics.MeasureString(Text, Font).ToSize();
InBlock.gif                
if (this.TextAlign == HorizontalAlignment.Left)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    x 
= 1;//←                  
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else if (this.TextAlign == HorizontalAlignment.Right)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    x 
= Width - s.Width;//→                  
ExpandedSubBlockEnd.gif
                }

InBlock.gif                
else if (this.TextAlign == HorizontalAlignment.Center)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    x 
= (Width - s.Width) / 2;
ExpandedSubBlockEnd.gif                }

InBlock.gif                y 
= 5.5F;
InBlock.gif             
InBlock.gif              
if (this.Multiline)
InBlock.gif                    pe.Graphics.DrawString(
this.Text, this.Font, Brushes.Black, new RectangleF(x, y, this.Width, this.Height));
InBlock.gif                
else
InBlock.gif                    pe.Graphics.DrawString(
this.Text, this.Font, Brushes.Black, new RectangleF(x, y, 1000this.Height));//单行是,width可以设置成无限大。
InBlock.gif
                pe.Graphics.DrawRectangle(new Pen(Color.Black, 2), new Rectangle(00this.Width, this.Height));//pe.ClipRectangle
InBlock.gif

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


ComBoboxReadOnly.cs
ContractedBlock.gif ExpandedBlockStart.gif Code
None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
using System.Windows.Forms;
None.gif
using System.Drawing;
None.gif
using System.ComponentModel;
None.gif
namespace pcd.Tools
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
// 2.声明一个委托。
InBlock.gif
    public delegate void ReadOnlyEventHandler(object sender, ReadOnlyEventArgs args);
InBlock.gif    
public class ComboboxReadOnly : ComboBox
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ReadOnly#region ReadOnly
InBlock.gif        
private bool _ReadOnle;
InBlock.gif        [Bindable(
true),
InBlock.gif       Category(
"Data"),
InBlock.gif       DefaultValue(
false),
InBlock.gif       Description(
"获取或者设置是否只读"),
InBlock.gif       DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
InBlock.gif        
public bool ReadOnly
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this._ReadOnle;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                ReadOnlyEventArgs roea 
= new ReadOnlyEventArgs(this._ReadOnle != value);
InBlock.gif                
this._ReadOnle = value;
InBlock.gif                OnReadOnlyChanged(roea);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
定义事件readonlychanged#region 定义事件readonlychanged
InBlock.gif        
// 3.定义事件
InBlock.gif
        [
InBlock.gif       Description(
"在更改控件的只读状态时发生。"),
InBlock.gif       DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
InBlock.gif        
public event ReadOnlyEventHandler readonlychanged;
InBlock.gif        
// 4.事件发生后,通知其他对象的方法
InBlock.gif        
//这个方法是受保护的虚方法
InBlock.gif
        protected virtual void OnReadOnlyChanged(ReadOnlyEventArgs args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
if (ReadOnly)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SetStyle(ControlStyles.UserPaint, 
true);               
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SetStyle(ControlStyles.UserPaint, 
false);
InBlock.gif                
this.Font = new Font("宋体", 9F);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
if (readonlychanged != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                readonlychanged(
this, args);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//***
InBlock.gif
            Invalidate();//强制一个控件触发onPaint事件
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
构造函数#region 构造函数
InBlock.gif        
public ComboboxReadOnly()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
this.DropDownStyle = ComboBoxStyle.DropDownList;
InBlock.gif          
this.TabStop = false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
override#region override
InBlock.gif        
//重写WndProc
InBlock.gif
        protected override void WndProc(ref Message m)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (this._ReadOnle && (m.Msg == 0xa1 || m.Msg == 0x201 || m.Msg == 0x203))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.WndProc(ref m);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//重写OnPaint   
InBlock.gif
        protected override void OnPaint(PaintEventArgs pe)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnPaint(pe);
InBlock.gif            
if (this.ReadOnly)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                pe.Graphics.FillRectangle(
new SolidBrush(SystemColors.ControlLight),
InBlock.gif                  pe.ClipRectangle);
InBlock.gif                
//文字描画   
InBlock.gif
                int x = 0, y = 0;
InBlock.gif                Size s 
= pe.Graphics.MeasureString(Text, Font).ToSize();
InBlock.gif                y 
= (Height - s.Height) / 2;
InBlock.gif                x 
= 1;//
InBlock.gif
                pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black, x, y);
InBlock.gif                pe.Graphics.DrawRectangle(
new Pen(Color.Black, 2), new Rectangle(00this.Width, this.Height));//pe.ClipRectangle
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
重写OnEnabledChanged也可以。不过用readonly更好#region 重写OnEnabledChanged也可以。不过用readonly更好
InBlock.gif        
//重写OnEnabledChanged   
InBlock.gif        
//protected override void OnEnabledChanged(EventArgs e)
InBlock.gif        
//{
InBlock.gif        
//    if (Enabled == false)
InBlock.gif        
//    {
InBlock.gif        
//        SetStyle(ControlStyles.UserPaint, true);
InBlock.gif        
//    }
InBlock.gif        
//    else
InBlock.gif        
//    {
InBlock.gif        
//        SetStyle(ControlStyles.UserPaint, false);
InBlock.gif        
//    }
InBlock.gif        
//    base.OnEnabledChanged(e);
InBlock.gif        
//}
InBlock.gif
InBlock.gif        
//重写OnPaint   
InBlock.gif        
//protected override void OnPaint(PaintEventArgs pe)
InBlock.gif        
//{
InBlock.gif        
//base.OnPaint(pe);           
InBlock.gif        
//if (Enabled == false)
InBlock.gif        
//{
InBlock.gif
InBlock.gif        
//    pe.Graphics.FillRectangle(new SolidBrush(SystemColors.ControlLight),
InBlock.gif        
//    pe.ClipRectangle);
InBlock.gif        
//    //文字描画   
InBlock.gif        
//    int x = 0, y = 0;
InBlock.gif        
//    Size s = pe.Graphics.MeasureString(Text, Font).ToSize();
InBlock.gif        
//    // x = Width - s.Width;//
InBlock.gif        
//    y = (Height - s.Height) / 2;
InBlock.gif        
//    x = 1;//
InBlock.gif        
//    pe.Graphics.DrawString(this.Text, this.Font, Brushes.Black, x, y);
InBlock.gif        
//    pe.Graphics.DrawRectangle(new Pen(Color.Black, 2), pe.ClipRectangle);
InBlock.gif
InBlock.gif        
//}
InBlock.gif        
// }
ExpandedSubBlockEnd.gif
        #endregion

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif    
1.定义一个携带事件信息的类。#region 1.定义一个携带事件信息的类。
InBlock.gif    
public class ReadOnlyEventArgs : EventArgs
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private bool readvalue;
InBlock.gif        
public bool ReadValue
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn readvalue; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public ReadOnlyEventArgs(bool readonlyvalue)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.readvalue = readonlyvalue;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

ExpandedBlockEnd.gif}



转载于:https://www.cnblogs.com/jdmei520/archive/2009/01/05/1369344.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值