DOTNET desktop programming plan. Down-down box. DataGrid drop-down window

xiaoxiao2021-03-06  15

First, renderings

Implementation

n Code Transformation [Enter the encoded focus to leave the name].

n pulls out the selection window with DataGrid.

n background window does not lose focus

Second, development background

Seeing that the N multi-program has this kind of function, such as the drop-down data window of the PB, Delphi also has the same kind of Grid and DOTNET's Grid control does not have this feature, two words "depressed", cooperate with some of the experience in the previous article And knowledge is easy, you can make the function, in the document returns 9CBS, the digital code. I am in the heart. If you want to reprint the article, please qq: 65323574 Email: Flashelf@163.com

Third, the program realizes code analysis

Description: The SystemShell and NOACTForm class use in this article in the previous article "DOTNET desktop programming program. Down-down box. Similar Word color drop-down box" is not repeated here.

Address: http://blog.9cbs.net/flashelf/archive/2005/01/30/273954.aspx

3.1 DataGridvnTextColumnStyle Class

The most basic class of this article [has a code, name conversion function]

using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Data; using System.Diagnostics; namespace MagicLink.FlashElf.Windows.Controls.WinDataGrid.ColumnStyle {// /

/// DataGridColumnStyle ///////// input 1 display in [Code | Name]] Men /// Input 2 Display Female /// public class DataGridVnTextColumnStyle: DataGridTextBoxColumn {/ // /// (1) Control is like binding data object Submit data is useful DELEGATE /// public delegate void delegateCommit (Object ActData, String TextboxText, Ref Bool Abort); //// /// (2) occurs when submitting data to the control data objects bound /// public event DelegateCommit DataCommit; private string _DisplayMember; private string _ValueMember; private DataView _dv; private DataTable _dt; private bool Isedit = false; private bool iscommit = false; public DataGridVNTextColumnStyle (): base () {// map base class for obtaining the TextBox key KeyPress editing state base.TextBox.KeyPress = new KeyPressEventHandler (TextBox_KeyPress); base.Disposed = new EventHandler (DataGridVNTextColumnStyle_Disposed); // / / TODO: Add constructor logic //} /// /// base class TextBox keypress /// /// // / private void textbox_keypress (Object sender, keypressEventargs e) {//debug.writeLine ("

TEXTBOX_KEYPRESS "); iSEDIT = true;} ///

/// Displayed data column /// public string displaymember {get {return_displaymember;} set {_displaymember = value;}} //// /// Implement data /// public datatable DataSource {get {return_dt;} set {= value; _dv = new DataView (); _dv.table = _d;}}} /// /// Write data column /// public string valuemember {get {returnember; Set {_valuemember = value;}} /// /// (3) Rewrive base class commit /// /// ///// /// protected override bool commit (currenufactageManager DataSource, int Rownum) {// If not in editing status [here behaves that the user does not enter data in TextBox Time] if (! ISedit) Return Base.comMit (Datasource, Rownum); // If the last commit is not processed, IF (ISCommit) returnaf false;

try {isCommit = true; // get the current cell data GetColumnValueAtRow object ActData = GetColumnValueAtRow (dataSource, rowNum); // current data in the TextBox string TextBoxText = TextBox.Text; bool isAbort = false;! if (DataCommit = null {Try {// Trigger Event [This makes it easy to control the user's input when using this class] // Event can use exception to stop submitting the DataGrid and pop up the prompt datacommit (actdata, textboxtext, ref isabort);} catch (Exception EX) ) {Throw ex;}} f (isabort) {Abort (rownum); Return False; _dv.sort = _valuemember; int index = 0; try {if (textBox.text! = "&& textbox.text! = nulltext) {Object odate = textbox.text; // Judgment data in DataView There is no return -1 index = _dv.find (odate);} else {// is set to DBNULL BASETCOLUMNVALUEATROW (DataSource, Rownum, System.dbnull.Value);

}}}} Catch (formatexception fEX) {fEX.getHashCode (); // Input is not expected type index = -1;} catch (exception ex) {// Other exception through ex;} if (index <0) {/ / If you are not a expected data throw new applicationException ("Enter incorrect data!");}; Try {return base.commit (datasource, rownum);} catch (exception ex) {throw ex;}} finaryly {/ / Restore switching variable ISCommit = false; ISEDIT = false;}} ///

/// (4) Tettime Draw Image or Text to Grid cells /// Rewrive this function to achieve [Name] display /// /// /// /// ////// /// /// ///

_imageList.ImageStream "))); this._imageList.TransparentColor = System.Drawing.Color.Transparent;} public DataGridVNButtonColumnStyle (): base () {InitializeComponent (); base.Disposed = new EventHandler (DataGridVNButtonColumnStyle_Disposed); _button.Dock = DockStyle.Right; _button.Size = new Size (16,16); base.TextBox.Controls.Add (_button); _button.GotFocus = new EventHandler (_button_GotFocus); _button.Visible = true; // // TODO: in Add constructor logic ///} ///

/// If the mouse click to get the TEXTBOX to get focus /// /// /// private void _Button_gotfocus (Object sender, Eventargs e) {_Button.Parent.focus ();} public button @Button {Get { return _button;}} private void DataGridVNButtonColumnStyle_Disposed (object sender, EventArgs e) {if (_button = null!) {_button.Dispose ();} if (_imageList = null!) {_imageList.Dispose ();}}}} 3.2 DataGridVMShowGridColumnStyle class

Realize drop-down DataGrid

using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.Data; using System.Diagnostics; namespace MagicLink.FlashElf.Windows.Controls.WinDataGrid.ColumnStyle {// /

/// to [code | name] displayed with pull down list DataGridColumnStyle /// DataGrid /// public class DataGridVMShowGridColumnStyle: DataGridVNButtonColumnStyle {DataView _dv = new DataView (); /// /// pull-down on top of the window a DataGrid /// GridForm _gf = new GridForm (); private CurrencyManager _mc; private int _Rownum; public DataGridVMShowGridColumnStyle (): base () {// event map some base. Button.Click = new EventHandler (_button_Click); _gf.MouseDown = new MouseEventHandler (Gf_MouseDown); _gf.MouseMove = new MouseEventHandler (Gf_MouseMove); this.Disposed = new EventHandler (DataGridVMShowGridColumn Style_disposed;} // (1) Rewrive the base class Edit to get currencymanager, rownum /// /// Rewind base class edit /// /// /// /// / // ///

protected override void Edit (CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) {_mc = source; _Rownum = rowNum; base.Edit (source, rowNum, bounds, readOnly, instantText, cellIsVisible);} ///

/// mouse single button show out of the large DataGrid window /// /// /// private void _Button_Click (Object sender, Eventargs e) {f (_dv.table == null) {_dv.table = base.datasource; _gf.dataGrid1.datasource = _dv; _dv.sort = valuemember; IF (_GF.OWNER == NULL) {_gf.Owner = this._button.toplevelcontrol as form;} // call noactform's show _Gf.show (textbox); // capture mouse _gf.capture = True; // Get the position of the current row in the DataView intateX = _dv.find (getColumnValueatrow (_mc, _rownum)); if (selectindex == - 1) SelectIndex = 0; // Not only how to clear the multi-selection of DataGrid FOR (int i = 0; i <_gf.datagrid1.visibleRowcount; i ) {_gf.dataGrid1.unselect (i);} // select the code in the DataGrid in the TextBox corresponding to the datagrid in the DataGrid _Gf.DataGrid1.select ( SelectIndex; _gf.bindingcontext [_dv] .position = selectindex;} ///

/// The mousemove of the drop-down window captures the mouse so you can get /// /// /// private void Gf_MouseMove (object sender, MouseEventArgs e) {DataGrid Grid = _gf.dataGrid1; System.Windows.Forms.DataGrid.HitTestInfo hti; // HitTestInfo achieved by the object coordinate hti = Grid.HitTest (eX, eY); // if The position of the mouse is on the cell or row head IF (hti.type == system.windows.forms.dataGrid.hittesttype.cell || hti.type == system.windows.Forms.DataGrid.hittesttype.rowheader) {int Index = hti.row; if (index == - 1) index = 0; // Not only to how to clear the multi-selection of DataGrid this written for this for (int i = 0; i /// drop-down window mouse Press /// /// /// private void gf_mousedown (object sender, mouseeventargs e) {_gf.capture = false; point pt = control.MousePosition; // If it is in the drop-down window window range Internal IF (! (Pt.x> _gf.right || pt.x <_gf.left || pt.y> _gf.bottom || pt.y <

_GF.top) {system.windows.Forms.DataGrid.hittestinfo hti; hti = _gf.dataGrid1.hittest (ex, EY); // Under the row of line int index = hti.row; // If there is no Way to handle if (index == - 1) index = 0; object odate = _dv [index] [base.valuemember]; Object Olddate = base.getColumnValueatrow (_MC, _ROWNUM); // If the drop-down window Select the data line selected by the drop-down window And the original data different if (! Olddate.equals (odate)) {// Sets the current cell data setColumnValueatrow (_MC, _ROWNUM, ODATE); columnStartedEditing (base.text = Odate.toString ();}} // Hide drop-down window _gf.hide ();} ///

/// cleaning battlefield /// /// /// private void DataGridvmshowgridcolumnStyle_disposed (Object sender, Eventargs e) {ix (_dv! = Null) {_dv.dispose ();} if (_gf! = null) {_gf.dispose ();}}}} 3.2 How to use

And the general DataGridColumnStyle check

private void Form1_Load (object sender, System.EventArgs e) {// prepare the DataGrid _dv.Table = GetMainTable (); // prepare drop down window data DataTable SexDt = GetShowGridTable (); DataGridTableStyle gts; gts = new DataGridTableStyle () ; DataGridVMShowGridColumnStyle Glc = new DataGridVMShowGridColumnStyle (); gts.MappingName = _dv.Table.TableName; Glc.HeaderText = "sex"; Glc.MappingName = "sex"; Glc.DataSource = SexDt; Glc.DisplayMember = "Txt"; Glc .ValueMember = "Id"; gts.GridColumnStyles.Add (Glc); dataGrid1.TableStyles.Add (gts); // add a general column DataGridColumnStyle dgcolStyle; dgcolStyle = new DataGridTextBoxColumn (); dgcolStyle.MappingName = "id"; DGCOLStyle.Headertext = "Number"; G ts.GridColumnStyles.Add (dgcolStyle); Glc.DataCommit = new DataGridVNTextColumnStyle.DelegateCommit (Glc_DataCommit); dataGrid1.DataSource = _dv;} private void Glc_DataCommit (object ActData, string TextBoxText, ref bool Abort) {// If that data does not meet Requires Abort = True can be console.writeline ("1 ::: {0}, {1}, {2}", actdata, textboxtext, abort); "); Dt.columns.add (" id ", typeof (int)); dt.columns.add ("

Birthday ", TypeOf (DateTime)); DT.Columns.Add (" SEX ", TypeOf (int)); dt.Rows.add (new object [] {1, datetime.now, 1}); return dt;} Private DataTable getShowgridTable () {dataable sexdt = new data ("sextable"); sexdt.columns.add ("id", typeof (int)); sexdt.columns.add ("txt", typeof (string); sexdt .Rows.add (new object [] {1, "male"}); sexdt.rows.add (new object [] {2, "female"}); return sexdt;}

转载请注明原文地址:https://www.9cbs.com/read-61520.html

New Post(0)