I hope my description will make everything clearer.
The idea is I have to create a sort of phonebook, that uses a List Control (as report), has a menu and can be saved (to an external file), loaded (from one) and you can add new contacts, edit and delete existing contacts.
I have to use new dialogs when adding and editing contacts. Like, I have a menu button named "add contact" that opens a new dialog with edit boxes (in which you type the first name, last name, phonenumber etc.).
The phonebook works, there are no errors, but I want to make it a bit better, functional:
Let's say I already have a contact named John Doe. If I try to add a contact named exactly John Doe, when I click the "add" button, I set the program to ask me:
"Contact name already exists; other details will be changed accordingly" with the OK and CANCEL options.
If I click OK, everything works, of course. The phone,email, adress and group (that's the rest of info) are changed. If I click Cancel the dialog for the "add" just disappears and it goes back to my main dialog which shows the list. That's also the code's thing, but I want it to ... do nothing. If I click that CANCEL, I want it to get back to my "add contact dialog window", but by keeping the edit boxes already completed like before I pressed "add" button. Because I can just call the function again if I click cancel, but that way I just get a new "add dialog" with empty edit boxes and that's not really what I want.
The same thing I want to happen when I insert a new contact (first and last name), but the phone number and/or the email I set in the edit boxes already exist(s). Like, it will say in a message: "Phone number already exists"; and if I click "OK" it should just return to the main dialog (the list, report), but if I click "CANCEL" I want it to return to the "add dialog" with unchanged edit boxes (the before-typed first name, last name, phone, adress, email, group) unchanged, so I can just edit the phone/email that already existed.
I hope you guys understand the idea. I know it's a lot of text. By the way, the group is chosen with radio buttons, if it matters.
The code for the Insert Function is below; I tried to translate the variables in English for now, so it's an easier read (I'm not native English speaker, sorry for possible mistakes);
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);
}
}
//
Now one more question (secondary), maybe someone knows about it and randomly enters here:
I have to make a "search as you type option". I did that. But it should also colour the found text. Assuming I'm searching for "Jo" and there's a "John" and a "Joanne", only those lines (all the columns, the info, proper to the found names) should appear. No problem, I did that. But is there a way to colour/bold/highlight only the Jo from John and Joanne? Like get the Jo-es red and the rest ('hn' and 'anne' to stay black). Or at least to get the whole text colored, but the other column text to stay black, default.
For searching I use an event handler from an edit box, compare the text from the box to every column in the list, line by line. If there is a match, the line is added to a new list control that is hidden by default, and it comes in front now. Hope you understand this. Probably I'll make another topic for this also.