?
Solved

MFC CEdit loses focus

Posted on 2011-02-28
8
Medium Priority
?
1,319 Views
Last Modified: 2012-05-11
Hi.
I have MFC application and one of my views(derived from CFormView) has CHtmlView and some CEdit inside on it.
CHtmlView displays content of html file and I have a timer (CMyHtmlView::OnTimer), which refreshes this html view and displays new info. (CHtmlView::refresh function, see code attachment)

I have "introduced" shortcuts, so if user presses one of the shortcuts - relative CEdit will gain focus.  This is done in CMyView::PreTranslateMessage(see code attachment for example)

The problem is, that if I make left mouse click on CHtmlView, when I press F4(and CEdit will gain focus and selection),
then after the next time CHtmlView::refresh will be called, CEdit will loose focus.

If I select text in CEdit by mouse or I press F4 without mouse click on CHtmlView or even if I make mouse click on some other button/window/view/frame no matter what (and then I press F4) - after CHtmlView::refresh focus will still be on CEdit.


Any ideas, how to fix that?

P.S Sorry for my english :)

void CMyHtmlView::OnTimer(UINT nIDEvent)
{
	if(m_bUpdateNeeded)
	{
		m_bUpdateNeeded = FALSE;
		Refresh();// load new content from file
	}
	CHtmlView::OnTimer(nIDEvent);
}
//
BOOL CMyView::PreTranslateMessage(MSG* pMsg)
{
.....
switch((int)pMsg->wParam)
{
case VK_F4: 
        m_OneOfCEdit.SetFocus();
	m_OneOfCEdit.SetSel(0,10);
        return TRUE;
}
.....
}

Open in new window

0
Comment
Question by:SpringMVC
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 35

Expert Comment

by:sarabande
ID: 34998030
can you show code of Refresh function?

you may try in OnTimer

   bool edit_has_focus = (GetFocus() == &m_OneOfCEdit);
   Refresh();// load new content from file

   if (edit_has_focus == true)
      m_OneOfCEdit.SetFocus();

Sara
   
0
 

Author Comment

by:SpringMVC
ID: 35006401
Refresh function is library function, CHtmlView::Refresh...
0
 

Author Comment

by:SpringMVC
ID: 35006525
CMyHtmlView::OnTimer
doesn't know anything about m_OneOfCEdit..

m_OneOfCEdit and CMyHtmlView are both defined ( and used ) in CMyView as UI elements...
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:sarabande
ID: 35006528
ok, i found an inline implementation of CHtmlView::Refresh which calls m_pBrowserApp->Refresh where m_pBrowserApp is type  ::ATL::CComPtr<IWebBrowser2>

i think it is good chance that the Refresh would set the focus differently. did you try the code above?

Sara

0
 
LVL 35

Expert Comment

by:sarabande
ID: 35006564
then try:

   CWnd * pOneOfCEdit = ((CMyView*)GetParent())->GetDlgItem(IDC_ONEOFCEDIT);
   bool edit_has_focus = (GetFocus() == pOneOfCEdit );
   Refresh();// load new content from file

   if (edit_has_focus == true)
      pOneOfCEdit->SetFocus();

the code assumes that the control id of m_OneOfCEdit is IDC_ONEOFCEDIT and that CMyView is parent of CMyHtmlView.

Sara


0
 

Author Comment

by:SpringMVC
ID: 35006609
Ok. Tnx, I try it a little bit later..
By the way, if I change code from using CHtmlView::refresh to CHtmlVieww::Navigate2(file_with_content)
focus won't dissapear.
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 35007584
if the Navigate2 is a working alternative you should use it. those special views like htmlview or listview were not made for use as controls and do more than needed.

Sara
0
 

Author Closing Comment

by:SpringMVC
ID: 35411175
A
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question