Basic winform control-example learning, winform Control

Source: Internet
Author: User

Basic winform control-example learning, winform Control

1. ImplementationIntegerCalculator

ComboBox control:

Items properties: add Items in the set.

this.comoper.Items.AddRange(new object[] {            "+",            "-",            "x",            "/"});

TextBox:

There are restrictions on TextBox input text:

1) only numeric values (integers and decimals) can be entered );

2) the decimal point cannot start with, and only one decimal point can be entered;

3) input that does not meet the requirements is not accepted.

Implementation Method

Use the KeyPress event of TextBox: The Key is pressed when the control has focus.

The KeyChar attribute gets or sets the character corresponding to the key pressed.

A KeyPress event cannot be triggered by a non-character key, but a non-character key can trigger a KeyDown or KeyUp event.

Use the KeyChar attribute to sample the keys at run time, and use or modify a subset of common keys.

To process Keyboard Events only at the form level and not allow other controls to receive Keyboard Events, set the KeyPressEventArgs. Handled attribute in the form's KeyPress event handling method to true.

// Process keyboard-based Keyboard Events. The current time focus is on the TextBox Control-level private void textnum1_KeyPress (object sender, KeyPressEventArgs e) {// obtain the control TextBox currtextbox = sender as TextBox that triggers the event through sender; if (e. keyChar <'0' | e. keyChar> '9') {// the user does not input a number e. handled = true ;}
// Whether the user has entered the backspace key if (e. keyChar = 8) {e. handled = false;} // enter the decimal point for the user to determine whether the decimal point is if (e. keyChar = 46) {// enter only one decimal point to check whether the current text box has a decimal point.
// Report the index of the first occurrence of the specified Unicode character or string in this instance. If this character or string is not found in this instance, this method returns-1.
If (currtextbox. Text. IndexOf (".") =-1 ){
// The decimal point cannot be in the first place. Note that the character is single input.
// SelectionStart gets or sets the start point of the text currently entered in the text box. If (currtextbox. SelectionStart> 0) {e. Handled = false ;}}}}

using System;using System.Windows.Forms;public class Form1: Form{    public Form1()    {        // Create a TextBox control.        TextBox tb = new TextBox();        this.Controls.Add(tb);        tb.KeyPress += new KeyPressEventHandler(keypressed);    }    private void keypressed(Object o, KeyPressEventArgs e)    {        // The keypressed method uses the KeyChar property to check         // whether the ENTER key is pressed.         // If the ENTER key is pressed, the Handled property is set to true,         // to indicate the event is handled.        if (e.KeyChar == (char)Keys.Return)        {            e.Handled = true;        }    }    public static void Main()    {        Application.Run(new Form1());    }}

2. Image Viewer

ID card number correction:

Private bool CheckCardId (string id) {// check bit weight or encoding value int [] wQuan = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; string checkWei = "10X98765432"; string number17 = id. substring (0, 17); string number18 = id. substring (17); int sum = 0; for (int I = 0; I <17; I ++) {sum + = Convert. toInt32 (number17 [I]. toString () * wQuan [I]; // the char type cannot be directly converted to an integer (actually converted to the corresponding ascii). tostring must be converted first} int mod = sum % 11; string resul T = checkWei [mod]. toString (); if (number18.Equals (result, StringComparison. ordinalIgnoreCase) {return false;} else {return true;} private void btnView_Click (object sender, EventArgs e) {// ID card may contain 15 or 18 digits // ID card number of 15 digits: province (20) City (2) County (2) year (2) month (2) day (2) + 3 serial number (odd male doll) // 18-digit ID card: First: born years ago plus 19 second: 18th digit check digit, string id = pidid calculated from the first 17 bits. text; int age = 0; int year = 0; if (id. length = 15) {year = Convert. toInt32 (id. Substring (6, 2) + 1900;} else if (id. Length = 18) {if (! This. checkCardId (id) {MessageBox. show ("Incorrect ID card input, please check"); return;} year = Convert. toInt32 (id. substring (6, 4);} else {MessageBox. show ("Incorrect ID card length, please enter it again"); return;} age = DateTime. now. year-year; if (age> = 18) {pic. visible = true;} else {MessageBox. show ("You are too young to go home to watch cartoons ");}}

3. Visit the website

WebBrowser class: allows you to navigate a webpage in a form.

private void button1_Click(object sender, EventArgs e)        {            webBrowser1.Navigate(button1.Text);        }

4. add or delete the query in the tree control

TreeView:

Corresponding Method:

Private void button#click (object sender, EventArgs e) {treeView1.HideSelection = false; MessageBox. show (treeView1.SelectedNode. text);} private void button2_Click (object sender, EventArgs e) {// MessageBox. show ("number of root nodes {0}", treeView1.Nodes. count. toString (); // treeView1.Nodes. add ("add"); Add a root node // treeView1.Nodes [0]. nodes [0]. nodes. add ("Wang Xiaodong"); // Add to level 1 node if (treeView1.SelectedNode! = Null) {treeView1.SelectedNode. nodes. add (textBox1.Text);} if (treeView1.SelectedNode = null) {treeView1.Nodes. add (textBox1.Text) ;}} private void TreeView_Load (object sender, EventArgs e) {treeView1.ExpandAll (); // expand the node treeView1.Nodes [0]. imageIndex = 3;} private void button4_Click (object sender, EventArgs e) {if (treeView1.SelectedNode! = Null) {// treeView1.SelectedNode. expandAll (); // expand all the nodes under the current node treeView1.SelectedNode. expand (); // Expand all subnodes under the current node} private void button3_Click (object sender, EventArgs e) {if (treeView1.SelectedNode! = Null) {treeView1.SelectedNode. collapse (); // close all nodes under the current node} private void button5_Click (object sender, EventArgs e) {treeView1.SelectedNode. remove ();} private void button6_Click (object sender, EventArgs e) {string str = "selected language"; foreach (TreeNode tn in treeView1.Nodes [0]. nodes [0]. nodes) {if (tn. checked = true) {str = str + tn. text ;}} MessageBox. show (str);} private void treeviewappsaftercheck (object sender, TreeViewEventArgs e) {foreach (TreeNode tn in e. node. nodes) {tn. checked = e. node. checked ;}}
View Code

 

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.