Develop WM application series with C ++ (6)-Deepen ListBox controls

Source: Internet
Author: User

Develop WM application series Article indexes using C ++

Introduction:

In the previous blog, we learned how to add items and clear all items in the ListBox control. Next to the tail of the previous article, we will introduce some tips on ListBox in this blog post, such as obtaining and deleting selected items.

Body:

First, open vs2008 and choose to create a new C ++ project. Use mfc_listbox as the project name. Of course, it is an intelligent device application based on MFC.ProgramProject. In the wizard window, select the sdk for the wm5 platform, and set the project to be based on the dialog box. Keep the default settings for other items and click Next. First, we open the resource view and see that we need to design the user interface as follows:

For a button control, set caption to show, and a cedit control, and add the variable txtedit to it. A ListBox control to add a variable: lbox. The general interface is like this. Of course, if you are interested, you can study the attributes of ListBox, which is very interesting. We will not proceed further here.

OK. After the interface is drawn, let's introduce the program logic. During program initialization, insert three items to ListBox (using addstring function ), after you select an item, Click Show. The content of the selected item is displayed in the edit control. Easy to use ~ :>

Well, we will write the following for two main processes:Code:

1. Program initialization:

First, we need to fill the ListBox at the beginning of the program. Naturally, we will find the mfc_listboxdlg.cpp: The function automatically generated by the system in the implementation file:

Bool cmfc_listboxdlg: oninitdialog (). Obviously, the function has already informed us that we can add the initialization code defined by ourselves. That's exactly what I want. You're welcome. Go directly to this function, add the following code:

 

Oninitdialog code

  Bool cmfc_listboxdlg: oninitdialog ()
{
Cdialog: oninitdialog ();

// Set the icon in the dialog box. When the application main window is not a dialog box, the framework will automatically
// Perform this operation
Seticon (m_hicon, true ); // Set a large icon
Seticon (m_hicon, false ); // Set small icon

// Todo: add additional initialization code here
Lbox. addstring (L " Test1 " );
Lbox. addstring (L " Test2 " );
Lbox. addstring (L " Test3 " );
Return True; // Returns true unless the focus is set to the control.
}

 

The three lines of lbox. addstring ("XXX") are our initialization code. In this way, after the program runs, The ListBox is filled with the three items.

2. Click the Show button to process the program:

Here, we need to add an event handler for the Show button. The adding method is as follows: Right-click the Show button and select "add event handler ".

Follow the wizard and use the default settings. ThenSource codeOn the Edit page, add the following code to the function:

 

Show button code

  Void  Cmfc_listboxdlg: onbnclickedbutton1 ()
{
Cstring strselect; // Set a string variable to obtain the selected items in ListBox.
Int nindex = 0 ; // Int variable used to specify the index number of the selected item
Nindex = Lbox. getcursel (); // Obtain the index number of the selected item in ListBox
Lbox. gettext (nindex, strselect ); // Pass the selected index number to the gettext function and assign the selected item to the strselect variable.

Txtedit. setwindowtext (strselect ); // Display the content of the selected item
}

As you can see, you can use the getcursel () method of ListBox to obtain the index of the selected item. The returned value is int. Then, you can use the gettext () function to obtain the content of a specific index.

Finally, assign the selected item to the edit control and use the setwindowtext () method.

At this point, we have completed the acquisition of the selected items. Of course, I didn't mean it before. How can I delete the selected items? It's easy to note that there is a deletestring () function in ListBox, and the input parameter happens to be the index value of the item. Isn't that the nindex? Simple.

The code for modifying the Show button is as follows to delete the selected item:

 

Show button Code 2

  Void  Cmfc_listboxdlg: onbnclickedbutton1 ()
{
Cstring strselect; // Set a string variable to obtain the selected items in ListBox.
Int nindex = 0 ; // Int variable used to specify the index number of the selected item
Nindex = Lbox. getcursel (); // Obtain the index number of the selected item in ListBox
Lbox. deletestring (nindex ); // Delete selected items
}

 

OK. Let's write it here first. Today, I checked other widgets in the toolbox. I will take some time and write some other widgets recently.Article. Thank you for your attention ~ If you have any shortcomings, please share them with us ~ Hey!

End:

In this study, we have a deep understanding of the selected items in ListBox, such as the getcursel function, used to obtain the index of the selected item; The gettext (INT index) function, used to obtain the content of a specified index. The deletestring (INT index) function is used to delete a specified index. Of course, there are still many methods that need to be explored ~~~~

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.