c#-winform-Basic properties and exercises for common controls

Source: Internet
Author: User

View → Toolbox

Basic operations: The value of the control, assignment, change value, event

1. button--button

autosize-indicates whether the control automatically resizes itself to fit the size of its contents.

False by default, when the text content is longer than its width, and if true, the control changes its width depending on the length of the text. Because the text length is not known, the common True property

Enabled-indicates whether the control is enabled.

By default true, the control is available, false, and the control is not available

visible-determines whether the control is visible or hidden.

Default true is visible, false when hidden is not visible

Events-click events

2. checkbox--check box

Checked-Indicates whether the component is selected.

False is not selected by default, True is selected

"Background Let it be selected by default:

Fill in the code in the constructor:

Exercise One: Click the button to change the check box to select the status

Double-click the button to fill in the code with the following statement:

// Button1 Click events Private void button1_click (object  sender, EventArgs e)        {            if (checkbox1.checked) // if the                CheckBox1 is selected false ;             Else true;        }                  
Click the button to select or uncheck

3. checkedlistbox-check box Group

items--the items in the list.

Point to fill in data, one behavior one data

"Assignment, value, change value

Practice:

Assign value

Import the data in the database into the check box group:

Create a new database connection class to encapsulate the information that will be presented

 Public class Nation    {        publicstringgetset;}          Public string Get Set ; }    }

Second, the new data operation class

 Public classNationdata {SqlConnection conn=NULL; SqlCommand cmd=NULL;  PublicNationdata () {conn=NewSqlConnection ("server=.; database=data0928;user=sa;pwd=123;"); CMD=Conn.        CreateCommand (); }         PublicList<nation>Select () {List<Nation> list =NewList<nation>(); Cmd.commandtext="Select *from Nation"; Conn.            Open (); SqlDataReader Dr=cmd.            ExecuteReader (); if(Dr. HasRows) { while(Dr. Read ()) {Nation n=NewNation () {Nationcode= dr[0]. ToString (), Nationname= dr[1].                    ToString ()}; List.                ADD (n); }} conn.            Close (); returnlist; }    }
New data Operation class

Third, write Click event

Private void button1_click (object  sender, EventArgs e)        {            Listnew  Nationdata (). Select ();             foreach inch list)            {                checkedListBox1.Items.Add (n.nationname);            }        }

Click to compare before and after

Take value

Iv. Create New button, set click event

Private voidButton2_Click (Objectsender, EventArgs e) {            stringEnd =""; intCount =0; foreach(ObjectOinchcheckedlistbox1.checkeditems) {if(Count >0) End+=","; End+=o.tostring (); Count++;        } MessageBox.Show (end); }
Take value

4. combobox-drop-down list

When you click Button1, the value of the database is given to ComboBox1

Private void button1_click (object  sender, EventArgs e)        {            Listnew  Nationdata (). Select ();            Combobox1.datasource= list; // Gets or sets the data source            " Nationname "; // Gets or sets the property to display        }

Pop-up options when you click Button2

Private void button2_click_1 (object  sender, EventArgs e)        {            as  Nation;            MessageBox.Show (N.nationname);        }

When setting click Button1, the default is the last one

Private void button1_click (object  sender, EventArgs e)        {            Listnew  Nationdata (). Select ();            Combobox1.datasource= list; // Gets or sets the data source            " Nationname "; // Gets or sets the properties            to display 1 ;        }

c#-winform-Basic properties and exercises for common controls

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.