Thread: Set focus by object ID?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    4

    Unhappy Set focus by object ID?

    Hi,
    I try to set focus by object ID, but I haven't got any idea. I have got cell 3x3 of CEdit and I want do something like Excel ... by arrows change focus ...

    Thanks for your ideas ...
    Stanley

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    and what does it have to do with C++...

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    4
    for example I have:
    Code:
    ...
    
    #define ID_EDIT1      101
    #define ID_EDIT2      102
    #define ID_EDIT3      103
    #define ID_EDIT4      104
    #define ID_EDIT5      105
    #define ID_EDIT6      106
    #define ID_EDIT7      107
    #define ID_EDIT8      108
    #define ID_EDIT9      109
    
    ...
    
    OnKeyDown(UINT nChar)
    {
          if(nChar == "KeyDown")FocusObject += 3;
          if(nChar == "KeyUp")FocusObject -= 3;
          if(nChar == "KeyRight")FocusObject += 1;
          if(nChar == "KeyLeft")FocusObject -= 1;
    
          // in FocusObject is for example 106 and I need set focus to ID_EDIT6
    
    }
    
    ...
    it isnīt function I only want show what it should do ...

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Ah I see. My bad. Didn't know it's MFC.

    Can't help you, though. I don't know MFC.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I use a custom Listview in report mode for this type of control.

    Or draw the text on a DC and use hot spots and mouse clicks (ie for tic tac toe).

    SetFocus() will change the focus to the required edit.

    In my example FocusEdit is a member to hold the current selected edit's ID (not offset from 101).

    Code:
    OnKeyDown(UINT nChar)
    {
          if(nChar == "KeyDown")FocusObject += 3;
          if(nChar == "KeyUp")FocusObject -= 3;
          if(nChar == "KeyRight")FocusObject += 1;
          if(nChar == "KeyLeft")FocusObject -= 1;
    
          //also needs test for invalid (?) presses ie Left Arrow in 101, 104, 107
    
           //validation for ID (please test as not checked)
          if(FocusObject>ID_EDIT9)
          {
                FocusObject=ID_EDIT1+(FocusOject-(ID_EDIT9+1));
          }
    
          //EDIT Add low value test, I am thinking switch / case instead of the 'if's
    
         // in FocusObject is for example 106 and I need set focus to ID_EDIT6
          CEdit *pEdit=NULL;
          pEdit=(CEdit*)GetDlgItem(FocusObject);
          if(pEdit)//no error
          {
                pEdit->SetFocus();
          }
          //handle error
    
    }
    Last edited by novacain; 06-30-2008 at 12:20 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    4
    Thanks, it works ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  2. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 05:27 PM
  3. Linked List Templates and Object Types
    By ventolin in forum C++ Programming
    Replies: 10
    Last Post: 06-16-2004, 12:05 PM
  4. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 13
    Last Post: 10-31-2002, 02:56 PM
  5. Set Classes
    By Nicknameguy in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2002, 07:40 PM