Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want use CRichEditCtrl and CListBox to customization my CRichEditCtrlEx control. This CRichEditCtrlEx control just like IntelliSense of VS IDE.
Now, I has some problems that when key down Return button the CListBox could not auto fill compeleted.
Can you give me some advices or sample code?

Thanks very mush!

What I have tried:

C++
void CRichEditCtrlEx::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default
	if ((nChar == 13))
	{
		// Check the SHIFT key is down or not?
		if (0 == (GetKeyState(VK_SHIFT) & 0x8000))
		{
			if (m_bIsAssistCreated)
			{
				const string buffer(1024, '\0');
				const int index = m_pAssistBox->GetCurSel();
				m_pAssistBox->GetText(index, const_cast<lptstr>(buffer.c_str()));

				m_pAssistBox->DestroyWindow();
				delete m_pAssistBox;
				m_pAssistBox = nullptr;
				m_bIsAssistCreated = false;

				const long nTotalLength = this->GetTextLength();
				const int nCurLine = LineFromChar(-1);
				const int nLength = LineLength(nCurLine);
				
				this->SetSel(nTotalLength - nLength, nTotalLength);
				this->ReplaceSel(buffer.c_str(), TRUE);
				this->Invalidate();
				return;
			}
		}
	}
	else
	{
		const int nCurLine = LineFromChar(-1);
		const int nLength = LineLength(nCurLine);
		string buffer(nLength + 2, '\0');
		this->GetLine(nCurLine, const_cast<lptstr>(buffer.c_str()), static_cast<int>(buffer.capacity()));
		buffer.erase(std::find_if(buffer.begin(), buffer.end(), [](const TCHAR ch) { return ch == '\r'; }), buffer.end());

		if (!m_bIsAssistCreated && !buffer.empty())
		{
			for (const auto item : m_vecSourceData)
			{
				if (string::npos != string(item).find(buffer))
				{
					m_pAssistBox = new CAssistBox();
					m_pAssistBox->SetSource(m_vecSourceData);
					const CSize size = GetStringSize(buffer.c_str());
					CPoint pt = GetCaretPos();
					pt.Offset(-size.cx + 2, 20);
					m_pAssistBox->Create(this, pt);
					m_pAssistBox->AutoSelect(buffer.c_str());
					m_bIsAssistCreated = true;
					break;
				}
			}
		}
		else
		{
			if (m_bIsAssistCreated)
			{
				if (!m_pAssistBox->AutoSelect(buffer.c_str()))
				{
					m_pAssistBox->DestroyWindow();
					delete m_pAssistBox;
					m_pAssistBox = nullptr;
					m_bIsAssistCreated = false;
					Invalidate();
				}
			}
		}
	}

	CRichEditCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}
Posted
Updated 14-Nov-23 21:30pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900