0

我希望我的描述能让一切更清楚。

我的想法是我必须创建一种电话簿,它使用列表控件(作为报告),有一个菜单,可以保存(到外部文件),加载(从一个),你可以添加新联系人,编辑和删除现有联系人。添加和编辑联系人时,我必须使用新对话框。比如,我有一个名为“添加联系人”的菜单按钮,它会打开一个带有编辑框的新对话框(您可以在其中输入名字、姓氏、电话号码等)。电话簿有效,没有错误,但我想让它变得更好,功能更强大:

假设我已经有一个名为 John Doe 的联系人。如果我尝试添加一个精确命名为 John Doe 的联系人,当我单击“添加”按钮时,我将程序设置为询问我:“联系人姓名已存在;其他详细信息将相应更改”,并带有 OK 和 CANCEL 选项。如果我单击确定,当然一切正常。电话、电子邮件、地址和组(即其余信息)已更改。如果我单击“取消”,“添加”对话框就会消失,它会返回到显示列表的主对话框。这也是代码的事情,但我希望它......什么都不做。如果我单击取消,我希望它返回到我的“添加联系人对话框窗口”,但要保持编辑框已经完成,就像我按下“添加”按钮之前一样。因为如果单击取消,我可以再次调用该函数,但这样我只会得到一个带有空编辑框的新“添加对话框”,而这并不是我真正想要的。当我插入新联系人(名字和姓氏)时,我希望发生同样的事情,但是我在编辑框中设置的电话号码和/或电子邮件已经存在。就像,它会在一条消息中说:“电话号码已经存在”;如果我单击“确定”,它应该只返回到主对话框(列表、报告),但是如果我单击“取消”,我希望它返回到编辑框未更改的“添加对话框”(之前键入的第一个姓名,姓氏,电话,地址,电子邮件,组)不变,所以我可以编辑已经存在的电话/电子邮件。当我插入新联系人(名字和姓氏)时,我希望发生同样的事情,但是我在编辑框中设置的电话号码和/或电子邮件已经存在。就像,它会在一条消息中说:“电话号码已经存在”;如果我单击“确定”,它应该只返回到主对话框(列表、报告),但是如果我单击“取消”,我希望它返回到编辑框未更改的“添加对话框”(之前键入的第一个姓名,姓氏,电话,地址,电子邮件,组)不变,所以我可以编辑已经存在的电话/电子邮件。当我插入新联系人(名字和姓氏)时,我希望发生同样的事情,但是我在编辑框中设置的电话号码和/或电子邮件已经存在。就像,它会在一条消息中说:“电话号码已经存在”;如果我单击“确定”,它应该只返回到主对话框(列表、报告),但是如果我单击“取消”,我希望它返回到编辑框未更改的“添加对话框”(之前键入的第一个姓名,姓氏,电话,地址,电子邮件,组)不变,所以我可以编辑已经存在的电话/电子邮件。

我希望你们能理解这个想法。我知道这是很多文字。顺便说一句,如果重要的话,该组是用单选按钮选择的。

插入函数的代码如下;我现在尝试将变量翻译成英文,这样更容易阅读(我不是以英语为母语的人,对可能出现的错误表示抱歉);

void Phonebook::OnContactAdd() // keep in mind this is everything in the programs Dlg.cpp (PhonebookDlg.cpp)
{
    Add newcontact; //the Add type, the class created for the add dialog, has some TCHAR* values FirstName, LastName etc. When I click "add button" in the add dialog, the text from the edit boxes goes accordingly to the TCHARs
    if (newcontact.DoModal()==IDOK)
    {
        TCHAR getFirstName[20],getLastName[20],getPhoneNo[20],getAdr[100],getEmail[30]; //after the classes "newcontact" TCHARs are set, these strings from here get the values already in the list and it compares them
        int i;
        for(i=0;i<list.GetItemCount();i++) // it compares the values in the edit boxes typed in the "add dialog" with the ones in each line already in the list
        {
            list.GetItemText(i,0,getFirstName,20); //gets the first name from line i
            list.GetItemText(i,1,getLastName,20);// gets last name from line i
            if (strcmp(getFirstName,newcontact.FirstName)==0 && strcmp(getLastName,newcontact.LastName)==0) //compares the firstname and lastname introduced with those from the line i and if they're equal...
                if (MessageBox("Contact name already exists; other details will be changed accordingly","Warning!",MB_ICONQUESTION | MB_OKCANCEL | MB_TOPMOST )==IDOK)
                {
                    list.SetItemText(i,2,newcontact.PhoneNo);
                    prefix(i,newcontact.PhoneNo);//function that determines the operator, not relevant to the problem
                    list.SetItemText(i,4,newcontact.Adr);
                    list.SetItemText(i,5,newcontact.Email);
                    setgrup(i,newcontact.grup); // again, this is a function that sets the group in the list according to the radio button checked; ignore it, not relevant to the problem
                    return; // it found something, it changed, it exists
                }
                //else IDCANCEL; // this is the problem! else what? if I put "else return", it exists to the list, of course; if i set "else OnCancel()" it closes the whole program
                list.GetItemText(i,2,getPhoneNo,20); // if the names are not equal, we go and check if the phone number already exists
                if (strcmp(getPhoneNo,newcontact.PhoneNo)==0)
                {
                    AfxMessageBox("Phone number already exists");
                    OnContactAdd(); //it exists and now the function is called again; that's what I was saying, but it's not what I want, I want to "cancel" and go back to editing the text boxes
                    return;
                }

                list.GetItemText(i,5,getEmail,30);//same thing for the mail, as for the phone number
                if (strcmp(getEmail,newcontact.Email)==0)
                {
                    AfxMessageBox("Email already exists");
                    OnContactAdd();
                    return;
                }
        }
        // if the names, phone number or email weren't already in the list, there is no special case, so we just add the input data to the top of the list
        list.InsertItem(0,newcontact.FirstName);
        list.SetItemText(0,1,newcontact.LastName);
        list.SetItemText(0,2,newcontact.PhoneNo);
        list.SetItemText(0,4,newcontact.Adr);
        list.SetItemText(0,5,newcontact.Email);
        prefix(0,newcontact.PhoneNo);
        setgrup(0,newcontact.grup);
    }
}

// 现在再问一个问题(次要),也许有人知道,随机进入这里:

我必须进行“在您键入选项时进行搜索”。我这样做了。但它也应该为找到的文本着色。假设我正在搜索“Jo”并且有一个“John”和一个“Joanne”,那么应该只出现那些行(所有列、信息、找到的名称)。没问题,我就是这么做的。但是有没有办法只给约翰和乔安妮的乔上色/加粗/突出显示?就像让 Jo-es 变成红色,其余的('hn' 和 'anne' 保持黑色)。或者至少让整个文本着色,但其他列文本保持黑色,默认。对于搜索,我使用编辑框中的事件处理程序,将框中的文本与列表中的每一列逐行进行比较。如果匹配,则该行将添加到默认隐藏的新列表控件中,并且它现在位于前面。希望你明白这一点。

4

1 回答 1

0

您可以在此处发布您的 Add 课程吗?我认为您应该处理将联系人保存在“添加”CDialog 类中。

按下“确定”按钮并调用 OnOk() (IDOK) 函数开始关闭对话窗口。您正在通过按下 EndDialog() 函数来调用它,并且您没有处理其中的任何事件(您是在对话框之外执行它 - 关闭它之后),因此对话框开始关闭。检查这个很好的例子,它确实说明了这一点: http: //msdn.microsoft.com/en-US/library/wddd3ztw (v=vs.80).aspx 。

当您在显示此消息框时调用 OnCancel() 时“联系人姓名已存在;其他详细信息将相应更改”主对话框的 OnCancel() 将被调用,因为您已经离开了“添加”对话框范围。再次,我建议您处理将联系人保存在“添加”类中。尝试制作一个函数处理将检查所有条件并执行适当操作的“保存”按钮。您也可以尝试覆盖 OnOk() 函数,但我会选择该函数。将函数放在“添加”对话框中,以便它可以在“Add”类的这个实例中执行所有必要的检查。

于 2013-06-01T18:50:20.323 回答