Edit-Feld, Text hinzufügen



  • Hi,

    ich wollte mal nachhöre ob jemand weiß ob es möglich ist einem editfeld einen Text hinzuzufügen. Also wenn das jetzt schon was drin steht und man es einfach aneinander reihen will.

    ich mache es halt erst mit getwindowtext, adde den string der dazu soll und mache dann wieder setwindowtext. weiß jemand wie das eleganter geht?

    Mfg



  • So...

    UpdateData(TRUE);
    CString Text="Test";
    
    //Edit - Feld Variable
    //m_Editfeld (CString)
    
    m_Editfeld=m_Editfeld + Text;
    
    UpdateData(False);
    


  • void AppendTextToEditCtrl(CEdit& edit, LPCTSTR pszText)
    {
       // get the initial text length
       int nLength = edit.GetWindowTextLength();
       // put the selection at the end of text
       edit.SetSel(nLength, nLength);
       // replace the selection
       edit.ReplaceSel(pszText);
    }
    

    oder:

    string buffer = "append this!"
    HWND hEdit = GetDlgItem (hDlg, ID_EDIT);
    int index = GetWindowTextLength (hEdit);
    SetFocus (hEdit); // set focus
    SendMessageA(hEdit, EM_SETSEL, (WPARAM)index, (LPARAM)index); // set selection to end of text
    SendMessageA(hEdit, EM_REPLACESEL, 0, (LPARAM)buffer.c_str()); // append
    

    https://support.microsoft.com/en-us/kb/109550



  • @HermannGo
    http://www.developerfusion.com/article/1710/avoiding-updatedata/2/

    Hab jetzt das in meinen files gefunden. Ob das eleganter ist weiß ich nicht.

    void CSOSDlg::AppendLinesToEditCtrl( LPCTSTR pszText )
    {
    	// get the initial text length
    	int nLength = c_msg_edit.GetWindowTextLength() + static_cast<int> (_tcslen( pszText ));
    	if( nLength > 30000 )
    		c_msg_edit.SetWindowText( pszText );
    	else
    	{
    		// put the selection at the end of text
    		c_msg_edit.SetSel(nLength, nLength);
    		// replace the selection
    		c_msg_edit.ReplaceSel(pszText);
    	}
    }
    

    EDIT:
    Normalerweise mach ich es mir aber auch einfacher und benutze nur
    GetWindowText + SetWindowText


Anmelden zum Antworten