MFC Quirks: Part 2

SCROLLING A DIALOG

Sometimes you will want a text Edit Control to automatically scroll when you add a string to it. Unfortunately there isnt any support in the default class for this. I have found the easiest way to do this is to use CEdit::SetSel to fake a user selection of the last character in the control. Here is an example of that being done using a CEdit control and a std::string:

SetDlgItemText(IDC_ERRORWINDOW, ErrorWindowText.c_str());

CEdit* errorWindow = static_cast<CEdit*>(GetDlgItem(IDC_ERRORWINDOW));
errorWindow->SetSel(ErrorWindowText.length() - 1, ErrorWindowText.length(), false);

CHANGING WORKING DIRECTORY WITH OPEN/SAVE DIALOG

When making calls to either GetSaveFileName or GetOpenFileName it will in the background change the working directory of your running program which can be a problem if you use any relative paths later. To get around this you can use GetCurrentDirectory to store the current working path and then after the call to get the save/open file name you can call SetCurrentDirectory with the stored path to restore it to actual working directory.

CCOMBOBOX CLEAR DOESN'T

CComboBox has a clear function but this only clears the textbox part of the combo box object. To clear the list of entries as well you will need to also call ResetContent.