Solved

MFC CEdit loses focus

Posted on 2011-02-28
8
1,264 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
  • 4
  • 4
8 Comments
 
LVL 32

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
 
LVL 32

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 32

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 32

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now