Winform form (2) -- controls and winform controls

Source: Internet
Author: User

Winform form (2) -- controls and winform controls

I,Form event

Each form has an event.Code that is executed after the form is loaded

Location: 1) Right-click Properties → events → load double-click to enter

2) double-click any position in the form to enter

Delete event: Delete the events on the event page, and then delete the events in the background code.

Namespace WindowsFormsApplication2 {public partial class Form1: Form {public Form1 () // constructor {InitializeComponent ();} private void Form1_Load (object sender, EventArgs e) // sender (event source): Who triggers the event, who is Form1, and e (event data) {// write event}Event

 

Find the control in the toolbox and double-click it to add it. You can also drag it in.

※Each tool, control, and form has a name, which is equivalent to an id used to identify the name of the object. The name value cannot be repeated.

Ii. Controls

1,Label -- text display tool

Text: the Text to be displayed-attributes

Label Value assignment:

Private void Form1_Load (object sender, EventArgs e) {label1.Text = "Ah hahaha"; MessageBox. Show (label1.Text );}Label

 

2. TextBox -- text box

TextBox Value assignment:

Private void Form1_Load (object sender, EventArgs e) {textBox1.Text = "Enter the user name"; MessageBox. Show (textBox1.Text );}TexBox

 

3. RichTextBox -- text field

You can adjust the size at will.

Some attributes:

Dock: defines the border of the control to be bound to the container.

Multiline: controls whether the text of the editing control can span multiple lines.

ReadOnly: controls whether to change the text in the editing control. The default value is False. If it is set to True, the value is read-only.

Enabled: indicates whether the control is Enabled.

Visible: Determine whether the control is Visible or hidden.

※The usage is the same as that of TextBox.

 

4. Button -- Button

Text: Modify the text displayed by the button

Name: id

FlatStyle: determines the display of the control when you move the mouse over the control and click

※Click event: Double-click a button to display certain time features.

 

5. radioButton -- Single-choice button

Text: text

Checked: whether to select

Multiple Single-choice buttons are mutually exclusive. winform does not have the group attribute, So it depends on their parent-level containers (only the upper-level containers). If the same parent-level container is the same group

If you want to group, put the buttons in the same group under the same container and use Panel

Select a value:

Each tool is a class. For example, radiobutton is a radiobutton class.

Each form is inherited from the form class. All tools also inherit from one parent class, that is, Control.

Private void button2_Click (object sender, EventArgs e) {// from all tools (objects) in form1 foreach (Control ctr in panel1.Controls) // all the tools under the container are a set {// determine whether the ctr of the object belongs to the subclass radiobutton if (ctr is RadioButton) {// if yes, the device type is RadioButton rd = ctr as RadioButton; if (rd. checked) {MessageBox. show (rd. text );}}}}Radiobutton -- Obtain the selected value

Set a selected item:

Private void button3_Click (object sender, EventArgs e) {// traverses the foreach (Control ctr in panel1.Controls) tool in form1 {// determines whether it is a subclass radiobutton if (ctr is RadioButton) {// if the conversion is RadioButton rd = ctr as RadioButton; if (rd. text = "female") {rd. checked = true ;}}}}Radiobutton -- set selected

※Use the code to add a tool to the container:

Private void Form1_Load (object sender, EventArgs e) {// create the tool's object RadioButton radiobutton4 = new RadioButton (); // assign the value radiobutton4.Text = "ha "; // There is a parent class in the brackets, and the rdsx principle is panel1.Controls. add (radiobutton4 );}Code adding Tool

 

6. checkbox -- check box Group

Checked attribute: whether to select

Tag attribute: You can store custom numbers.

Select a value:

Private void button4_Click (object sender, EventArgs e) {foreach (Control ctr in panel2.Controls) {if (ctr is CheckBox) {CheckBox ck = ctr as CheckBox; if (ck. checked) {MessageBox. show (ck. text );}}}}Checkbox -- get the selected value

Read access customization:MessageBox. Show (ck. Tag. ToString ());

Set a selected item:

Private void button5_Click (object sender, EventArgs e) {foreach (Control ctr in panel2.Controls) {if (ctr is CheckBox) {CheckBox ck = ctr as CheckBox; if (ck. text = "Han") {ck. checked = true ;}}}}Checkbox -- set the selected

Select All:

Private void checkBox3_CheckedChanged (object sender, EventArgs e) {foreach (Control ctr in panel2.Controls) {if (ctr is CheckBox) {CheckBox ck = ctr as CheckBox; ck. checked = checkBox3.Checked ;}}}Select All or all

 

7. listbox -- list box

Items: the value in the list box is a set that is generally added by reading data from the database.

SelectionMode: indicates whether the list box will be selected individually.

Add data from the database:

First, establish a database connection (three classes, the class creation program is omitted), and then use the following program to add;

Private void formationload (object sender, EventArgs e) {NationDA da = new NationDA (); // specify the data source listBox1.DataSource = da for listbox. select (); // specify the displayed value listBox1.DisplayMember = "Name"; // specify the background value listBox1.ValueMember = "Code ";}Add data from database

Take the value of multiple selected values:

Private void button6_Click (object sender, EventArgs e) {foreach (Nation data in listBox1.SelectedItems) // SelectedItems obtains the set of currently selected items {MessageBox. Show (data. Code );}}Obtain the value of multiple selected items

Take the selected value:

MessageBox. Show (listNation. SelectedValue. ToString ())

Select an item:(The object is obtained)

Nation data = listbox1.SelectedItem as Nation; MessageBox. Show (data. Code );

Set a selected item:

Private void button7_Click (object sender, EventArgs e) {// Method 1: // listBox1.SelectedIndex = 1; // set the index of the currently selected item starting from 0 // Method 2: listBox1.SelectedValue = "n005"; // set the value of the specified member attribute}Set two methods for selecting an item

 

8. combobox -- drop-down list

All usage is the same as listbox:

Private void formationload (object sender, EventArgs e) {NationDA da = new NationDA (); // select Nation data = new Nation (); data. code = "qxz"; data. name = "select"; List <Nation> list = da. select (); list. add (data); comboBox1.DataSource = list; comboBox1.DisplayMember = "Name"; comboBox1.ValueMember = "Code ";}Add database information and add another item.

 

9. DataTimePicker-date Selection Control
Select Settings:

Private void button8_Click (object sender, EventArgs e) {// define a time string sj = "2016-1-1 1-1"; // Method 1: // dateTimePicker1.Value = Convert. toDateTime (sj); // Method 2: dateTimePicker1.Text = sj ;}Set Time 2 Method

Select a value:

Private void button7_Click (object sender, EventArgs e) {MessageBox. Show (dateTimePicker1.Value. ToString ("yyyy MM dd HH: mm: ss: ms "));}Select time

 

10. PictureBox

Appearance. You can select the Image path for the Image.

Behavior. SizeMode allows you to set the image size layout mode.

 

11. Imagelist-image set

ImageList1.Images [n];

 

12. notifyicon-Tray Icon Tool
Text: The Text displayed with the mouse
Icon: tray Icon
Visible: Visible or hidden
ContextMenuStrip: shortcut menu associated with the icon

 

13. MenuStrip-menu Tool
Set the hotkey: enter (& F) during editing)
Set Shortcut Keys: Select menu items -- Right-click Properties -- ShortCutKeys -- set shortcut keys
Set the separation line: Enter the minus sign (-) when entering (-)

 

14. ContextMenuStrip-shortcut menu

 

15. ToolTip -- move the mouse into the prompt box

ToolTipTitle: the title of the prompt

Error: Error icon

Info: information icon

None: not a standard icon

Warning: Warning Icon

In miscellaneous of other controls, select

 

16. ProgressBar -- progress bar
Value: to set the progress of the progress bar.

MarqueeAnimationSpeed: subtitle animation speed in milliseconds

Maximum: Maximum used range

 

17. Timer-clock Tool

Enabled: unavailable
Interval: Interval
Tick event: Events EXECUTED AT INTERVALS

※Tick events and progress bars are used in combination.

Private void timerjavastick (object sender, EventArgs e) {if (progressBar1.Value <100) {progressBar1.Value = progressBar1.Value + 10;} label1.Text = DateTime. now. toString ("HH: mm: ss ");}Union

 

※In the preceding settings and the selected values, the Click Event of the Button is used!

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.