Visual c++ “正确设置”;取消按钮";在ID为的消息框中,单击取消;对话框中的对话框

Visual c++ “正确设置”;取消按钮";在ID为的消息框中,单击取消;对话框中的对话框,visual-c++,mfc,messagebox,cancel-button,Visual C++,Mfc,Messagebox,Cancel Button,我希望我的描述能让一切更清楚 我的想法是,我必须创建一种电话簿,它使用列表控件(作为报告),有一个菜单,可以保存(到外部文件),加载(从一个文件),您可以添加新联系人,编辑和删除现有联系人。 添加和编辑联系人时,我必须使用新对话框。比如,我有一个名为“添加联系人”的菜单按钮,它会打开一个带有编辑框的新对话框(在其中键入名字、姓氏、电话号码等)。 电话簿工作正常,没有错误,但我想让它更好一点,功能: 假设我已经有一个叫约翰·多伊的联系人了。如果我尝试添加一个名为John Doe的联系人,当我单击“

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

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

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

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

插入函数的代码如下所示;现在我试着用英语翻译变量,这样更容易阅读(我不是英语母语,很抱歉可能会出错)

void Phonebook::OnContactAdd()//请记住,这是程序Dlg.cpp(PhonebookDlg.cpp)中的所有内容
{
Add newcontact;//Add type是为Add对话框创建的类,它有一些TCHAR*值FirstName、LastName等。当我在Add对话框中单击“Add button”时,编辑框中的文本会相应地转到TCHARs
if(newcontact.DoModal()==IDOK)
{
TCHAR getFirstName[20]、getLastName[20]、getPhoneNo[20]、getAdr[100]、getEmail[30];//设置类“newcontact”TCHAR后,这些字符串从这里获取列表中已有的值并进行比较
int i;

对于(i=0;i你能在这里发布你的Add类吗?我认为你应该在“Add”CDialog类中保存联系人

按下“确定”按钮并调用OnOk()(IDOK)函数开始关闭对话框窗口。 您正在通过按下EndDialog()函数来调用它,并且您没有处理它内部的任何事件(您在对话框外部执行此操作-在关闭它之后),因此对话框开始关闭。请检查这个很好的示例,它确实显示了这一点:


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

是的,我担心这就是问题所在。我通过一个变通方法解决了这个问题(创建了一个“exception”函数,该函数接收来自编辑框的文本作为参数;它基本上是对“AddContact”函数的另一个调用,但参数将从编辑框开始设置;在调用“exception()”之后我有一个返回,所以ADD函数将结束(以级联方式)。我知道这并不漂亮,但尝试在ADD对话框中完成整个过程。cpp将意味着重写我的代码的一大部分。我先尝试了,但我做不到,因为联系人存储在列表控件中。
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);
    }
}