📄 commonqueryform.cs
字号:
#region Methods
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
switch( m.Msg )
{
case WM_HOTKEY:
btnExecute_Click( btnExecute, new System.EventArgs() );
break;
}
base.WndProc (ref m);
}
private void QueryForm_Load(object sender, System.EventArgs e)
{
this.btnAdd.Click += new EventHandler(this.SetButtonProperty);
this.btnOr.Click += new EventHandler(this.SetButtonProperty);
this.btnClear.Click += new EventHandler(this.SetButtonProperty);
foreach(Item i in query.Items)
cmbCondition.Items.Add( i.Caption );
ltbCondition.Items.Clear();
pnlAll.BorderStyle = query.BorderStyle;
SetButtonStyle();
SetCommandConnection();
NativeWin32.RegisterHotKey( Handle, 100, NativeWin32.KeyModifiers.Control, Keys.Enter );
}
private void cmbCondition_SelectedValueChanged(object sender, System.EventArgs e)
{
SetCurrentItem( cmbCondition.Text );
if( -1 != currItemIndex )
{
if( query.Items[currItemIndex].IsLookup ) // Lookup
{
cmbValue.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
LoadValue();
}
else // Normal
{
cmbValue.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
cmbValue.Items.Clear();
}
cmbValue.Text = "";
}
}
private void SetButtonProperty(object sender, System.EventArgs e)
{
if( 0<ltbCondition.Items.Count )
{
btnAdd.Text = "并且(&A)";
btnOr.Enabled = true;
btnClear.Enabled = true;
}
else
{
btnAdd.Text = "新增(&A)";
btnOr.Enabled = false;
btnClear.Enabled = false;
}
cmbValue.Text = "";
isButton = true;
pnlAll.SelectNextControl( ltbCondition, true, true, false, true );
}
private void btnAdd_Click(object sender, System.EventArgs e)
{
string line = GetCurrentLine();
if( null == line )
return;
string header = " ";
if( 0<ltbCondition.Items.Count )
header = "AND ";
ltbCondition.Items.Add( header + line );
}
private void btnOr_Click(object sender, System.EventArgs e)
{
string line = GetCurrentLine();
if( null == line )
return;
string header = "OR ";
ltbCondition.Items.Add( header + line );
}
private void btnClear_Click(object sender, System.EventArgs e)
{
ltbCondition.Items.Clear();
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
ltbCondition.Items.Clear();
this.Hide();
}
private void btnExecute_Click(object sender, System.EventArgs e)
{
if( 0>= ltbCondition.Items.Count )
return;
this.Hide();
}
private void cmbCondition_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if( isButton )
{
isButton = !isButton;
return;
}
if( System.Windows.Forms.Keys.Enter == e.KeyData && ((ComboBox)sender).Text != "" )
pnlAll.SelectNextControl( cmbCondition, true, true, false, true );
}
private void cmbOperator_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if( System.Windows.Forms.Keys.Enter == e.KeyData && ((ComboBox)sender).Text != "" )
pnlAll.SelectNextControl( cmbOperator, true, true, false, true );
}
private void cmbValue_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if( System.Windows.Forms.Keys.Enter == e.KeyData && ((ComboBox)sender).Text != "" )
pnlButton.SelectNextControl( pnlButton, true, true, true, true );
}
#region Lookup
private string SQLString()
{
Item it = query.Items[currItemIndex];
return "SELECT DISTINCT " + it.Field + " FROM " + it.Database + ".dbo." + it.TableName+" (NOLOCK)";
}
private void LoadValue()
{
try
{
command.CommandText = SQLString();
using(System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader(System.Data.CommandBehavior.Default))
{
while( reader.Read() )
cmbValue.Items.Add( reader.GetString(0) );
}
}
catch(Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
#endregion
private void SetCommandConnection()
{
if( query.SqlConnectionString != null && query.SqlConnectionString != "" )
{
System.Data.SqlClient.SqlConnection sqlcnnt = new System.Data.SqlClient.SqlConnection(query.SqlConnectionString);
command.Connection = sqlcnnt;
}
else
command.Connection = query.SqlConnection;
if( System.Data.ConnectionState.Closed == command.Connection.State )
command.Connection.Open();
}
private void SetButtonStyle()
{
btnAdd.FlatStyle = query.ButtonStyle;
btnOr.FlatStyle = query.ButtonStyle;
btnClear.FlatStyle = query.ButtonStyle;
btnCancel.FlatStyle = query.ButtonStyle;
btnExecute.FlatStyle = query.ButtonStyle;
}
private void SetCurrentItem(string caption)
{
if( "" == caption )
currItemIndex = -1;
currItemIndex = cmbCondition.SelectedIndex;
}
private string GetCurrentLine()
{
if( "" == cmbCondition.Text || "" == cmbOperator.Text || "" == cmbValue.Text )
return null;
return query.Items[currItemIndex].Alias + " " + cmbOperator.Text + " '" + cmbValue.Text + "' ";
}
public string Condition()
{
string result="";
for(int i = 0; i<ltbCondition.Items.Count; i++)
result +=ltbCondition.Items[i].ToString();
return result;
}
#endregion
private void QueryForm_Closed(object sender, System.EventArgs e)
{
ltbCondition.Items.Clear();
NativeWin32.UnregisterHotKey( Handle, 100 );
}
}
#endregion
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -