⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 isecombobox.cpp

📁 动态连接ODBC数据库.使用通过数据源的名称动态链接访问ODBC数据库
💻 CPP
字号:
// ISEComboBox.cpp : implementation file
//

#include "stdafx.h"
#include "DynData.h"
#include "ISEComboBox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CISEComboBox

CISEComboBox::CISEComboBox()
{
	m_bAutoComplete = FALSE;
	m_pConPool = NULL;
}

CISEComboBox::~CISEComboBox()
{
	// delete list of strings
	while (!m_StringList.IsEmpty())
		m_StringList.RemoveHead();
}//CISEComboBox::~CISEComboBox()


BEGIN_MESSAGE_MAP(CISEComboBox, CComboBox)
	//{{AFX_MSG_MAP(CISEComboBox)
	ON_CONTROL_REFLECT(CBN_EDITUPDATE, OnEditupdate)
	ON_CONTROL_REFLECT(CBN_DROPDOWN, OnDropdown)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CISEComboBox message handlers

void CISEComboBox::OnEditupdate() 
{
	// if we are not to auto update the text, get outta here
	if (!m_bAutoComplete) 
      return;

	// Get the text in the edit box
	CString str;
	GetWindowText(str);
	int nLength = str.GetLength();
  
	// Currently selected range
	DWORD dwCurSel = GetEditSel();
	WORD dStart = LOWORD(dwCurSel);
	WORD dEnd   = HIWORD(dwCurSel);

	CString sISE = GetConcatinationString(str);

	// Search for, and select in, and string in the combo box that is prefixed
	// by the text in the edit box
	if( sISE.GetLength())
	{
	      str  += sISE;
		  SetWindowText(str);
		  SetEditSel(dStart,dEnd);
		  if (dwCurSel != CB_ERR)
				SetEditSel(dStart, dEnd);	//restore cursor postion
	  }//if( sISE.GetLength())
  
	  // Set the text selection as the additional text that we have added
	  if (dEnd < nLength && dwCurSel != CB_ERR)
		  SetEditSel(dStart, dEnd);
	  else
		  SetEditSel(nLength, -1);	
}//void CISEComboBox::OnEditupdate() 

BOOL CISEComboBox::PreTranslateMessage(MSG* pMsg) 
{

	// Need to check for backspace/delete. These will modify the text in
	// the edit box, causing the auto complete to just add back the text
	// the user has just tried to delete. 

	if (pMsg->message == WM_KEYDOWN)
	{
		m_bAutoComplete = TRUE;

		int nVirtKey = (int) pMsg->wParam;
		if (nVirtKey == VK_DELETE || nVirtKey == VK_BACK)
			m_bAutoComplete = FALSE;
	}
	return CComboBox::PreTranslateMessage(pMsg);
}

// Function : GetConcatinationString
// Purpose  : Gets a concatination string that should be added to the value of the 
//			  calling field ex. sValue = "hu" . return = "ssam"
// Param    : Text in the current field
// Return   : Concatinating string
CString CISEComboBox::GetConcatinationString(CString sCurValue)
{

	CString sSql,sRet;
		
	if( sCurValue.GetLength() <= 1)
	{
			// delete list of strings
		while (!m_StringList.IsEmpty())
			m_StringList.RemoveHead();

		sSql.Format("SELECT DISTINCT %s FROM %s WHERE %s LIKE '%s*' ORDER BY %s",m_sColumnName,
			m_sTableName,m_sColumnName,sCurValue,m_sColumnName);
		
		
		CDBConnection * pCon = m_pConPool->GetConnection(m_sDSN);
		if(pCon->ExecuteStatement(sSql))
		{
			sRet =  pCon->GetNextRow();		
			while(sRet.GetLength())
			{
				m_StringList.AddTail(sRet);
				sRet =  pCon->GetNextRow();		
			}//while(sRet.GetLength())
		}		
	}//if( sCurValue.GetLength() <= 1)

	// find the right string
	CString sTemp,sTemp2;
	BOOL Found = FALSE;
	POSITION Position = m_StringList.GetHeadPosition();
	while(!Found && Position != NULL )
	{
		sTemp = m_StringList.GetAt(Position);
		sTemp2 = sTemp.Left(sCurValue.GetLength());
		int nRes = sTemp2.CompareNoCase(sCurValue);
		if( nRes == 0)
		{
			Found  = TRUE;
			sRet = sTemp.Right(sTemp.GetLength() - sTemp2.GetLength());
		}
		else
			if(nRes > 0)
			{
				Found = TRUE;
				sRet = "";
			}
			m_StringList.GetNext(Position);
	}//while(!Found)
	return sRet;	
}//CString CISEComboBox::GetConcatinationString(CString sCurValue)

void CISEComboBox::SetConnectionPool(CConnectionPool *pConPool)
{
	m_pConPool = pConPool;
}

// Function : OnDropdown
// Purpose  : Retrives all possible values in a database column that is connected to this combo
// Param    : Column in database that is connected to this field
void CISEComboBox::OnDropdown() 
{
	CString sSql,sRet;
	
	ResetContent();

	sSql.Format("SELECT DISTINCT %s FROM %s ORDER BY %s",
		m_sColumnName,m_sTableName,m_sColumnName);
		
	CDBConnection * pCon = m_pConPool->GetConnection(m_sDSN);
	if(pCon->ExecuteStatement(sSql))
	{
		sRet = pCon->GetNextRow();

		while(sRet.GetLength() > 0)
		{
			sRet.TrimLeft();
			sRet.TrimRight();
			AddString(sRet);
			sRet = pCon->GetNextRow();
		}
	}
}//void CISEComboBox::OnDropdown() 

⌨️ 快捷键说明

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