Capture Double-click Events for DataGrid

xiaoxiao2021-03-06  79

Namespace DataGridDoubleClick

{

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.data;

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.forms.DataGrid DataGrid1;

Private dataset mydataset;

DateTime GridMouseDowntime;

Private system.windows.Forms.Label label1;

Private system.componentmodel.Container Components = NULL;

Public Form1 ()

{

InitializationComponent ();

Gridmousedowntime = datetime.now;

Setup ();

}

Private void setup ()

{

// Create DataSet with 2 Table and 1 and Relation

MakedataSet ();

// data binding

DataGrid1.SetDataBinding (MyDataSet, "Customers");

// Add a style

AddCustomDataTyle ();

}

Private void makedataset ()

{

// Create a DataSet.

MyDataSet = New Dataset ("MyDataSet");

// Create 2 DataTables.

DataTable TCUST = New DataTable ("Customers");

// Create two columns and add to the first table

Datacolumn ccustid = New Datacolumn ("CustID");

Datacolumn ccustname = new Datacolumn ("CustName");

Datacolumn ccurrent = new Datacolumn ("CustCITY");

Tcust.column.add (ccustid);

Tcust.column.add (ccustname);

Tcust.column.add (ccurrent);

// Add Tables to DataSet.

MyDataSet.Tables.Add (TCUS);

/ * Calculate Tables. For each customer, create a DATAROW variable * /

DataRow newrow1;

// Add record to Customers Table.

For (int i = 1; i <4; i )

{

NEWROW1 = Tcust.newrow ();

NEWROW1 ["Custid"] = (100 * i) .tostring ();

TCUST.ROWS.ADD (NewRow1);

}

TCUST.ROWS [0] ["CustName"] = "The Wonderful World of Mengxian]

TCUST.ROWS [1] ["custname"] = "net_lover";

TCUST.ROWS [2] ["custname"] = "http://xml.sz.luohuedu.net/";

TCUST.ROWS [0] ["CustCITY"] = "Beijing"; tcust.rows [1] ["CustCity"] = "Shanghai";

TCUST.ROWS [2] ["CustCITY"] = "Henan";

}

Private void addcustomDataTylestyle ()

{

DataGridtablesTyle Ts1 = New DataGridtableStyle ();

Ts1.mappingname = "customers";

// Set the properties

Ts1.alternatingbackbackcolor = color.lightgray;

// Add the TextBox column style so that we can capture mouse events

DataGridTextBoxColumn TextCol = New DataGridTextBoxColumn ();

Textcol.mappingname = "custom";

Textcol.Headertext = "Sequence";

Textcol.width = 100;

// Add event processor

Textcol.textbox.mousedown = new mouseeventhandler (TextBoxMouseDownHandler);

TextCol.TextBox.doubleClick = New EventHandler (TextBoxDoubleClickHandler);

Ts1.gridColumnStyles.Add (Textcol);

Textcol = new DataGridTextBoxColumn ();

Textcol.mappingname = "custname";

Textcol.Headertext = "Name";

Textcol.width = 100;

// Add event processor

Textcol.textbox.mousedown = new mouseeventhandler (TextBoxMouseDownHandler);

TextCol.TextBox.doubleClick = New EventHandler (TextBoxDoubleClickHandler);

Ts1.gridColumnStyles.Add (Textcol);

Textcol = new DataGridTextBoxColumn ();

Textcol.mappingname = "custom";

Textcol.Headertext = "Address";

Textcol.width = 100;

// Add event processor

Textcol.textbox.mousedown = new mouseeventhandler (TextBoxMouseDownHandler);

TextCol.TextBox.doubleClick = New EventHandler (TextBoxDoubleClickHandler);

Ts1.gridColumnStyles.Add (Textcol);

DataGrid1.tables.add (TS1);

}

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer generated codeprivate void initializecomponent ()

{

This.DataGrid1 = new system.windows.forms.dataGrid ();

THIS.LABEL1 = New System.windows.Forms.label ();

(System.comPonentModel.isupportInitialize) (DataAgrid1)). Begininit ();

THIS.SUSPENDLAYOUT ();

//

// DataGrid1

//

This.DataGrid1.captionbackColor = system.drawing.systemcolors.Info;

This.DataGrid1.captionForeColor = system.drawing.systemcolors.windowText;

THIS.DATAGRID1.CAPTIONVISIBLE = FALSE;

THIS.DATAGRID1.DATAMEMBER = "";

This.DataGrid1.HeaderForeColor = system.drawing.systemcolors.ControlText;

this.DataGrid1.location = new system.drawing.point (11, 9);

This.DataGrid1.name = "datagrid1";

This.DataGrid1.size = new system.drawing.size (368, 144);

this.DataGrid1.tabindex = 0;

This.DataGrid1.mousedown = new system.windows.Forms.MouseEventHandler (this.DataGrid1_mousedown);

//

// label1

//

This.label1.Location = new system.drawing.point (4, 166);

THIS.LABEL1.NAME = "label1";

This.label1.size = new system.drawing.size (383, 23);

THIS.Label1.tabindex = 1;

THIS.Label1.TextAlign = system.drawing.contentAlignment.middleCenter;

This.Label1.Click = new system.eventhandler (this.form1_click);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (5, 13);

This.ClientSize = New System.drawing.size (387, 201);

this.controls.addrange (new system.windows.forms.control] {

THIS.LABEL1,

THIS.DATAGRID1});

THIS.NAME = "Form1";

This.Text = "Example of the Mouse Double-click Event;

(System.comPonentModel.isupportInitialize) (THISDATAGRID1)). Endinit ();

This.ResumeLayout (false);

}

#ndregion

[Stathread]

Static void main () {

Application.run (New Form1 ());

}

Private Void TextBoxDoubleClickHandler (Object Sender, Eventargs E)

{

Messagebox.show ("Double-click the event. The value is double-hit:" ((TextBox) .Text.toString ());

}

Private Void TextBoxMousedownHandler (Object Sender, MouseEventArgs E)

{

IF (DateTime.Now

{

Messagebox.show ("Double-click the event. The value is double-hit:" ((TextBox) .Text.toString ());

}

Label1.text = "Textbox mouse is pressed."

}

Private Void DataGrid1_mousedown (Object Sender, System.Windows E)

{

Gridmousedowntime = datetime.now;

Label1.text = "DataGrid1 mouse is pressed."

}

Private Void Form1_Click (Object Sender, System.EventArgs E)

{

Label1.text = "";

}

Private void Label1_Click (Object Sender, System.EventArgs E)

{

Label1.text = "";

}

}

}

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

New Post(0)