Solved

MFC CEdit loses focus

Posted on 2011-02-28
8
1,298 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 34

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
Independent Software Vendors: 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 34

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 34

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 34

Accepted Solution

by:
sarabande earned 500 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

Technology Partners: 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!

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

726 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