"MFC" Student information management, implementation of List control node additions and deletions

Source: Internet
Author: User
Tags delete key

Previously in the MFC column, wrote a lot about the Win32 program,

In fact, nothing, Win32 is the basis of MFC,

MFC is just a Win32 extension, the system comes with the extension, a new MFC as a new Win32 program, but this Win32 program with a lot of empty function framework at the beginning.


I. BASIC OBJECTIVES

To establish the Student Information management MFC program as shown, of course, this program should consider connecting the database later, Access,sql server can,

At the same time, with the "MFC" with the dialog page to implement user login (click the open link), so that users can log in to manage information, of course, this is something.

1, this program, the school number, name, class edit box to enter the information, click the "Add" button, will insert the information into the list control


2, if you do not select any node, click on the "Modify" button will prompt the user to select the item in order to modify the list node information, the user selects one of the nodes, ask the user whether to modify, if not, then nothing happens, if so, then the current edit box in the number, name, Class replaces this node


3, delete button, also need to select a node before the user can be deleted, or the prompt will pop up. Before deleting the same pop-up dialog box for questioning, here asks the dialog box and the change, will have the current selection node information


4, Close button as the name implies, close the current window



Second, the production process

1, in VC6 Select File-New:, in the pop-up window select MFC AppWizard (EXE), enter the project name, select Save location, click Confirm



2, then not as directly as the console program to create a new, in the first step of the wizard, select the "Basic dialog" application type, then click Next



3, in the second step of the wizard, all the checkboxes are changed to unchecked, do not select any one, the title of the dialog here can be changed, after the control edit can change, no matter, now change can also, remember to remove all the tick to point the next



4, the final step of the wizard, "Do you want to use the MFC library?" "Change to" as a static DLL "can point to" done "



5, then directly to the WIN32 program exactly the same as the control editing interface, where the only one by one point is to set the control properties, not like the WIN32 program double-click, but to right-click the control, select Properties, to change, double-click the property here is the addition of new member functions. Unfamiliar with the WIN32 program interface editing can refer to my previous "MFC" Basic dialog Box Program-Adder (click to open the link)



6, at the beginning of the system will be with the "OK" and "Cancel" button, press DELETE key to delete the "OK" button, the "Cancel" button to retain, pull to the lower right corner, the caption to "Close", where the "close" Do not Like "MFC" Basic dialog box program-adder (Click to open the link) inside the WIN32 program to write code for this button, MFC has the "Cancel" button is encapsulated, click to close. Then drag into the 3 button controls, where the control toolbar is located just below the edit box EditField, rename it to add, modify, delete, and use the alignment tool below to arrange them. The position of the list control to change the view in the Style page of its properties to "Report", "Single selection" tick.



7, and then the whole large dialog box caption changed to "Student management system", at the top of the "learning number" and other edit boxes and static text, as shown in the overall, in order to make the program beautiful, please use the following Alignment tool layout good. Finally, double-click the 3 button to add a member function to it. The name of the member function is the default, without renaming. You have not changed your name, personally like it. Then, like the Win32 program, cut into ClassView and start writing code.



8, ClassView as shown in, because here using MFC, rather than a simple WIN32 program, so there are many functions at the beginning. Since my project name is Stumanager, it will automatically generate Cstumanagerapp.h,cstumanagerapp.cpp,cstumanagerdlg.h, CStumanagerDlg.cpp and other 4 documents, divided into two major procedures, there are many classes, there is no need to consider the temporary. We only need to pay attention to the Cxxxdlg in OnInitDialog (), with the three member functions just created OnButton1 (), OnButton2 (), OnButton3 (), XXX is the project name, I am here stumanager.



9, OnInitDialog () is a program load function, it has something in it, because it needs to load icons for the program, loading dialog boxes, we click on the ClassView in the OnInitDialog (), do not move inside the existence of something, in//TODO:  ADD Extra initialization here, return TRUE; Return TRUE unless you set the focus to a control before writing the code. We want to initialize the list control and load students, names, classes, and so on. So the code for OnInitDialog () is modified as follows:

BOOL Cstumanagerdlg::oninitdialog () {cdialog::oninitdialog ();//Set The icon for this dialog.  The framework does this automatically//when the  application ' s main window was not a Dialogseticon (M_hicon, TRUE);//Se T Big Iconseticon (M_hicon, FALSE);//Set small icon//todo:add Extra initialization here//This is the pointer that gets the idc_list1 of the list control to manipulate CLISTC TRL *plist= (CListCtrl *) GetDlgItem (IDC_LIST1);//Insert a column, study number, left alignment, width of 100, and then so on, is the title bar inside the list control Plist->insertcolumn ( 0, "School Number", lvcfmt_left,100);p list->insertcolumn (1, "name", lvcfmt_left,100);p list->insertcolumn (2, "Class", Lvcfmt_ LEFT,100); return TRUE;  Return TRUE  Unless you set the focus to a control}

10, click on the ClassView in the OnButton1 (), we create a member function, that is, click on the "Add" button to trigger the action, the member function is not at the beginning of the thing

void Cstumanagerdlg::onbutton1 () {//Todo:add your control notification handler code here//This is the Get Manipulation list control idc_ LIST1 pointer CListCtrl *plist= (CLISTCTRL *) GetDlgItem (IDC_LIST1);//Here is how many items appear in the list control, in order for us to insert to the end, to find the current end of the position is how much// For example, there are 3 columns now, so we should insert int ncount=plist->getitemcount () in column 3rd, and/or this is WIN32 string, in order to match the GetDlgItemText function below CString str;// Put the edit box idc_edit1, which is the "learning number" in the content, still to the string, and then into the last line, and so on and so on GetDlgItemText (IDC_EDIT1,STR);p List->insertitem (ncount, STR); GetDlgItemText (IDC_EDIT2,STR);p list->setitemtext (NCOUNT,1,STR); GetDlgItemText (IDC_EDIT3,STR);p list->setitemtext (NCOUNT,2,STR);}

Note here that inserting the first item with InsertItem, after which it should be unified with the setitemtext, because the principle of insertion is to create a new column, after the No. 0 item, after the unification is set to empty, the following work is to change the subsequent empty items to the value you want, so that the user appears to be inserted together.


11. Click the OnButton2 () in ClassView, and the action triggered after the "modify" button is written.

void Cstumanagerdlg::onbutton2 () {//Todo:add your control notification handler code here//This is the Get Manipulation list control idc_ LIST1 pointer CListCtrl *plist= (CLISTCTRL *) GetDlgItem (IDC_LIST1);//Here is the position to get the selection, if there is no choice, the two lines of the function finally find out the Nsel is-1, if selected, The Nsel is the number of rows currently selected position pos=plist->getfirstselecteditemposition (); int Nsel=plist->getnextselecteditem (POS); if (nsel<0)//Do not select anything, on the pop-up window, AfxMessageBox is the function in MFC, you can also use the Win32 MessageBox pop-up window,//Here only two parameters, you can not like the MessageBox set the title of the popup window, The default is our project name, but the advantage is that you don't have to write the handle who AfxMessageBox ("Please select the item you want to modify!") ", MB_OK); else{//choose something, pop-up window select User, AfxMessageBox get idyes value, that is, the user press" yes "to make the following modification code if (AfxMessageBox (" Confirm the modification? ") ", Mb_yesno) ==idyes) {CString str; GetDlgItemText (IDC_EDIT1,STR)///The current idc_edit1 is the value of the "study number" edit box to replace the selected row of the No. 0 item, and so on, this is done to modify Plist->setitemtext (nsel,0, STR); GetDlgItemText (IDC_EDIT2,STR);p list->setitemtext (NSEL,1,STR); GetDlgItemText (IDC_EDIT3,STR);p list->setitemtext (NSEL,2,STR);}}

12, click on the ClassView in the OnButton3 (), for the "delete" button to write after the action triggered

Actions triggered by the Delete button are essentially similar to the Modify button logic

void Cstumanagerdlg::onbutton3 () {//Todo:add your control notification handler code here//This is the Get Manipulation list control idc_ LIST1 pointer CListCtrl *plist= (CLISTCTRL *) GetDlgItem (IDC_LIST1);//Here is the position to get the selection, if there is no choice, the two lines of the function finally find out the Nsel is-1, if selected, The Nsel is the number of rows currently selected position pos=plist->getfirstselecteditemposition (); int Nsel=plist->getnextselecteditem (POS); if (nsel<0) AfxMessageBox ("Please select the item you want to delete! ", MB_OK); else{//The IF in this case, the selected row is deleted by GetItemText to get the information of the row now selected, and after the user is determined. if (AfxMessageBox ("Confirm Delete: \ n:" +plist->getitemtext (nsel,0) + "\ n Name:" +plist->getitemtext (nsel,1) + "\ n class:" + Plist->getitemtext (nsel,2) + "\ n students?" ", Mb_yesno) ==idyes) Plist->deleteitem (Nsel);}}

Since the "Close" button has been encapsulated by MFC, no need to write code, the original "Cancel" button to change the name of the line. This compiles and runs, the whole program is done. Although the whole procedure is very complex, it becomes a breeze when you are skilled.

"MFC" Student information management, implementation of List control node additions and deletions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.