一个非常好的 TextBox与Button组合体

作者在 2008-04-11 13:27:54 发布以下内容

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HVS.WinUI.Forms;

namespace HVS.WinUI.Components
{
    public partial class TextEdit : System.Windows.Forms.TextBox
    {

        #region 事件
        public delegate void CancelLookupEventHandler(object sender, CancelLookupEventArgs e);
        public delegate void LookupEventHandler(object sender, LookupEventArgs e);

        public event CancelLookupEventHandler BeforeLookup;
        public event LookupEventHandler AfterLookup;
        LookupEventArgs AfterLookupArgs = new LookupEventArgs();
        #endregion

        #region 属性
        Button LookUpbtn = new Button();
        Dictionary<string, string> Columus = new Dictionary<string, string>();

        private LookUp _lookup = new LookUp();
        PictureBox picRequired = new PictureBox();
        private bool _isRetureLookUpColumn = false;
        private bool _isShowButton = false;
        private string _drillDown = string.Empty;
        private bool _required = false;

        [Browsable(true)]
        [DefaultValue(null)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Category("hvs")]
        [TypeConverterAttribute(typeof(ExpandableObjectConverter))]
        public LookUp Lookup
        {
            get { return _lookup; }
            set { _lookup = value; }
        }

        public string DrillDown
        {
            get { return _drillDown; }
            set { _drillDown = value; }
        }

        public bool Required
        {
            get { return _required; }
            set { _required = value; }
        }

        #endregion

        #region 构造函数
        private System.ComponentModel.IContainer components = null;
        public TextEdit()
        {
            components = new System.ComponentModel.Container();
        }

        public TextEdit(IContainer container)
        {
            container.Add(this);
            components = new System.ComponentModel.Container();
        }
        #endregion

        #region TextEdit
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
            if (_isShowButton)
                this.LookUpbtn.Visible = true;
            if (this.Required)
                picRequired.Visible = true;
            this.BackColor = SystemColors.Info;
            LookUpbtn.BackColor = SystemColors.Control;

        }

        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            this.LookUpbtn.Visible = false;
            //picRequired.Visible = false;
            this.BackColor = SystemColors.Window;
            if (this.Required)
            {
                if (string.IsNullOrEmpty(this.Text))
                {
                    this.Focus();
                }
            }
        }

        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (this.DesignMode)
                LookUpbtn.Visible = false;

            if (!string.IsNullOrEmpty(DrillDown))
            {
                FontFamily family = new FontFamily("宋体");
                this.Font = new Font(family, this.Font.Size, FontStyle.Underline);
                this.ForeColor = Color.Blue;
            }

            //显示红色图片
            if (this.Required)
            {
                picRequired.Dock = DockStyle.Right;
                picRequired.Size = new System.Drawing.Size(12, this.Height - 10);
                picRequired.BackgroundImage = Properties.Resources.Required;
                picRequired.BackgroundImageLayout = ImageLayout.Stretch;
                picRequired.BackColor = Color.Transparent;
                this.Controls.Add(picRequired);
            }

            //显示按钮
            if (!string.IsNullOrEmpty(Lookup.SQL))
            {
                _isShowButton = true;
                LookUpbtn.Dock = DockStyle.Right;
                LookUpbtn.Text = "...";
                LookUpbtn.Size = new System.Drawing.Size(20, this.Height - 4);
                this.Controls.Add(LookUpbtn);
                LookUpbtn.Click += new EventHandler(btn_Click);
            }

        }

        #endregion

        #region LookUp
        int i = 0;
        void btn_Click(object sender, EventArgs e)
        {
            //这里实在是不明白为什么会执行三次
            if (i % 3 == 0)
                btn_LookUp(sender, new CancelLookupEventArgs());
            ++i;
        }

        public void btn_LookUp(object sender, CancelLookupEventArgs e)
        {
            #region BeforeLookup
            if (BeforeLookup != null)
            {
                BeforeLookup(sender, e);
                if (e.Cancel == true)//beforelookup前进行取消
                {
                    this.SelectAll();
                    this.SelectionStart = this.Text.Length;
                    this.Focus();
                    return;
                }
            }
            #endregion

            #region Lookup
            if (!string.IsNullOrEmpty(Lookup.SQL))
            {
                try
                {
                    DataTable table = new DataTable();

                    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                    cmd.CommandText = Lookup.SQL;
                    System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(cmd.CommandText, HVS.Common.Shared.sqlConnection);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    DataTable newtable = new System.Data.DataTable();
                    Columus.Clear();
                    foreach (DataColumn co in ds.Tables[0].Columns)
                    {
                        DataColumn col = new DataColumn(co.ColumnName, co.DataType);
                        col.ReadOnly = true;
                        newtable.Columns.Add(col);
                        Columus.Add(co.ColumnName, co.ColumnName);
                        if (!_isRetureLookUpColumn && !string.IsNullOrEmpty(Lookup.ReturnColumn))
                        {
                            if (co.ColumnName == Lookup.ReturnColumn)
                            {
                                _isRetureLookUpColumn = true;//如果指定的列合法
                            }
                        }

                    }
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        object[] value = new object[row.Table.Columns.Count];
                        for (int i = 0; i < row.Table.Columns.Count; i++)
                            value[i] = row[i];
                        newtable.Rows.Add(value);
                    }
                    LookupForm lookupForm = new LookupForm(newtable, Columus);
                    if (_isRetureLookUpColumn)
                        lookupForm.ReturnColumn = this.Lookup.ReturnColumn;//返回指定行的指定列
                    else
                        lookupForm.ReturnColumn = "";//返回当前行第一列

                    lookupForm.TopMost = true;
                    lookupForm.ShowDialog();
                    if (LookupForm.IsSelectRow)
                    {
                        this.Text = lookupForm.ReturnValue;
                        AfterLookupArgs.LookUpSelectRow = lookupForm.LookUpSelectRow;
                    }
                    this.SelectAll();
                    this.SelectionStart = this.Text.Length;
                    // this.Focus();
                }
                catch
                {
                    MessageBox.Show("失败");
                    this.Text = "";
                    this.Focus();
                    this.SelectionStart = this.Text.Length;
                    AfterLookupArgs.LookUpSelectRow = null;
                    return;
                }
            }
            #endregion

            #region  AfterLookup
            if (AfterLookup != null && LookupForm.IsSelectRow)
            {
                AfterLookup(sender, AfterLookupArgs);
                this.Focus();
                this.SelectionStart = this.Text.Length;
            }
            #endregion
        }
        #endregion

        #region drilldown
        protected override void OnDoubleClick(EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.DrillDown))
            {
                Form keysform = new Form();
                keysform.Show();
            }
            base.OnDoubleClick(e);
        }
        #endregion

        #region  释放资源

        public virtual void ReleaseResources()
        {

        }

        private void PerformReleaseResources()
        {
            ReleaseResources();
            Columus.Clear();
            Columus = null;
            _lookup = null;
            AfterLookupArgs = null;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                try
                {
                    PerformReleaseResources();
                }
                catch
                {

                }

                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #endregion
    }
}

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HVS.WinUI.Forms
{
    public partial class LookupForm : DockForm
    {
        public string ReturnValue = "";
        public string ReturnColumn;
        public static bool IsSelectRow = false;
        public DataGridViewRow LookUpSelectRow = new DataGridViewRow();
        public Dictionary<string, string> Columus = new Dictionary<string, string>();


        public LookupForm()
        {
            InitializeComponent();
        }

        public LookupForm(DataTable table, Dictionary<string, string> ColumusKeys)
        {
            InitializeComponent();
            Columus = ColumusKeys;
            this.grid.DataSource = table;
        }


        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            foreach (string column in Columus.Keys)
                this.cboColumn.Items.Add(column);
            if (this.cboColumn.Items.Count > 0)
                this.cboColumn.SelectedIndex = 0;
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            this.KeyPreview = false;
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            DataGridViewCellMouseEventArgs es = new DataGridViewCellMouseEventArgs(grid.CurrentCell.ColumnIndex, grid.CurrentCell.RowIndex, 0, 0, new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0));
            dataGridView1_CellMouseDoubleClick(null, es);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            IsSelectRow = true;
            if (string.IsNullOrEmpty(ReturnColumn))
                ReturnValue = this.grid.Rows[e.RowIndex].Cells[0].Value.ToString();
            else
                ReturnValue = this.grid.Rows[e.RowIndex].Cells[ReturnColumn].Value.ToString();
            LookUpSelectRow = grid.Rows[e.RowIndex];
            this.Close();
        }
     
        private void grid_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                DataGridViewCellMouseEventArgs es = new DataGridViewCellMouseEventArgs(grid.CurrentCell.ColumnIndex, grid.CurrentCell.RowIndex, 0, 0, new MouseEventArgs(MouseButtons.Left, 0, 0, 0, 0));
                dataGridView1_CellMouseDoubleClick(null, es);
            }
        }

         }
}

组件 | 阅读 2899 次
文章评论,共1条
vfdff
2008-04-11 19:13
1
没有看懂
什么语言
游客请输入验证码
浏览24975次