Simple implementation of C # form program to judge whether leap year _c# tutorial

Source: Internet
Author: User

Take advantage of VS2010 's C #. NET can be very simple to create a form. At the same time it is easy to layout for this form, add events, the following with a simple form program to determine whether a leap year to illustrate this problem. Plus a Shutdown event that pops up a dialog before the user closes the form asking the user whether to close the form? The following figure:


The specific approach is as follows.

1. After opening VS2010, file-> new-> project-> select->visual C #->windows->windows form application in other languages

The name of this project is isleapyear at the bottom, then the point is determined.


2, after, double-click Form1.cs, enter into the Form1 form design mode. Right-click Form1.cs to view the code, you can enter the Form1.cs background editing mode.


This Form1 form can be adjusted like a picture, and in the right-hand toolbox, drag the component to the form, Groupbox, TextBox, Label, Button. Placed in the position of the following figure.


If you cannot find this toolbox, click on the icon on the menu bar to add the following figure first.

The toolbox may remind you that there are no tools in a few seconds of loading, please wait patiently.


We drag the Toolbox's components into the form, and we may not be able to drag them to an exact position once in a while, especially some of the gang.

You can use the various dimensions and alignment tools on the toolbar, in addition to the horizontal, vertical center alignment on the right side, after selecting multiple components, horizontal and vertical spacing is the same tool.

You can also select two components through the CTRL key, then use the left to make the height, width of the same tool, the top, the bottom of its tools, the end of the use of the mouse, do not write any code, make a more beautiful form.


3. After the layout is finished, we begin to program the form.

Click on any component first, will display the property window on the right, if not display the Property window, click on the top of the property icon.

The representation of the program action component is the use of name in the attribute, such as the following figure, after the name of the update button1 is BT1, all background programming files are automatically updated as if they were r files in Android.


In each property window, the text of the GroupBox, the text of the form, and the text of the label are changed to look like this. The textalign of the textbox is changed to center center.


At the same time, in order for our form to not be resized and maximized by the user,

After double-clicking Form1, also change its formborderstyle to FixedDialog, and change MaximizeBox from True to False


Double-click any component to add the most basic event to this component in the background. For example, double-clicking Button1 can add a Button1 handler function in the background.


Click Form1 at the same time, select the Event button, double-click the event, you can add the corresponding event.

The following figure, which is the Form1 form, adds the form Close event formclosing.


Then modify the Form1.cs code as follows,

In the Button1 Click event, MessageBox.Show is able to eject an alert box that is equivalent to the JavaScript alert.

A warning box with a Yes or no button is displayed in the Form1 shutdown event. Equivalent to the confirm of JavaScript.

The regular expression that is used after the click event of the Button1

is used in C # to determine whether input is pure number, container class (Click to open link) and "JS uses regular expression to verify input content as Web site" has been said. The main decision is whether the user entered a number, not the other.

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms; The using system.text.regularexpressions;//uses the regular expression namespace Isleapyear {public partial class Form1:form {public Fo
  RM1 () {InitializeComponent (); //form1 Shutdown Event private void form1_formclosing (object sender, FormClosingEventArgs e) {if (MessageBox.Show) ("Shut down process") Order? "," title bar, messageboxbuttons.yesno) = = Dialogresult.yes) {e.cancel = false;//OK close} else {e.cancel = Tru e;//does not close}//button1 the Click event private void Bt1_click (object sender, EventArgs e) {if (New Regex ("^[0-9]+$"). IsMatch (TextBox1.Text) | | New Regex ("^[-][0-9]+$"). IsMatch (TextBox1.Text))//Use a regular expression to determine whether a number {int year = int is entered.
     Parse (TextBox1.Text)//convert textBox1 text to cosmetic if (year% 4 = 0 && Year%!= 0) | |-year% 400 = 0) { MessageBox.Show ("Enter a leap year!")
    ");
} else {     MessageBox.Show ("Input is not a leap year!")
    "); } else {MessageBox.Show ("input is not a normal year!") Please re-enter!
   ");


 } 
  }
 }
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.