Winform 元件

 1 using System;
 2 using System.ComponentModel;
 3 using System.Collections.Generic;
 4 using System.Diagnostics;
 5 
 6 using System.Windows.Forms;
 7 using System.Drawing;
 8 
 9 namespace UserControl
10 {
11     public partial class TextBoxAlert :TextBox
12     {
13         //定义参数
14         private bool bAlert = false;
15         private ShowDataGridView showDataGridView;
16         private Form parentForm;
17         private Point pLocation;
18 
19         private int _dgvX, _dgvY;
20 
21 
22         /// <summary>
23         /// 是否弹出对话框
24         /// </summary>
25         [DefaultValue(true),Description("是否弹出对话框")]
26         public bool BAlert
27         {
28             get { return bAlert; }
29             set { bAlert = value; }
30         }
31 
32 
33 
34 
35 
36         public TextBoxAlert()
37         {
38             this.MaxLength = 30;
39 
40             this.GotFocus+=new EventHandler(TextBoxAlert_GotFocus);
41             this.KeyPress += new KeyPressEventHandler(TextBoxAlert_KeyPress);
42         }
43 
44         private void TextBoxAlert_KeyPress(object sender, KeyPressEventArgs e)
45         {
46             SendKeys.Send("{TAB}");
47             showDataGridView.Focus();
48             if (showDataGridView.SelectedRows.Count == 0)
49             {
50                 showDataGridView.Rows[0].Selected = true;
51             }
52         }
53 
54         /// <summary>
55         /// 获得焦点
56         /// </summary>
57         /// <param name="sender"></param>
58         /// <param name="e"></param>
59         private void TextBoxAlert_GotFocus(object sender, EventArgs e)
60         {
61             
62                 if (showDataGridView == null)
63                 {
64                     showDataGridView = new ShowDataGridView();
65                     this.FindForm().Controls.Add(showDataGridView);
66                 }
67 
68                 if (showDataGridView.Visible == false)
69                 {
70                     showDataGridView.Visible = true;
71                 }
72 
73                 int x, y;
74 
75                 x = this.Location.X;
76                 y = this.Location.Y + this.Height + 1;
77 
78                 //若還沒設置DataGridView 出現的位置
79                 pLocation = new Point(x, y);
80                 showDataGridView.BringToFront();
81                 //設置兩者的連接
82                 showDataGridView.TextEmpID = this.Name;
83                 showDataGridView.Location = pLocation;
84 
85                 //设置DataGridView获得焦点
86                 showDataGridView.Focus();
87                 if(showDataGridView.SelectedRows.Count==0)
88                     showDataGridView.Rows[0].Selected = true;
89         }
90 
91     }
92 }
93 
 
 
代码
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using System.Windows.Forms;
using System.Data;

namespace UserControl
{
    
public partial class ShowDataGridView :DataGridView
    {
        
#region  Fields
        
private TextBoxAlert TextBoxAlert;  //文本框
        private Form parentForm;
        
private string sTextBoxAlert;
        
#endregion

        [Description(
"設定收銀員文字對話框的控制項的Name屬性"),Browsable(true)]
        
public string TextEmpID
        {
            
get { return this.sTextBoxAlert; }
            
set {
                
if (string.IsNullOrEmpty(value) == false)
                {
                    
this.sTextBoxAlert = value;
                    SetTextBoxReference();
                }
            }
        }


        
private void SetTextBoxReference()
        {
            
if (parentForm == null)
            {
                parentForm 
= this.FindForm();
            }

            
this.FindControl(parentForm, this.sTextBoxAlert);
            TextBoxAlert.TextChanged 
+=new EventHandler(TextBoxAlert_TextChanged);

            
if (this.SelectedRows.Count == 1)
            {
                
this.SelectedRows[0].Selected = false;
            }
        }

        
/// <summary>
        
/// 找出此按鈕對應的文字方框控制項
        
/// </summary>
        
/// <param name="parentControl">上層控制項</param>
        
/// <param name="controlName">要找到的目標控制項Name屬性</param>
        private void FindControl(Control parentControl, string controlName)
        {
            
if (parentControl == null)
            {
                
return;
            }

            
//找到表單所屬的文字方塊
            foreach (Control c in parentControl.Controls)
            {
                
if (c.Name == controlName)
                {
                    
this.TextBoxAlert = c as TextBoxAlert;
                    
break;
                }

                
//判讀是否有子控制項,如有則循環呼叫
                if (c.HasChildren)
                    FindControl(c,controlName);
            }
        }

        
private void TextBoxAlert_TextChanged(object sender,EventArgs e)
        { 
            
        }

        
public ShowDataGridView()
        {
            SetProperties();
            AddColumns();
            BindData();

            
//添加雙擊事件
            this.CellDoubleClick += new DataGridViewCellEventHandler(ShowDataGridView_CellDoubleClick);

            
//添加事件
            this.CellClick += new DataGridViewCellEventHandler(ShowDataGridView_CellClick);

            
//添加失去焦點後,隱藏控制項目
            this.LostFocus += new EventHandler(ShowDataGridView_LostFocus);
        }

        
/// <summary>
        
/// 單據數據
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void ShowDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        
private void ShowDataGridView_LostFocus(object sender, EventArgs e)
        {
            
this.Visible = false;
            
//MessageBox.Show("已經失去焦點");
        }

        
/// <summary>
        
/// 添加雙擊事件
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void ShowDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
//判斷有沒有選取資料
            if (e.RowIndex > -1 && this.SelectedRows.Count > 0)
            {
                GetBackMsg();
            }
            
else
            {
                MessageBox.Show(
"沒有選取得資料");
            }
        }


        
/// <summary>
        
/// 綁定數據
        
/// </summary>
        private void BindData()
        {
            DataTable dtEmpName 
= new DataTable();
            DataColumn cEmpName 
= new DataColumn("empName",typeof(string));
            dtEmpName.Columns.Add(cEmpName);
            DataRow dr 
= dtEmpName.NewRow();
            dr[
"empName"= "小明";
            dtEmpName.Rows.Add(dr);

            DataRow dr2 
= dtEmpName.NewRow();
            dr2[
"empName"= "文強";
            dtEmpName.Rows.Add(dr2);

            DataView dvEmpName 
= dtEmpName.DefaultView;
            
this.DataSource = dvEmpName;
        }

        
/// <summary>
        
/// 添加基本欄位
        
/// </summary>
        private void AddColumns()
        {
            DataGridViewTextBoxColumn txtEmpName 
= new DataGridViewTextBoxColumn();
            txtEmpName.HeaderText 
= "姓名";
            txtEmpName.DataPropertyName 
= "empName";
            txtEmpName.Width 
= 115;

            
this.Columns.AddRange(txtEmpName);
        }

        
/// <summary>
        
/// 設置自身屬性
        
/// </summary>
        private void SetProperties()
        {
            
this.Name = Guid.NewGuid().ToString();
            
this.ReadOnly = true;
            
this.AutoGenerateColumns = false;
            
this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            
this.AllowUserToResizeRows = false;
            
this.AllowUserToResizeColumns = false;
            
this.AllowUserToDeleteRows = false;
            
this.AllowUserToAddRows = false;
            
this.Width = 230;
            
this.Height = 160;
            
            
this.MultiSelect = true;
            
            
this.KeyDown += new KeyEventHandler(ShowDataGridView_KeyDown);
        }

        
/// <summary>
        
/// 
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void ShowDataGridView_KeyDown(object sender, KeyEventArgs e)
        {
            
if (e.KeyData == Keys.Enter)
            { 
                
//判斷是否已經選擇了數據
                if (this.SelectedRows.Count > 0)
                {
                    GetBackMsg();
                }
            }
            
else if (e.KeyData == Keys.Escape)
            {
                
if (TextBoxAlert != null)
                { 
                    
//若有選擇的資料改為不選取狀態
                    if (this.SelectedRows.Count > 0)
                    {
                        
this.SelectedRows[0].Selected = false;
                    }

                    TextBoxAlert.Focus();
                    TextBoxAlert.SelectionStart 
= TextBoxAlert.Text.Length;
                }
            }
        }

        
/// <summary>
        
/// 帶回選擇的信息
        
/// </summary>
        private void GetBackMsg()
        {
            TextBoxAlert.Text 
= this.SelectedRows[0].Cells[0].Value.ToString();
            TextBoxAlert.Focus();
            TextBoxAlert.SelectionStart 
= TextBoxAlert.Text.Length;
            
this.Visible = false;
        }
    }
}

 

 
 
 
 
 
 

 

posted on 2010-03-04 14:30  秋迪  阅读(290)  评论(0编辑  收藏  举报

导航