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
Solved

MFC CEdit loses focus

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

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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 33

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 33

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 33

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
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 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.

828 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