Dynamically adding method to created controls

This is a discussion on Dynamically adding method to created controls within the Windows Programming forums, part of the Platform Specific Boards category; I want to creat edit boxes once the user clicks on a button. The number created will depend on some ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Dynamically adding method to created controls

    I want to creat edit boxes once the user clicks on a button. The number created will depend on some other parameters. I can create the edit boxes but I can't add functionality tothem at the same time.

    If I had an existing edit box, say Edit1, this is what I'd like it to do:
    Code:
    void CInitialGUIDlg::OnSetfocusEdit1() 
    {
    	// TODO: Add your control notification handler code here
    	CString str;
    	CListBox* pGetList = (CListBox*) GetDlgItem(IDC_LIST1);
    
    	pGetList->GetText((pGetList->GetCurSel()),str);
    
    	m_Edit1 = str;
    	 UpdateData(false);
    	
    }
    But now I want to add that functionality to this edit box once it's dynamically created.

    Code:
    void CInitialGUIDlg::OnButton2() 
    {
    	// TODO: Add your control notification handler code here
    	
    	CEdit* pEdit = new CEdit;
    	pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
          CRect(325, 100,400, 125), this, 1);
    
    /*
    CString str;
    	CListBox* pGetList = (CListBox*) GetDlgItem(IDC_LIST1);
    
    	pGetList->GetText((pGetList->GetCurSel()),str);
    
    	m_Edit1 = str;
    	 UpdateData(false);
    
    */
    }
    Is there a way to do it.

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Maybe you can create them hidden, then only show them when the user clicks the button? Like I mentioned in the other thread, i am not so familiar with MFC, and so I am not sure how to handle the DDX with dynamically created controls or how to add message handlers.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    >>I can create the edit boxes but I can't add functionality

    What kind of functionality do you need?

    Look at 'subclassing'.

    For dynamically created controls you would need to handle WM_COMMAND msg's (OnCommand) and switch based on the controls ID number or HWND. Then apply you specific functionality.

    But you set every controls ID to 1 (as per the slack MSDN example) and have local (not member) variable to hold the controls.

    Depending on complexity / number of choices I would create a dlg for each choice and show the correct one as needed.
    I would 'paste' the dlg on to a 'frame' control on the parent dlg. Make sure both move together by processing the move/size/close msgs (and so appear as one dlg).
    "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

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Depending on complexity / number of choices I would create a dlg for each choice and show the correct one as needed.
    I would 'paste' the dlg on to a 'frame' control on the parent dlg. Make sure both move together by processing the move/size/close msgs (and so appear as one dlg).
    If I understand correctly (which I'm really skeptical about), you are suggesting to make a new window (dlg) for each of the edit boxes. If I get into that I will very confused in a hurry. I just started learning Visual C++, and trying to stick to simple stuff.

    Using good old MSDN, I can now input the selected text from the listbox into the edit box, but as soon as Button2 is pressed. I tried using the OnSetFocus function but it disagrees with me. The only thing missing is to take the listbox Item and copy it into the editbox only when the mouse is clicked on the editbox.

    Here's what I got:
    Code:
    void CInitialGUIDlg::OnButton2() 
    {
    	// TODO: Add your control notification handler code here
    	
    	CEdit* pEdit = new CEdit;
    	pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER,
          CRect(325, 100,400, 125), this, 1);
    
    // If the mouse button is pressed, do this:
    
    	CString str;
    	CListBox* pGetList = (CListBox*) GetDlgItem(IDC_LIST1);
    
    	pGetList->GetText((pGetList->GetCurSel()),str);
    
    	CWnd* pWnd = pEdit; 
        pWnd->SetWindowText(str);
    ............
    }
    Last edited by earth_angel; 06-24-2005 at 07:09 AM.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,856
    Can you 'get' the text again though. I think you will have lost the handle to the edit and will find it hard to get back. Look at the last param of the Create(). This is the ID number for the edit. Make each one on the dlg different.

    >>you are suggesting to make a new window (dlg) for each of the edit boxes.

    No.
    I was trying to suggest that you use the resource editor to create a set of small dlgs. One for each group of edits, like you see in the wizards or on TAB controls. Then make the two dlgs appear as one by moving them together.

    May be more complex than you need.

    >>The only thing missing is to take the listbox Item and copy it into the editbox only when the mouse is clicked on the editbox.

    Look at WM_LBUTTONDOWN msg. I posted some code for finding the control (rect) that was clicked recently using PtInRect().
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding functionality to controls on a GUI
    By earth_angel in forum C++ Programming
    Replies: 3
    Last Post: 06-16-2005, 01:35 PM
  2. confused about adding controls to main window
    By terracota in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2004, 11:35 AM
  3. Adding controls when DLL hooking
    By bennyandthejets in forum Windows Programming
    Replies: 10
    Last Post: 10-19-2003, 05:11 PM
  4. I need help on adding tooltips to controls
    By Templario in forum Windows Programming
    Replies: 6
    Last Post: 01-03-2003, 02:47 PM
  5. Adding Default values to controls in Dialog boxes
    By juhigarg in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2001, 11:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21