Solved

MFC CEdit loses focus

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

717 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