querydlg.cpp

来自「VC做的矢量画图程序!」· C++ 代码 · 共 482 行

CPP
482
字号
// QueryDlg.cpp : implementation file
//

#include "stdafx.h"
#include "draw.h"
#include "QueryDlg.h"
#include "BrowseDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CQueryDlg dialog


CQueryDlg::CQueryDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CQueryDlg::IDD, pParent)
{
	m_sqlStr="";
	m_type=((CBrowseDlg *)pParent)->m_type;
	//{{AFX_DATA_INIT(CQueryDlg)
	m_datetime1 = CTime::GetCurrentTime();
	m_datetime2 = CTime::GetCurrentTime();
	m_value1 = _T("");
	m_value2 = _T("");
	//}}AFX_DATA_INIT
}


void CQueryDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CQueryDlg)
	DDX_Control(pDX, IDC_RADIO1, m_radio_andor);
	DDX_Control(pDX, IDC_COM_OPE2, m_com_ope2);
	DDX_Control(pDX, IDC_COM_VALUE2, m_com_value2);
	DDX_Control(pDX, IDC_COM_VALUE1, m_com_value1);
	DDX_Control(pDX, IDC_COM_FIELD2, m_com_field2);
	DDX_Control(pDX, IDC_COM_OPE1, m_com_ope1);
	DDX_Control(pDX, IDC_COM_FIELD1, m_com_field1);
	DDX_DateTimeCtrl(pDX, IDC_DATETIME1, m_datetime1);
	DDX_DateTimeCtrl(pDX, IDC_DATETIME2, m_datetime2);
	DDX_Text(pDX, IDC_EDIT_VALUE1, m_value1);
	DDX_Text(pDX, IDC_EDIT_VALUE2, m_value2);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CQueryDlg, CDialog)
	//{{AFX_MSG_MAP(CQueryDlg)
	ON_CBN_SELCHANGE(IDC_COM_FIELD1, OnSelchangeComField1)
	ON_CBN_SELCHANGE(IDC_COM_FIELD2, OnSelchangeComField2)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CQueryDlg message handlers

BOOL CQueryDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
  
	GetDlgItem(IDC_COM_VALUE1)->ShowWindow(FALSE);
	GetDlgItem(IDC_COM_VALUE2)->ShowWindow(FALSE);
	GetDlgItem(IDC_DATETIME1)->ShowWindow(FALSE);
	GetDlgItem(IDC_DATETIME2)->ShowWindow(FALSE);

	
	strField[0]="管道编号";
	strField[1]="管径";
	strField[2]="材料";
	strField[3]="埋深";
	strField[4]="管长";
	strField[5]="管沟宽";
	strField[6]="布设时间";
	strField[7]="阀门编号";
	strField[8]="坡度";

	strField1[0]="LINE_NO";
	strField1[1]="GUANJING";
	strField1[2]="CAILIAO";
	strField1[3]="MAISHEN";
	strField1[4]="GUANCHANG";
	strField1[5]="GOUKUAN";
	strField1[6]="BUSHESHIJIAN";
	strField1[7]="FAMENNO";
	strField1[8]="PODU";

	strOperator[0]="<>";
	strOperator[1]="<=";
	strOperator[2]=">=";
	strOperator[3]="=";
	strOperator[4]="<";
	strOperator[5]=">";
	
	//向字段下拉框中添加相关字段

	for(int i=0;i<=7;i++)
	{   
		 if(m_type==2 && i==5)//如果是下水应添加坡度字段
		  {
		     m_com_field1.AddString(strField[8]);
		     m_com_field2.AddString(strField[8]);
		  }
		  else if(m_type==2 && i==7)//如果是下水不添加法门编号字段
			  continue;
		  else
		  {
		      m_com_field1.AddString(strField[i]);
		      m_com_field2.AddString(strField[i]);
		  }
	}

	//令管线编号字段选中
	m_com_field1.SetCurSel(0);
	//给第一个操作符下拉框填加操作符,并置等号选中
	for(i=0;i<=5;i++)
		m_com_ope1.AddString(strOperator[i]);
	 m_com_ope1.SetCurSel(3);
    //使单条件选中
	 m_radio_andor.SetCheck(1);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CQueryDlg::OnSelchangeComField1() 
{
	// TODO: Add your control notification handler code here
	if(m_com_field1.GetCurSel()==2)
	{   
		m_com_ope1.ResetContent();
		m_com_ope1.AddString(strOperator[3]);
	    m_com_ope1.AddString(strOperator[0]);
		m_com_value1.ResetContent();
		if(m_type==1)
		{
		  m_com_value1.AddString("普通铸铁");
		  m_com_value1.AddString("球墨铸铁");
		  m_com_value1.AddString("镀锌管");
		  m_com_value1.AddString("其它");
		}
		else if(m_type==2)
		{
		   m_com_value1.AddString("砼管");
		   m_com_value1.AddString("砖砌");
		   m_com_value1.AddString("其它");
		}
		GetDlgItem(IDC_DATETIME1)->ShowWindow(false);
		GetDlgItem(IDC_EDIT_VALUE1)->ShowWindow(false);
		m_com_value1.ShowWindow(true);
	}
	else
	{
		m_com_ope1.ResetContent();
		for(int i=0;i<=5;i++)
			m_com_ope1.AddString(strOperator[i]);
		if(m_com_field1.GetCurSel()==6)
		{
			GetDlgItem(IDC_DATETIME1)->ShowWindow(true);
		    GetDlgItem(IDC_EDIT_VALUE1)->ShowWindow(false);
		    m_com_value1.ShowWindow(false);
		}
		else
		{
			GetDlgItem(IDC_DATETIME1)->ShowWindow(false);
		    GetDlgItem(IDC_EDIT_VALUE1)->ShowWindow(true);
		    m_com_value1.ShowWindow(false);
		}
			
		
	}

}

void CQueryDlg::OnSelchangeComField2() 
{
	// TODO: Add your control notification handler code here
	if(m_com_field2.GetCurSel()==2)//如果点选了材料字段
	{
		m_com_ope2.ResetContent();
		m_com_ope2.AddString(strOperator[3]);
	    m_com_ope2.AddString(strOperator[0]);
		m_com_value2.ResetContent();
		if(m_type==1)
		{
		  m_com_value2.AddString("普通铸铁");
		  m_com_value2.AddString("球墨铸铁");
		  m_com_value2.AddString("镀锌管");
		  m_com_value2.AddString("其它");
		}
		else if(m_type==2)
		{
		   m_com_value2.AddString("砼管");
		   m_com_value2.AddString("砖砌");
		   m_com_value2.AddString("其它");
		}
		GetDlgItem(IDC_DATETIME2)->ShowWindow(false);
		GetDlgItem(IDC_EDIT_VALUE2)->ShowWindow(false);
		m_com_value2.ShowWindow(true);
	}
	else
	{
		m_com_ope2.ResetContent();
		for(int i=0;i<=5;i++)
			m_com_ope2.AddString(strOperator[i]);
	    if(m_com_field2.GetCurSel()==6)
		{
			GetDlgItem(IDC_DATETIME2)->ShowWindow(true);
		    GetDlgItem(IDC_EDIT_VALUE2)->ShowWindow(false);
		    m_com_value2.ShowWindow(false);
		}
		else
		{
			GetDlgItem(IDC_DATETIME2)->ShowWindow(false);
		    GetDlgItem(IDC_EDIT_VALUE2)->ShowWindow(true);
		    m_com_value2.ShowWindow(false);
		}
			

	}
}

void CQueryDlg::OnOK() 
{   
	
	UpdateData(true);
	//判断输入是否有效
	if(QueryValid()==false)
		return;
	//第一个查询条件
	CString str;
    int ncom=m_com_field1.GetCurSel();
	if(m_type==2 && ncom==5)
		m_sqlStr+=strField1[8];
	else
	    m_sqlStr+=strField1[ncom];
	GetDlgItemText(IDC_COM_OPE1,str);
	m_sqlStr+=str;
	switch(ncom)
	{
	case 0:
	case 7:
	 m_sqlStr+="'"+m_value1+"'";
	 AfxMessageBox(m_sqlStr);
	break;
	case 1:
	case 3:
	case 4:
	case 5:
	 m_sqlStr+=m_value1;
	 AfxMessageBox(m_sqlStr);
	 break;
	case 2:
     ncom=m_com_value1.GetCurSel();
	 if(ncom==0)
	  m_sqlStr+="1";
	 else if(ncom==1)
	  m_sqlStr+="2";
	 else if(ncom==2)
	  m_sqlStr+="3";
	 else if(ncom==3)
	  m_sqlStr+="4";	 
	 break;
	case 6:
	   m_sqlStr+="#"+m_datetime1.Format("%Y-%m-%d")+"#";
	   AfxMessageBox(m_sqlStr);
	   break;
	default:
		break;
	}
	//如果有第二个查询条件则与m_sqlStr相加
	if(((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck()!=1)
	{
        //判断是OR 还是 AND
		if(((CButton *)GetDlgItem(IDC_RADIO2))->GetCheck()==1)
			m_sqlStr+=" and ";
		else if(((CButton *)GetDlgItem(IDC_RADIO3))->GetCheck()==1)
			m_sqlStr+=" or ";
        
		ncom=m_com_field2.GetCurSel();
		if(m_type==2 && ncom==5)
		   m_sqlStr+=strField1[8];
	    else
	       m_sqlStr+=strField1[ncom];
	    
		GetDlgItemText(IDC_COM_OPE2,str);
	    m_sqlStr+=str;
	    switch(ncom)
		{
	       case 0:
	       case 7:
	       m_sqlStr+="'"+m_value2+"'";
	       AfxMessageBox(m_sqlStr);
	       break;
	       case 1:
	       case 3:
	       case 4:
	       case 5:
	       m_sqlStr+=m_value2;
	       AfxMessageBox(m_sqlStr);
	       break;
	       case 2:
           ncom=m_com_value2.GetCurSel();
	       if(ncom==0)
	         m_sqlStr+="1";
	       else if(ncom==1)
	         m_sqlStr+="2";
	       else if(ncom==2)
	         m_sqlStr+="3";
	       else if(ncom==3)
	        m_sqlStr+="4";
	      AfxMessageBox(m_sqlStr);
	 
	      break;
	      case 6:
	       m_sqlStr+="#"+m_datetime2.Format("%Y-%m-%d")+"#";
	       AfxMessageBox(m_sqlStr);
	      break;
	      default:
		  break;
		}
	}
	
	CDialog::OnOK();
}

bool CQueryDlg::QueryValid()
{
	
	//判断第一个条件的有效性
	
	if(m_com_ope1.GetCurSel()==CB_ERR)
	{
		AfxMessageBox("请选择操作符!");
        m_com_ope1.SetFocus();
		return false;
	}
	if(m_com_value1.IsWindowVisible())
	{
		
		if(m_com_value1.GetCurSel()==CB_ERR)
		{
			AfxMessageBox("请选择查询值!");
            m_com_value1.SetFocus();
		     return false;
		}
	}
		
    if(GetDlgItem(IDC_EDIT_VALUE1)->IsWindowVisible())

	{
		m_value1.TrimLeft();
		m_value1.TrimRight();
		if(m_value1=="")
		{
			AfxMessageBox("请输入查询值!");
            GetDlgItem(IDC_EDIT_VALUE1)->SetFocus();
		     return false;
		}
		if(m_com_field1.GetCurSel()==0)
		{
			 bool bTag=true;
	         for(int i=0;i<m_value1.GetLength();i++)
			 {
		       char ch=m_value1.GetAt(i);
	        	if(ch<'0' || ch>'9')
				{ 
			       bTag=false;
			        break;
				}
			 }
	        if(m_value1.GetLength()!=8 || !bTag)
			{
		        AfxMessageBox("请输入8位数字组成的编号 !");	
		        GetDlgItem(IDC_EDIT_VALUE1)->SetFocus();
             return false;
			}
		}
	}
	//如果有第二个条件则进行验证
	if(((CButton *)GetDlgItem(IDC_RADIO1))->GetCheck()!=1)
	{
		if(m_com_field2.GetCurSel()==CB_ERR)
		{ 
			AfxMessageBox("请选择查询字段!");
            m_com_field2.SetFocus();
		     return false;
		}
		if(m_com_ope2.GetCurSel()==CB_ERR)
		{
	  	    AfxMessageBox("请选择操作符!");
            m_com_ope2.SetFocus();
		    return false;
		}
		if(m_com_value2.IsWindowVisible())
		{
		   if(m_com_value2.GetCurSel()==CB_ERR)
		   {
			  AfxMessageBox("请选择查询值!");
              m_com_value2.SetFocus();
		      return false;
		   }
		   
		}
		
       if(GetDlgItem(IDC_EDIT_VALUE2)->IsWindowVisible())

	   {
		 m_value2.TrimLeft();
		 m_value2.TrimRight();
		 if(m_value2=="")
		 {
			AfxMessageBox("请输入查询值!");
            GetDlgItem(IDC_EDIT_VALUE2)->SetFocus();
		     return false;
		 }
		 if(m_com_field2.GetCurSel()==0)
		   {
			 bool bTag=true;
	         for(int i=0;i<m_value2.GetLength();i++)
			 {
		       char ch=m_value2.GetAt(i);
	        	if(ch<'0' || ch>'9')
				{ 
			       bTag=false;
			        break;
				}
			 }
	        if(m_value2.GetLength()!=8 || !bTag)
			{
		        AfxMessageBox("请输入8位数字组成的编号 !");	
		        GetDlgItem(IDC_EDIT_VALUE2)->SetFocus();
             return false;
			}
		  }
	   }
	}


	return true;

}

void CQueryDlg::OnRadio1() 
{
	
		m_com_field2.EnableWindow(FALSE);
	    m_com_ope2.EnableWindow(FALSE);
		m_com_value2.EnableWindow(FALSE);
		GetDlgItem(IDC_DATETIME2)->EnableWindow(FALSE);
		GetDlgItem(IDC_EDIT_VALUE2)->EnableWindow(FALSE);

}

void CQueryDlg::OnRadio2() 
{
	    m_com_field2.EnableWindow();
	    m_com_ope2.EnableWindow();
		m_com_value2.EnableWindow();
		GetDlgItem(IDC_DATETIME2)->EnableWindow();
		GetDlgItem(IDC_EDIT_VALUE2)->EnableWindow();
	
}

void CQueryDlg::OnRadio3() 
{
        m_com_field2.EnableWindow();
	    m_com_ope2.EnableWindow();
		m_com_value2.EnableWindow();
		GetDlgItem(IDC_DATETIME2)->EnableWindow();
		GetDlgItem(IDC_EDIT_VALUE2)->EnableWindow();	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?