Thread: Subclassed editbox: Do I need to create 2 subclasses for 2 edit controls?

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    5

    Subclassed editbox: Do I need to create 2 subclasses for 2 edit controls?

    Hi there.

    I am using

    Operating System: Windows XP
    Project: Visual Studio 2003 C++ MFC Dialog Based Application

    I subclassed an edit box control because I want to capture the WM_KEYDOWN messages when a user enters characters in the edit box. I need this because I want to measure the time interval between keystrokes for the phrase (a password in this case) that the user enters in the edit box.

    But I want the user to enter the password twice, once in one edit box and again in another. So I created two edit boxes and then to each edit box added a control variable of type the custom edit box subclass I created.

    Now the problem is that when a enter a character in either of the edit boxes, the OnKeyDown function of the subclass is called.

    How do I know in which edit box did the user type in the key?

    Should I rather create two subclasses for the edit boxes, and create a control variable for the one editbox that is of type: the one subclass, and to the other editbox I add a control variable that is of type: the other subclass?

  2. #2
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    First off, post in the Windows Programming section, next time.

    As for your question, you're processing in a WndProc function, so you could just do a test against your HWND type argument. Or you could retreive the ID from the HWND handle and check which control it's from.
    Code:
    void function(void)
     {
      function();
     }

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    5
    Hi Jaken

    I am not processing in the WndProc function.

    Lets say the class that I created, the custom edit box class, is called CMyEditControl. Then the function that I use to process the event that a key was pressed in the edit box looks like this:

    Code:
    void CMyEditControl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    	// TODO: Add your message handler code here and/or call default
    
    	char pressedKey;
    	pressedKey = char(nChar); 
    	short keystate;
    
    	switch(nChar)
    	{
    	case VK_SHIFT:
    		// do whatever
                    case VK_LEFT:
                                    // do whatever
    	}
    
    }
    The OnKeyDown is the default name for the event handler( the one that MFC creates for you). How do I get hold of the HWND handle from here (from within this function).

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Assuming 'CMyEditControl' is an mfc CWnd derived class then either use the CWnd::m_hWnd member variable or the CWnd::operator HWND.

    Please post any future windows specific questions to the windows board.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. edit controls
    By ZerOrDie in forum Windows Programming
    Replies: 11
    Last Post: 04-08-2003, 12:09 PM
  2. I need help disabling Keyboard and Mouse input on Edit controls
    By Templario in forum Windows Programming
    Replies: 4
    Last Post: 01-07-2003, 12:59 AM
  3. Edit controls
    By Yatta in forum Windows Programming
    Replies: 3
    Last Post: 05-22-2002, 02:40 AM
  4. Edit Controls and such
    By Unregistered in forum Windows Programming
    Replies: 2
    Last Post: 12-01-2001, 11:52 AM
  5. Edit controls not sending messages to parent window
    By EMiller in forum Windows Programming
    Replies: 5
    Last Post: 11-13-2001, 11:03 PM