CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2010
    Posts
    13

    autocomplete edit box

    Hello

    I would like to know how we can do an autocomplete box, which will call a callback function in order to add data in a drop down list (listBox) in a edit box.

    For instance, i type the first letters of a word , a call to the database is done and data is retrieved and display to the user in a dropdown list.

    Thanks in advance
    Last edited by mumuri; January 13th, 2010 at 09:21 AM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: autocomplete edit box

    As always, break the problem into its pieces.

    1) Identify when a user has typed a character
    2) Retrieve the edit control data
    3) Query the database
    4) Clear the list box
    5) Add data from the database to the list box

    Which piece(s) do you need help with?

  3. #3
    Join Date
    Apr 1999
    Posts
    3,585

    Re: autocomplete edit box

    If you need a sample, look here.
    Gort...Klaatu, Barada Nikto!

  4. #4
    Join Date
    Jan 2010
    Posts
    13

    Re: autocomplete edit box

    hello

    my problem is i don't know how to display the menu in my listbox

    i tried something like this, but the focus is lost in the edit box
    Code:
    void CAutocompleteDlg::OnChangeEdit1() 
    {
    	// TODO: If this is a RICHEDIT control, the control will not
    	// send this notification unless you override the CDialog::OnInitDialog()
    	// function and call CRichEditCtrl().SetEventMask()
    	// with the ENM_CHANGE flag ORed into the mask.
    	
    	// TODO: Add your control notification handler code here
    	CString test;
    	m_editBox.GetWindowText(test);
    	
    	CMenu l_menu;
     
    	RECT rect;
        m_editBox.GetWindowRect(&rect); 
    
    	l_menu.CreatePopupMenu();
    	l_menu.AppendMenu(MF_STRING, 1, "Section");
    	l_menu.AppendMenu(MF_STRING, 1, "Secte");
    	l_menu.AppendMenu(MF_STRING, 1, "Septique");
     
    	m_editBox.SetFocus();
    	int l_curSel = l_menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_NONOTIFY | TPM_RETURNCMD, rect.left, rect.bottom, this);
        l_menu.DestroyMenu();
     
    }

    thank for your example Mike Harnad, i already know about it but that's not exactly what i wan't. My problem is to display a list of word without losting the focus in the edit box.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: autocomplete edit box

    You didn't say anything about a menu. What are you trying to accomplish there?

  6. #6
    Join Date
    Jan 2010
    Posts
    13

    Re: autocomplete edit box

    i m trying to create dynamically a listBox, then add elements to it and display it above the edit control without losting the focus.

    i did it with a menu because i don't know how to create a listbox dynamically

    if you have the good syntaxe to create this autocomplete box with a listbox, i m interested .

    thanks
    Last edited by mumuri; January 13th, 2010 at 09:23 AM.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: autocomplete edit box

    Quote Originally Posted by mumuri View Post
    i m trying to create dynamically a listBox, then add elements to it and display it above the edit control without losting the focus.

    i did it with a menu because i don't know how to create a listbox dynamically

    if you have the good syntaxe to create this autocomplete box with a listbox, i m interested .

    thanks
    I broke down the steps for you. Take them one at a time. If you don't have a whole lot of data, you could just use a drop list combo box, load it all up at once, and it'll do the work for you.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: autocomplete edit box

    Quote Originally Posted by mumuri View Post
    ...i did it with a menu because i don't know how to create a listbox dynamically
    Well, according to MSDN it is quite easy (see CListBox Class ):
    ... To create it directly, construct the CListBox object, then call the Create member function to create the Windows list-box control and attach it to the CListBox object.
    Victor Nijegorodov

  9. #9
    Join Date
    Jan 2010
    Posts
    13

    Re: autocomplete edit box

    ok i managed to do it thanks to your advices

    Code:
            CString test;
    	m_editBox.GetWindowText(test);
    	
    	//CMenu l_menu;
    	CListBox * l_box = new CListBox();
     
    	RECT rect;
            m_editBox.GetWindowRect(&rect);
    	ScreenToClient(&rect);
     	rect.top = rect.bottom;
    	rect.bottom += 100;
    
    	l_box->Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD|WS_HSCROLL, rect ,this,ID_LISTBOX);
    	l_box->AddString("choice 1");
    	l_box->AddString("choice 2");
    	l_box->AddString("choice 3");
    	l_box->AddString("choice 4");
    	l_box->AddString("choice 5");
    	l_box->AddString("choice 6");
    now how can i add an event in my listbox to retrieve the content of the listBox ?
    Last edited by mumuri; January 13th, 2010 at 11:02 AM.

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: autocomplete edit box

    Handle LBN_SELCHANGE notification.
    Victor Nijegorodov

  11. #11
    Join Date
    Jan 2010
    Posts
    13

    Re: autocomplete edit box

    ok thank for your reply

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured