📄 queryfrm.cs
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Data;
using System.Threading;
using System.Drawing;
using QueryToolHelper;
using System.Text;
using System.Windows.Forms;
namespace QueryTool_V1
{
enum State
{
Ready,
Working,
Completed
}
public partial class QueryFrm : Form
{
// Member Variables
public TimeSpan laspedTime;
public bool cellIsUpdated;
public bool calenderFlag;
public DataGVCellCollection cell_list;
public TabPage currentSelectedTabPage;
ArrayList _columnList;
ArrayList _columnListSelected;
ArrayList _columnListNotSelected;
public DataTable resultTable;
CDataColumns objDataColumns;
public COperator _operators;
// Result Window
Label lblEmployeeID;
TextBox txtEmployeeID;
Label lblEmployeeName;
TextBox txtEmployeeName;
class ShowProgressArgs : EventArgs
{
public string _statusText;
public State _state;
public ShowProgressArgs(string statusText, State state)
{
this._statusText = statusText;
this._state = state;
}
}
class TimeArgs : EventArgs
{
public string _hours;
public string _minutes;
public string _seconds;
public State _state;
public TimeArgs(string hours, string minutes, string seconds, State state)
{
this._hours = hours;
this._minutes = minutes;
this._seconds = seconds;
this._state = state;
}
}
public delegate void ShowProgressHandler(object sender, ShowProgressArgs e);
public delegate void UpdateTimeHandler(object sender, TimeArgs e);
public QueryFrm()
{
InitializeComponent();
runQueryToolStripMenuItem.Visible = false;
saveToolStripMenuItem.Visible = false;
deleteClauseToolStripMenuItem.Visible = false;
cellIsUpdated = true;
calenderFlag = false;
_columnList = new ArrayList();
_columnListSelected = new ArrayList();
_columnListNotSelected = new ArrayList();
_operators = new COperator();
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedTab.Text.Contains("Query"))
{
// Query Window
runQueryToolStripMenuItem.Visible = false;
saveToolStripMenuItem.Visible = false;
openQueryToolStripMenuItem.Visible = true;
}
else
{
// Result Window
runQueryToolStripMenuItem.Visible = true;
saveToolStripMenuItem.Visible = true;
openQueryToolStripMenuItem.Visible = false;
}
}
void ShowProgress(object sender, ShowProgressArgs e)
{
// Make sure we're on the right thread
if (this.InvokeRequired == false)
{
if (e._state == State.Working)
{
// Make the Progress Bar Visible
progressBarStatus.Visible = true;
lblStatus.Text = e._statusText;
progressBarStatus.Style = ProgressBarStyle.Marquee;
StartTimer();
}
if (e._state == State.Completed)
{
progressBarStatus.Style = ProgressBarStyle.Blocks;
progressBarStatus.Value = 0;
progressBarStatus.Visible = false;
lblStatus.Text = e._statusText;
EndTimer();
}
}
// Transfer control to correct thread
else
{
ShowProgressHandler
showProgress =
new ShowProgressHandler(ShowProgress);
Invoke(showProgress, new object[] { sender, e });
}
}
public delegate void InitializeDatabaseDelegate();
public delegate void InitializeDataGridViewDelegate();
private void InitializeDatabase()
{
object sender = System.Threading.Thread.CurrentThread;
ShowProgressArgs e = new ShowProgressArgs("Retrieving...", State.Working);
ShowProgress(sender, e);
Thread.Sleep(new TimeSpan(0,0,1));
// Connect to Database and populate the Data Columns
objDataColumns = new CDataColumns();
e = new ShowProgressArgs("Ready", State.Completed);
ShowProgress(sender, e);
}
private void InitializeDatabaseCompleted(IAsyncResult result)
{
// In ASyncCallBack, we are not in the same thread as the that of the Form UI Thread
InitializeDatabaseDelegate initializeDB = (InitializeDatabaseDelegate)result.AsyncState;
initializeDB.EndInvoke(result);
InitializeDataGridView();
}
private void InitializeDataGridView()
{
if (!this.InvokeRequired)// Make sure we're on the right thread
{
// Populate the DataGridView
runQueryToolStripMenuItem.Visible = true;
// Change to edit mode when user enters a cell.
dataGridViewQuery.EditMode = DataGridViewEditMode.EditOnEnter;
// ------------------------------------------------
// First create the columns
dataGridViewQuery.Columns.Add("AndOr", "And/Or");
dataGridViewQuery.Columns.Add("Field", "Field");
dataGridViewQuery.Columns.Add("Operator", "Operator");
dataGridViewQuery.Columns.Add("Value", "Value");
// ------------------------------------------------
// Create a row and add a textbox cell
DataGridViewRow dataGridRow = new DataGridViewRow();
DataGridViewCell[] cells = new DataGridViewCell[4];
DataGridViewTextBoxCell txt00 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell txt01 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell txt02 = new DataGridViewTextBoxCell();
DataGridViewTextBoxCell txt03 = new DataGridViewTextBoxCell();
txt00.Value = string.Empty;
txt01.Value = "DataTable";
txt02.Value = "Equals";
txt03.Value = "EmployeeInfo";
dataGridRow.Cells.Add(txt00);
txt00.ReadOnly = true;
dataGridRow.Cells.Add(txt01);
txt01.ReadOnly = true;
dataGridRow.Cells.Add(txt02);
txt02.ReadOnly = true;
dataGridRow.Cells.Add(txt03);
txt03.ReadOnly = true;
dataGridViewQuery.Rows.Add(dataGridRow);
dataGridViewQuery[3, 0].DataGridView.Width = 20;
dataGridViewQuery.ReadOnly = true;
this.dataGridViewQuery.CellClick += new DataGridViewCellEventHandler(dataGridViewQuery_CellClick);
this.dataGridViewQuery.CellValueChanged += new DataGridViewCellEventHandler(dataGridViewQuery_CellValueChanged);
}
else // Transfer control to correct thread ***
{
InitializeDataGridViewDelegate initializeGV = new InitializeDataGridViewDelegate(InitializeDataGridView);
Invoke(initializeGV, new object[] { });
}
}
private void openQueryToolStripMenuItem_Click(object sender, EventArgs e)
{
// Clear the DataGridView First
dataGridViewQuery.Rows.Clear();
dataGridViewQuery.Columns.Clear();
deleteClauseToolStripMenuItem.Visible = true;
// Create Object of DataGridViewCell Collection
cell_list = new DataGVCellCollection();
// Initialize Database
// Asynch delegate method
InitializeDatabaseDelegate initializeDB = new InitializeDatabaseDelegate(InitializeDatabase);
initializeDB.BeginInvoke(new AsyncCallback(InitializeDatabaseCompleted), initializeDB);
}
// This Code Module has bugs
// 1. Not to allow user to select any node, it should be in sequence
// 2. Did not validate values entered by the user
private void dataGridViewQuery_CellClick(object sender, DataGridViewCellEventArgs e)
{
// Data
DataGVCell dataCell = null;
// check if the previous any cell is not updated
bool previousAnyCellnotUpdated = false;
// check if cell is already added to list
bool cellAdded = false;
if (cell_list != null)
{
foreach (DataGVCell dataGVCell in cell_list)
{
if (dataGVCell._rowIndex == e.RowIndex && dataGVCell._columnIndex == e.ColumnIndex)
{
cellAdded = true;
break;
}
}
if (!cellAdded)
{
dataCell = new DataGVCell(e.RowIndex, e.ColumnIndex, false, string.Empty, false);
cell_list.Add(dataCell);
cellAdded = true;
}
}
foreach (DataGVCell dataGVCell in cell_list)
{
if (dataGVCell._cellValue.Equals(""))
{
if (dataGVCell._rowIndex != e.RowIndex || dataGVCell._columnIndex != e.ColumnIndex) //Current Cell
{
previousAnyCellnotUpdated = true;
break;
}
}
}
if (!previousAnyCellnotUpdated) // To make sure the previous cell is updated
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -