using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Collections; namespace ControlLib.inh.autosearcher_Res { /// /// 搜索控件属性编辑窗体 /// public partial class FrmPropertySet : Form { SearcherPropertyCollector FSearcherPropertyCollector; public FrmPropertySet(SearcherPropertyCollector _SearcherPropertyCollector) { InitializeComponent(); try { CboSearchMatchWay.SelectedIndex = 0; FSearcherPropertyCollector = _SearcherPropertyCollector; if (FSearcherPropertyCollector.QuerySQL != string.Empty) { txtSql.Text = FSearcherPropertyCollector.QuerySQL; SetSQL(); CboKeyField.SelectedIndex = CboKeyField.Items.IndexOf(FSearcherPropertyCollector.KeyField); CboQueryField.SelectedIndex = CboQueryField.Items.IndexOf(FSearcherPropertyCollector.QueryField); CboReturnField.SelectedIndex = CboReturnField.Items.IndexOf(FSearcherPropertyCollector.ReturnValueField); txtReturnMaxCount.Text = FSearcherPropertyCollector.ReturnMaxCount.ToString(); ChkConvert.Checked = FSearcherPropertyCollector.ConvertToChineseSpell; CboSearchMatchWay.SelectedIndex = (int)FSearcherPropertyCollector.SearchMatchWay; txtFormWidth.Text = FSearcherPropertyCollector.ShowFormWidth.ToString(); txtFormHeight.Text = FSearcherPropertyCollector.ShowFormHeight.ToString(); if (FSearcherPropertyCollector.ShowFormWidth == 0 && FSearcherPropertyCollector.ShowFormHeight == 0) { ChkAuto.Checked = true; } for (int i = 0; i < FSearcherPropertyCollector.FieldPropertyList.Length; i++) { ListViewItem item = lstV.Items[i]; TFieldPropertyData fp = FSearcherPropertyCollector.FieldPropertyList[i]; item.SubItems[1].Text = fp.FieldCaption; item.SubItems[2].Text = fp.FieldWidth.ToString(); item.SubItems[3].Text = fp.ShowColumn.ToString(); } } } catch (Exception ex) { MessageBox.Show("发生错误!错误是:" + ex.Message); } } private string AnalyseSQL = ""; /// /// txtSql语句分析 /// /// private bool SetSQL() { bool Result = false; try { String S; string FieldName; int Locate; string S1; string S2; CboQueryField.Items.Clear(); CboReturnField.Items.Clear(); CboKeyField.Items.Clear(); lstV.Items.Clear(); AnalyseSQL = ""; S = txtSql.Text.Trim(); S1 = S.ToLower(); int Index = S1.IndexOf(" from "); if (Index == -1) { MessageBox.Show("给出的查询语句不正确,没有发现from,请重新设定。", "错误!"); return Result; } S2 = S1.Substring(Index, S1.Length - Index); S = S.Substring(0, S.Length - S2.Length); Locate = S.IndexOf(" "); if (Locate == -1) { MessageBox.Show("给出的查询语句不正确,请重新设定。", "错误!"); return Result; } S = S.Remove(0, Locate); S = S.Trim(); Locate = S.IndexOf(","); if (Locate == -1) { Locate = S.IndexOf(" "); if (Locate == -1) { MessageBox.Show("给出的查询语句不正确,请重新设定。", "错误!"); return Result; } FieldName = S.Substring(0, Locate); if (FieldName.ToUpper() == "TOP") { MessageBox.Show("查询返回的记录数请在另一个属性设定!", "错误!"); return Result; } AddField(FieldName.Trim()); CboKeyField.Items.Add(FieldName.Trim()); } else { while (S.IndexOf(",") != -1) { Locate = S.IndexOf(","); FieldName = S.Substring(0, Locate); AddField(FieldName.Trim()); CboKeyField.Items.Add(FieldName.Trim()); S = S.Remove(0, Locate + 1); } FieldName = S.Trim(); AddField(FieldName); CboKeyField.Items.Add(FieldName); } S = txtSql.Text.Trim(); S = S.Remove(0, 6); AnalyseSQL = "select top " + txtReturnMaxCount.Text + " " + S; for (int i = 0; i < CboKeyField.Items.Count; i++) CboQueryField.Items.Add(CboKeyField.Items[i]); for (int i = 0; i < CboKeyField.Items.Count; i++) CboReturnField.Items.Add(CboKeyField.Items[i]); } catch (Exception ex) { MessageBox.Show("发生错误!错误是:" + ex.Message); } Result = true; return Result; } private void AddField(String FileName) { ListViewItem item = new ListViewItem(); item.Text = FileName; item.SubItems.Add(FileName); item.SubItems.Add("2000"); item.SubItems.Add("true"); lstV.Items.Add(item); } private void btnOK_Click(object sender, EventArgs e) { ListViewItem Node; String S; if (AnalyseSQL == "") { if (SetSQL() == false) { return; } } //if(PropertyValue.Count<=0) //{ // if(SetSQL()==false) // { // return; // } //} if (AnalyseSQL == "") { MessageBox.Show("请输入SQL查询语句。", "错误!"); return; } if (CboKeyField.SelectedIndex == -1) { MessageBox.Show("请选择主键字段。", "错误!"); return; } if (CboQueryField.SelectedIndex == -1) { MessageBox.Show("请选择查询字段。", "错误!"); return; } if (CboReturnField.SelectedIndex == -1) { MessageBox.Show("请选择返回值字段。", "错误!"); return; } for (int i = 0; i < lstV.Items.Count; i++) { Node = lstV.Items[i]; if (Node.SubItems[0].Text.Trim() == "" && Node.SubItems[3].Text == "true") { S = Node.Text; S = "必须输入列" + S + "的显示描述。"; MessageBox.Show(S, "错误!"); return; } } FSearcherPropertyCollector.QuerySQL = txtSql.Text.Trim(); FSearcherPropertyCollector.SQL = AnalyseSQL.ToLower(); FSearcherPropertyCollector.KeyField = CboKeyField.Text.Trim(); FSearcherPropertyCollector.QueryField = CboQueryField.Text.Trim(); FSearcherPropertyCollector.ReturnValueField = CboReturnField.Text.Trim(); FSearcherPropertyCollector.ReturnMaxCount = int.Parse(txtReturnMaxCount.Text); FSearcherPropertyCollector.ConvertToChineseSpell = ChkConvert.Checked; FSearcherPropertyCollector.ShowFormWidth = int.Parse(txtFormWidth.Text); FSearcherPropertyCollector.ShowFormHeight = int.Parse(txtFormHeight.Text); if (ChkAuto.Checked) { FSearcherPropertyCollector.ShowFormWidth = 0; FSearcherPropertyCollector.ShowFormHeight = 0; } FSearcherPropertyCollector.FieldPropertyList = new TFieldPropertyData[lstV.Items.Count]; for (int i = 0; i < lstV.Items.Count; i++) { ListViewItem item = lstV.Items[i]; TFieldPropertyData fp = new TFieldPropertyData(); FSearcherPropertyCollector.FieldPropertyList[i] = fp; fp.FieldName = item.SubItems[0].Text; fp.FieldCaption = item.SubItems[1].Text; fp.FieldWidth = int.Parse(item.SubItems[2].Text); fp.ShowColumn = bool.Parse(item.SubItems[3].Text); } FSearcherPropertyCollector.SearchMatchWay = (TSearchMatchWay)CboSearchMatchWay.SelectedIndex; this.DialogResult = System.Windows.Forms.DialogResult.OK; } private void tabCtrl_SelectedIndexChanged(object sender, EventArgs e) { if (AnalyseSQL == "") { if (txtSql.Text.Trim() == "" || SetSQL() == false) tabCtrl.SelectedIndex = 0; } } private void txtSql_TextChanged(object sender, EventArgs e) { AnalyseSQL = ""; } private void txtReturnMaxCount_Validating(object sender, CancelEventArgs e) { try { UInt32.Parse(txtReturnMaxCount.Text); } catch (FormatException) { e.Cancel = true; MessageBox.Show("无效的值", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void txtFormWidth_Validating(object sender, CancelEventArgs e) { try { UInt32.Parse(txtFormWidth.Text); } catch (FormatException) { e.Cancel = true; MessageBox.Show("无效的值", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void txtFormHeight_Validating(object sender, CancelEventArgs e) { try { UInt32.Parse(txtFormHeight.Text); } catch (FormatException) { e.Cancel = true; MessageBox.Show("无效的值", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void txtFieldShowWidth_Validating(object sender, CancelEventArgs e) { try { UInt32.Parse(txtFieldShowWidth.Text); } catch (FormatException) { e.Cancel = true; MessageBox.Show("无效的值", "验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } public SearcherPropertyCollector searcherPropertyCollector { get { return FSearcherPropertyCollector; } set { FSearcherPropertyCollector = value; } } private void lstV_Click(object sender, EventArgs e) { if (lstV.SelectedItems == null) return; lblFieldName.Text = lstV.SelectedItems[0].SubItems[0].Text; txtFieldDescrip.Text = lstV.SelectedItems[0].SubItems[1].Text; txtFieldShowWidth.Text = lstV.SelectedItems[0].SubItems[2].Text; chkShowColumn.Checked = bool.Parse(lstV.SelectedItems[0].SubItems[3].Text); } private void btnUpdate_Click(object sender, EventArgs e) { if (lstV.SelectedItems == null) return; if (txtFieldDescrip.Text.Trim() == string.Empty) { MessageBox.Show("字段描述不能为空"); return; } MessageBox.Show(txtFieldDescrip.Text); lstV.SelectedItems[0].SubItems[1].Text = txtFieldDescrip.Text.Trim(); lstV.SelectedItems[0].SubItems[2].Text = txtFieldShowWidth.Text.Trim(); lstV.SelectedItems[0].SubItems[3].Text = chkShowColumn.Checked.ToString(); } private void CboSearchMatchWay_SelectedIndexChanged(object sender, EventArgs e) { CboMatchNames.SelectedIndex = CboSearchMatchWay.SelectedIndex; } } }