C # Window Programming (3)

Source: Internet
Author: User

Textbox Control events

Each widget has many events, some of which are the same, such as mouse and keyboard events. Each of these events has different controls.

For details, go to msdn to view the events of each control.

Here I will show the event list URL of the Textbox Control in msdn:

Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.textbox_events (V = vs.80). aspx

Let's take a look at three events: keydown, keypress, and keyup.

For different types of events, the second parameter of its event processing function is also different,

The second parameter type of the event handler functions such as keydown and keyuup isKeyeventargsAnd keypress isKeypresseventargs.

The corresponding delegates are also different. For example, if you click an event, the delegate isEventhandlerClick Is a delegate for eventhandler type definition.

The delegate for keydown and keyup isKeyeventhandler, Keypress isKeypresseventhandler.

There is also a sequence of events.In the preceding three event sending sequence: (first-come-first-served) keydown, keypress, and keyup.

The keydown event is generated when you press the next key, but keypress is also generated when you press the next key, but it only supports the character key press, the system key press, no response.

The keyup event is generated when a key pops up, that is, after a key is pressed, it is released.

Explanation of the second parameter of the keydown and keyup event processing functions:

Keyeventargs is a class that contains an attribute member keycode, which is a keys Enumeration type variable. keycode stores the currently pressed key.

Definition of keys Enumeration type reference: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.keys
At the bottom

Description of the keypresseventargs class:

Keychar in the keypresseventargs class stores the ASCII code value of the pressed key. The handled Member in this class indicates whether the event is processed, that is, whether the system determines whether to handle the keypress event by default, and whether the buttons can be shielded, if the value is true, it indicates that the processing has been completed (which does not need to be processed by default ).

Example: Let textbox receive only the number key.

This example is very easy. You only need to add the keypress event processing function, and then judge whether the keychar is a number in it. If not, set the value of handled to true.

Add keypress event handler: This. textbox1.keypress + = new system. Windows. Forms. keypresseventhandler (this. textbox#keypress );

The keypress event handler code is as follows:

Private void textbox1_keypress (Object sender, keypresseventargs E)
{
If (! (E. keychar> = '0' & E. keychar <= '9 '))
E. Handled = true;
}

In this way, the Textbox Control can only receive numbers.

 

ListBox control

Like the ComboBox class of the combo box, The ListBox class also has an items attribute member, the objectcollection type, which can be viewed as an array similar to the arraylist.

If the objectcollection class has a removeat method, you can delete the items (elements) of the specified index)

Objectcollection stores object-class objects, which convert the passed values to the object type.

The same as the combo box,Items. Add ();Its parameter is the text content of the item.

The content of the selected item isListbox1.selecteditem. tostring ();

You can also use the getitemtext function, for example, listbox1.getitemtext (listbox1.selecteditem)

Listbox1.selecteditem is an object type object. The parameter type of the getitemtext function is object, so that when an object type object is uploaded,

It will be searched in the "array" in items.

 Set the font and background color of The ListBox control.
The ListBox class hasFontAttribute member. This specifies the font style. Its type is system. Drawing. Font. This font class has seven or eight
Reload the constructor. We usually set the font and only the font name and size of the constructor.
In this way, we only need to understand a constructor.Font (string, single );The second parameter indicates the font name, such as "" and the second parameter.
Is the font size, such as 10.5f.
The code for setting the font of The ListBox control is:
This. listbox1.font = new system. Drawing. Font ("Comic Sans MS", 13f); // assign a value to font
Changing the background color of the ListBox control is similar to that of C # Window Programming 1. Sample Code:
This. listbox1.Backcolor= System. Drawing. color. fromargb (221,221,221 );
Correspondingly, forecolor is available when backcolor is available. This forecolor specifies the font color, for example:
This. listbox1.Forecolor= System. Drawing. color. fromargb (80, 80, 80 );
Write the above three sentences into the program and you will get a list box like this:

 

Listview List View Control

The listview control has five styles:Largeicon, smallicon, list, detial, and title.

The widget style is specified by the member attribute view in the listview class., View is an enumeration type. The default style of the listview control is largeicon.

Let's take a look.Largeicon Style.
Largeicon is translated as a big icon in English. You can see that listview can display the icon. In fact, you can directly add the item text. If the image is not associated.

For example:

Listview1.items. Add ("1111 ");

Listview1.items. Add ("2222 ");

In this way, the List View control only displays text. To display images, you must first associate the control with the image.

The image corresponding to the largeimagelist attribute member storage item in the listview class(Smallicon style corresponds to smallimagelist). It is an imagelist class object. The role of this class is similar to that of cimagelist in C ++.

Their models are the same. I mean, add a group of images to imagelist. Items must be associated with images, which are identified by indexes. This is the same as that of MFC.

The add function in the listviewitemcollection class (items type) has the following overload: Public Virtual listviewitem add (string text, int imageindex );

The following parameter imageindex indicates the English meaning of image index. In the order of adding imagelist, the first is 1, and the second is 2.

See the following code:

// Create an imagelist
System. Windows. Forms. imagelist imagelistlarge = new system. Windows. Forms. imagelist ();
// Set the Icon size to 32*32
Imagelistlarge. imagesize = new system. Drawing. Size (32, 32 );
// Set 32-bit icon color
Imagelistlarge. colordepth = system. Windows. Forms. colordepth. depth32bit;
// Add the icon to the imagelist set
Imagelistlarge. Images. Add (new system. Drawing. Icon ("D: \ image \ kugou. ICO "));
Imagelistlarge. Images. Add (new system. Drawing. Icon ("D: \ image \ webqq. ICO "));
Imagelistlarge. Images. Add (new system. Drawing. Icon ("D: \ image \ recycle. ICO "));
Imagelistlarge. Images. Add (new system. Drawing. Icon ("D: \ image \ 432.ico "));
Imagelistlarge. Images. Add (new system. Drawing. Icon ("D: \ image \ 3000soft. ICO "));
// There are many add methods in the class corresponding to images to load different types of images. One of the parameter types is the image class.

This. listview1.largeimagelist = imagelistlarge;
// Add the item text. The following number indicates the corresponding image index.
This. listview1.items. Add ("kugou", 0 );
This. listview1.items. Add ("webqq", 1 );
This. listview1.items. Add ("recycle", 2 );
This. listview1.items. Add ("432", 3 );
This. listview1.items. Add ("3000 soft", 4 );

Imagelistlarge. images is an imagecollection object in the imagelist class. The relationship between this imagecollection class and the imagelist class is quite similar.

The relationship between the listviewitemcollection class and the listview class. These relationships are a bit round, but you only need to use them, that is, you know imagelistlarge. images. add: The image is added. Why is it not imagelistlarge. add, that is, the system thing. It is used in this way.

Obtain the selected item text

The selecteditems array in the listview class stores the selected itemsWhy is it stored in arrays, because the listview control is selected multiple by default,
(Press ctrl to select multiple), that isThe value of multiselect is true.. Therefore, an array is used to store multiple items. Each element corresponds to a selected item.

If only one attribute is selected,
Selecteditems [0] is the selected itemThat is, the storage of the first element of the array.
To disable multiple selections, set multiselect to false.
Let's take a look at the code for clicking the event processing function: Get the selected text (only one choice)
String STR = listview1.selecteditems [0]. Text;
MessageBox. Show (STR );
It was discovered later that selecteditems is not an array, but it only uses the indexer. However, the above explanation does not have any effect. I mean, it does not affect obtaining the final selected item text.
How can we determine the number of items selected by selecteditems? It is not an array and does not use selecteditems. length.
Selecteditems is the selectedlistviewitemcollection class object, while the selectedlistviewitemcollection class has
Count attribute MemberUsed to record the number of selected items.
In combination with the above, you can make a basic application of the listview control, the effect is as follows:


 

 Smallicon StyleThe item text of the smallicon style is displayed on the right of the icon, and the icon set corresponding to the smallicon style is

Smallimagelist

Details Style
The details style is the same as the "Report" style in the MFC list control and has columns.
Add columns first
// Set the style to details.
This. listview1.view = system. Windows. Forms. View. details;
// Set instances and add column items
Listview1.columns. Add ("Name of a song", 150 );
Listview1.columns. Add ("artist", 80, system. Windows. Forms. horizontalalignment. center );
Listview1.columns. Add ("size", 50, system. Windows. Forms. horizontalalignment. center );
Listview1.columns. Add ("type",-2, system. Windows. Forms. horizontalalignment. center );
Columns is an attribute member corresponding to the column information. It is a columnheadercollection class object. The two overload add functions in the columnheadercollection class are used as follows:
Add (string, int32)
Add (string, int32, horizontalalignment)
String indicates the column text, int32 indicates the column width in the second parameter table,-2 indicates the adaptive width, and is automatically adjusted based on the text length,
Horizontalalignment is an enumeration type.It specifies how the column text is arranged, center, left ),
Or right-aligned (right ).
In the first column, the item text cannot be arranged and is always left aligned.

 

Add an item and set the subitem content.

After setting the column information, let's add the actual content.
// Create an item, that is, a row
System. Windows. Forms. listviewitem Item1 = new system. Windows. Forms. listviewitem ("days passed together ");
// Set subitem content
Item1.subitems. Add("Andy Lau ");
Item1.subitems. Add ("4.5 m ");
Item1.subitems. Add ("Classic old songs ");
Listview1.items. Add (Item1); // Add the item to the listview Control
// Create another
System. Windows. Forms. listviewitem item2 = new system. Windows. Forms. listviewitem ("just like you ");
Item2.subitems. Add ("Chen baiqiang ");
Item2.subitems. Add ("3.5 m ");
Item2.subitems. Add ("Classic old songs ");
// The third
System. Windows. Forms. listviewitem item3 = new system. Windows. Forms. listviewitem ("11 ");
Item3.subitems. Add ("Qiu Yongchuan ");
Item3.subitems. Add ("4.6 m ");
Item3.subitems. Add ("sentimental fashion ");
// You can add multiple addrange arrays of the listviewitem type as parameters.
Listview1.items. addrange (new system. Windows. Forms. listviewitem [] {item2, item3 });

// Allow the entire row to be selected
Listview1.fullrowselect = true;
// Display the gridlines.
Listview1.gridlines = true;

Obtain the selected text content and the content of each subitem.
The method for obtaining the selected item text is the same as the largeicon style, for example, listview1.selecteditems [0]. text, which is the text content of the selected item,
That is, get the text content of the first column of a row, and if you want to get the content of the subitem, that is, the content of a row of 2, 3... and other columns.
That is, listview1.selecteditems [0]. subitems [0]. Text is the first column of the selected item.
In fact, the content of selecteditems [0]. subitems [0]. text is the same as that of selecteditem [0]. Text. Obtain the second column from 1.
This is similar to the two-dimensional array of C ++. A, a [0] And & A [0] [0] are both the first addresses of arrays.
Example: (click the code in the event with one button). Only one row is selected.
Private void button#click (Object sender, eventargs E)
{
String STR = "song :";
STR + = listview1.selecteditems [0]. text;
// Subitem text
STR + = "\ n SINGER :";
STR + = listview1.selecteditems [0]. subitems [1]. text;
STR + = "\ n size :";
STR + = listview1.selecteditems [0]. subitems [2]. text;
STR + = "\ n type :";
STR + = listview1.selecteditems [0]. subitems [3]. text;

MessageBox. Show (STR );
}
The running effect is as follows:

Remember to select an item. Otherwise, an error occurs when you access selecteditems [0 ].

Related Article

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.