📄 addixdlg.cpp
字号:
// either there never was a field name specified or the user has
// chosen not to add the field
if (retCode == IDYES)
{
// move previous
m_nIndexIndex -= 1;
// check for this item by index, no error reporting
// if found, display info, if not treat as new index
if (getIndexInfo(m_pTableDef, &m_II, m_nIndexIndex, FALSE))
{
// list ctrl should be cleared and then set to new values for this index
setFieldListSelections(FALSE);
// most properties are readonly once a field is in a collection
disableControlsForExisting(TRUE);
}
else
{
// initialize field property values for new field
indexInitializer();
// list box should be cleared
setFieldListSelections(TRUE);
// new field have read/write properties
disableControlsForExisting(FALSE);
}
// update the dialog controls with new values
UpdateData(FALSE);
}
}
// set focus to name of field edit box
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_INDEX_NAME);
pEdit->SetFocus();
}
// user selected the "done" button--no more indexes to add/view. The only
// risk is losing an index that is under construction--check for that
void CAddIndexDlg::OnDone()
{
// set default to "ready to exit dialog"
int retCode = IDYES;
// if user has entered an index name, then warn them they will lose
// it if it is not explicitly added--we don't want to do an auto-add
// since user may have entered name without meaning to add another index
//
// see if there is a index name specified
CString name;
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_INDEX_NAME);
pEdit->GetWindowText(name);
// if there is a name, then warn the user
if (name.GetLength() != 0)
{
// only an issue if this is not an existing index
if (!IsExistentIndex(m_pTableDef, name))
retCode = AfxMessageBox(_T("Current index will be ignored unless added. Continue anyway?"),
MB_YESNO);
}
// either there never was a index name specified or the user has
// chosen not to add the index
if (retCode == IDYES)
{
// done with tabledef object
delete m_pTableDef;
// end the dialog
CDialog::EndDialog(0);
}
}
// user selected to exit the dialog--don't do any
// checking for loss of newly specified indexes
void CAddIndexDlg::OnClose()
{
// close the tabledef if it is open
if (m_pTableDef->IsOpen())
m_pTableDef->Close();
// clean up
delete m_pTableDef;
CDialog::OnClose();
}
// user wants to add the specified index to the collection
void CAddIndexDlg::OnAddIndex()
{
// get values from control -- don't continue if failure
if (!UpdateData(TRUE))
return;
// don't do anything if this is an existing index (except
// say so)
if (!IsExistentIndex(m_pTableDef, m_II.m_strName))
{
// must create the fields that are in the index
createFieldArray(&(m_II.m_pFieldInfos), &(m_II.m_nFields));
// try to create the index with error checking--may fail if a
// duplicate named index already exists. Note: creating an index
// also appends it to the tabledef's index collection
if (!createNewIndex(m_pTableDef, &m_II))
return;
// clean out all properties
indexInitializer();
// list constrol should be cleared
setFieldListSelections(TRUE);
// performs visible clearing of controls that are initialized
UpdateData(FALSE);
// move to next field index
m_nIndexIndex += 1;
}
else
{
AfxMessageBox(_T("Can't add index--it already exists in the TableDef."));
}
// set focus to name of index edit box
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_INDEX_NAME);
pEdit->SetFocus();
}
// user selected to delete the current index--prompt for acceptance
void CAddIndexDlg::OnDeleteIndex()
{
// get values from control -- don't continue if failure
if (!UpdateData(TRUE))
return;
// can only delete existing indexes
if (IsExistentIndex(m_pTableDef, m_II.m_strName))
{
// is user sure?
if (IDYES == AfxMessageBox(_T("Delete current index?"), MB_YESNO))
{
// only react if field is deleted!
if (deleteIndex(m_pTableDef, m_II.m_strName))
{
// index into collection is unchanged, so
// get the information for this index if there is one
// (no error reporting)
if (getIndexInfo(m_pTableDef, &m_II, m_nIndexIndex, FALSE))
{
// list control should be cleared and then set to reflect new index
setFieldListSelections(FALSE);
// disable user selections
disableControlsForExisting(TRUE);
}
else
{
// there is no index in collection following the
// deletion at this collection index, so
// set the index info to initial state
indexInitializer();
// list control should be cleared
setFieldListSelections(TRUE);
// enable user selections
disableControlsForExisting(FALSE);
}
// update the dialog controls to erase deleted field
UpdateData(FALSE);
}
}
}
// set focus to name of field edit box
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_INDEX_NAME);
pEdit->SetFocus();
}
// since most index properties become read-only once the index
// is added to a collection, manage the controls on the dialog
// appropriate to whether this is an existing index or not
void CAddIndexDlg::disableControlsForExisting(BOOL bDisable/* = TRUE*/)
{
m_UniqueControl.EnableWindow(!bDisable);
m_RequiredControl.EnableWindow(!bDisable);
m_IgnoreNullControl.EnableWindow(!bDisable);
m_PrimaryControl.EnableWindow(!bDisable);
// set the name edit to read-only
CEdit *pEdit = (CEdit *)GetDlgItem(IDC_INDEX_NAME);
pEdit->SetReadOnly(bDisable);
}
// populate the list control with the fields in the field collection of
// the current tabledef
void CAddIndexDlg::populateFieldList()
{
// struct for getting field information
CDaoFieldInfo fieldInfo;
// loop controls and index into the field collection
BOOL bContinue = TRUE;
int itemIndex = 0;
// until we run out of items in the field collection, keep adding items
for (int i = 0; bContinue; i++)
{
// try to get info on the ith field -- no error reporting as specified by last
// parameter being FALSE
if (getFieldInfo(m_pTableDef, &fieldInfo, i, FALSE))
{
// if this is an indexable field type, then display it
if ((fieldInfo.m_nType != dbLongBinary) &&
(fieldInfo.m_nType != dbMemo))
{
// insert the name into the list view item, clear the subitem text
m_FieldListListControl.InsertItem(itemIndex, fieldInfo.m_strName);
m_FieldListListControl.SetItemText(itemIndex, 1, _T(""));
// move to next index into the collection
itemIndex += 1;
}
}
else // once we fail to get info on an item in the collection, stop the loop
bContinue = FALSE;
}
}
// for every item selected in the list control, create an index field info
// struct with the pertinent info and add it to the list of such
// structures
void CAddIndexDlg::createFieldArray(CDaoIndexFieldInfo **ppFields, short *pnFields)
{
// get maximum count of items in the list control
int limit = m_FieldListListControl.GetItemCount();
// if array isn't initialized, delete it, then allocate a new one
if ((*ppFields) != NULL)
delete [] (*ppFields);
// allocate at maximum size although we probably won't use all
// just for simplicity's sake!
(*ppFields) = new CDaoIndexFieldInfo[limit];
// keep track of how many items selected and loop through all items
(*pnFields) = 0;
for (int i = 0; i < limit; i++)
{
// if selected, get the name of the field (selection indicated by
// non-empty subitem string)
if (m_FieldListListControl.GetItemText(i, 1) != _T(""))
{
// put the name into the array
(*ppFields)[(*pnFields)].m_strName = m_FieldListListControl.GetItemText(i, 0);
// put the sort order into the array as a boolean
(*ppFields)[(*pnFields)].m_bDescending =
(m_FieldListListControl.GetItemText(i, 1) == _T("descending"));
// up the count by one
(*pnFields) += 1;
}
}
}
// make the list control reflect which fields make up the current index
//
// IN: bJustClear-by default, clear AND make selection in the list control, but
// you can specify to just clear the control of all selections
//
void CAddIndexDlg::setFieldListSelections(BOOL bJustClear /* = FALSE*/)
{
// get maximum number of items in list
int numStrings = m_FieldListListControl.GetItemCount();
int selection;
// reset any current selections
for (int i = 0; i < numStrings; i++)
{
m_FieldListListControl.SetItemState(i, 0, LVIS_SELECTED);
m_FieldListListControl.SetItemText(i, 1, _T(""));
}
// this function can be called to simply clear the list control
// of selections or you can reflect the current index selections too
if (!bJustClear)
{
// select the appropriate items--set up the finder struct
LV_FINDINFO lvfi;
lvfi.flags = LVFI_STRING; // searching for strings
CString strSort; // used to create string to indicate sort direction
// for all items in the index info struct, find and set the items
for (int i = 0; i < m_II.m_nFields; i++)
{
// find the item in the already populated list control--start from the top
lvfi.psz = m_II.m_pFieldInfos[i].m_strName;
selection = m_FieldListListControl.FindItem(&lvfi, -1);
// select the item
m_FieldListListControl.SetItemState(selection, LVIS_SELECTED, LVIS_SELECTED);
// set the sort order subitem text appropriately
strSort = m_II.m_pFieldInfos[i].m_bDescending ? _T("descending") : _T("ascending");
m_FieldListListControl.SetItemText(selection, 1, strSort);
}
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -