c# .net winfrom sunnyui的RichTextBox TextBox的KeyPress和KeyDown对Enter按钮无效处理
效果图:
在此之前感谢博主,winform无法屏蔽回车事件、KeyPress事件不执行问题解决-CSDN博客
根据该大佬的方式,需处理添加回车事件处理,便继承了RichTextBox重写该方法,且执行了执行事件
public sealed class MyUIRichTextBox : UIRichTextBox { public bool LastRow=false; public event KeyEventHandler MyEnterEvent; protected override bool ProcessDialogKey(Keys keyData) { //处理回车键 if (keyData == Keys.Enter && LastRow) { MyEnterEvent?.Invoke(this,new KeyEventArgs(keyData)); return true; } return base.ProcessDialogKey(keyData); } }
下面是实现的结果,实现自动对每行超过文本框的内容展示RichBox悬浮展示,且添加允许修改的列和回车保存
public class UIDataGridView : DataGridView, IStyleInterface { MyUIRichTextBox richText = new MyUIRichTextBox(); Rectangle _Rectangle; public UIDataGridView() { richText.MyEnterEvent += RichText_KeyDown;//这个事件是因为表格的最后一行得手动处理提交到Cell的Value richText.Parent = this; richText.Visible = false; richText.TabStop = false; richText.Font = this.Font; this.CellEnter += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv_CellClick); this.CellLeave += new DataGridViewCellEventHandler(this.dgv_CellLeave); this.CellValidating += new System.Windows.Forms.DataGridViewCellValidatingEventHandler(this.dgv_CellValidating); } private void RichText_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter && richText.LastRow) { dgv_CellLeave(null, null); } } List<string> ColumnsNameNotRich = null; /// <summary> /// 无论任何情况该列都不显示RichTextBox /// </summary> /// <param name="columns">列名</param> public void SetNotRich(string[] columns) { ColumnsNameNotRich = columns.ToList(); } /// <summary> /// 可修改的多行文本框richText,richText默认是只读 /// </summary> List<string> ColumnsNameMustRich = null; public void MustRich(string[] columns) { ColumnsNameMustRich = columns.ToList(); } private void dgv_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; } DataGridView _dgv = this; if (_dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.ReadOnly) { return; } Type _type = _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].ValueType; if (_type == typeof(string)) { return; } object o = _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue; if (o == null || string.IsNullOrEmpty(o.ToString())) { return; } bool isError = false; DataGridViewColumn column = _dgv.Columns[e.ColumnIndex]; if (_type == typeof(double) || _type == typeof(decimal) || _type == typeof(float)) { decimal testD = 0; if (decimal.TryParse(o.ToString(), out testD) == false) { isError = true; } if (isError) { if (column is DataGridViewTextBoxColumn) { Sunny.UI.Common.ShowErrorTip("请输入正确的数值" + column.HeaderText); e.Cancel = true; } } } else if (_type == typeof(int)) { int testD2 = 0; if (int.TryParse(o.ToString(), out testD2) == false) { isError = true; } if (isError) { if (column is DataGridViewTextBoxColumn) { Sunny.UI.Common.ShowErrorTip("请输入正确的数值" + column.HeaderText + ",输入整数!"); e.Cancel = true; } } } } DgvFilterManager filterManager = null; private void dgv_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e) { if (filterManager == null) { filterManager = new DgvFilterManager(this); //开启万能查询 } //if (this.InvokeRequired) //{ // this.Invoke(new Action(() => // { // richText.Font = this.Font; // })); //} //else //{ // richText.Font = this.Font; //} //new GUTAERP.UserControls.DataGridViewColumnSelector(this); } private void dgv_CellLeave(object sender, EventArgs e) { if (!richText.ReadOnly && richText.Tag != null) { DataGridViewCell cell = (DataGridViewCell)richText.Tag; cell.Value = richText.Text; } if (richText.Visible) { richClear(); } } private void richClear() { richText.LastRow = false; richText.Visible = false; richText.Text = ""; richText.Tag = null; } private void dgv_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0 || e.ColumnIndex < 0) { return; } DataGridView _dgv = this; if (_dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null) { return; } if (richText.Visible) { richClear(); return; } if (ColumnsNameNotRich != null) { int i = ColumnsNameNotRich.Count(a => a == _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name); if (i > 0) { return; } } bool mustShow = false; if (ColumnsNameMustRich != null) { int i = ColumnsNameMustRich.Count(a => a == _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Name); if (i > 0) { mustShow = true; } } if (!_dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.ReadOnly && !mustShow) { return; } string str = _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); int _width = TextRenderer.MeasureText(str, this.Font).Width; _width = str.IndexOf("\n") != -1 ? Screen.PrimaryScreen.WorkingArea.Width : _width; if (_width > _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].OwningColumn.Width || mustShow) { _Rectangle = _dgv.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true); //得到所在单元格位置和大小 richText.Text = _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(); richText.Size = new Size(_dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Size.Width, 200); //把单元格大小赋给控件 AutoSizeControl(richText); richText.Location = new Point(_Rectangle.X, _Rectangle.Y + _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Size.Height); //把单元格位置赋给控件 richText.Visible = true; //可以显示控件了 richText.BringToFront(); //richText.Focus(); if (!mustShow) { richText.ReadOnly = true; } else { richText.ReadOnly = false; if (_dgv.RowCount - 1 == e.RowIndex) { richText.LastRow = true; } richText.Focus(); //光标定位最后 richText.Select(richText.TextLength, 0); //滚动到控件光标处 richText.ScrollToCaret(); } richText.Tag = _dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; } else { richClear(); } } private void AutoSizeControl(Control co) { Graphics g = co.CreateGraphics(); SizeF sif = new SizeF(co.Size.Width, 650);//最大高度 Size preSize = g.MeasureString(co.Text, co.Font, sif).ToSize(); if (preSize.Width < co.Width) { preSize.Width = co.Width; } if (preSize.Height < co.Height) { preSize.Height = co.Height; } co.Size = preSize; g.Dispose(); } }
作者:兮去博客
出处:c# .net winfrom sunnyui的RichTextBox TextBox的KeyPress和KeyDown对Enter按钮无效处理 - 兮去 - 博客园 (cnblogs.com)
版权:本文版权归作者和博客园共有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任