Approaching VB.net (iv.) about data types and examples

Source: Internet
Author: User
Tags exit integer modify
Example | data | data type Go near VB. Net (iv) about data types and examples
The most talked about in the previous chapters is the variant (VB6) to Object (vb.net) conversion, which is called the universal data type. In addition, a 32-bit long (VB6) is replaced by an integer (vb.net), which stores a 64-bit signed integer in vb.net. The short store 16 digits replaces the position of the VB6 integer. And what we're going to talk about here is the decimal data type.

At the beginning of a beginner's VB6, it's possible that all of us have done the same introductory program "Calculator", and you may see that when you compute a larger number, the results appear exponentially, such as: xxxxe+yyyy. This often makes the person using the calculator inexplicably, and some people don't even understand what it means. Some experience may use format to make him appear with the actual number, but the problem is, the FORMATR is fixed, if you are a 5 decimal, then only one decimal, he will also appear in the back of 4 zeros. Of course you'll soon be using character processing to clear the back 0, which is easy for you. But you will find that his calculations are limited to a certain bit, followed by all zeros, regardless of what he actually is, is completely inaccurate, or even unreliable. The currency type, commonly referred to as the currency type, is provided in VB6. Provide accurate fixed-point operation, but he only has four decimal digits, that is to say, even if you actually need eight digits, he can only have four digits. And when you're out of a limited range, you have to catch this error and switch it safely to floating-point operations.

In vb.net, you are given a decimal type of data that replaces the original Currency,decimal store with a signed 96-bit integer, and a 28-bit decimal number, and he dynamically dominates the decimal digits, and when your number behaves as a large integer, He will get the maximum accuracy by reducing the number of decimal places based on the size of the data. In other words, we can make a more accurate calculator than the calculator with Windows, think so easy, should be able to firm you continue to learn VB. NET's confidence.

Create a new project, add a welcome form

Make sure you get a cool picture of a welcome background picture.
Add Timer1 Control

In the Project Management window right click on the project name, click the last attribute, set startup object to welcome

Add TextBox1 to Form1, TextBox2 down, button1 between the two, text= "+". Button2 is next to "=". Button3 as "Exit"

The code for Welcome is as follows:

Option Strict off ' open option Strict after adjusting the transparent number will have trouble, in short, this is no way

Imports System.Drawing

Imports system.winforms

Imports System.ComponentModel

Public Class Welcome

Inherits System.WinForms.Form



Public Sub New ()

MyBase.New ()

Welcome = Me

' This is required by the Win Form Designer.

InitializeComponent ()

Me.borderstyle = WinForms.FormBorderStyle.None ' Don't title bar

Me.height = Me.BackgroundImage.Height ' Set form height equals picture height

Me.Width = Me.BackgroundImage.Width ' Set form width equals picture width

Timer1. Enabled = True ' Start timer

Timer1. Interval = 1:me.opacity = 0 ' make form fully transparent

' Todo:add any initialization on the InitializeComponent () call

End Sub

' Form overrides dispose to clean up the component list.

Public Overrides Sub Dispose ()

Mybase.dispose ()

Components. Dispose ()

End Sub



#Region "Windows Form Designer generated code"

' Required by the Windows Form Designer

Private Components as System.ComponentModel.Container



Private WithEvents Timer1 as System.WinForms.Timer



Dim WithEvents Welcome as System.WinForms.Form



' Note:the following procedure is required by the Windows Form Designer

' It can be modified using the ' Windows Form Designer.

' Do not modify it using the ' Code Editor.

Private Sub InitializeComponent ()

Dim resources as System.Resources.ResourceManager = New System.Resources.ResourceManager (GetType (Welcome))



Me.components = New System.ComponentModel.Container ()

Me.timer1 = New System.WinForms.Timer (components)



' @design Me.trayheight = 90

' @design Me.traylargeicon = False

' @design Me.trayautoarrange = True

' @design timer1.setlocation (New System.Drawing.Point (7, 7))

Me.Text = "Welcome"

Me.autoscalebasesize = New System.Drawing.Size (6, 14)

Me.backgroundimage = CType (resources. GetObject ("$this. BackgroundImage "), System.Drawing.Image)



End Sub



#End Region

Protected Sub Timer1_Tick (ByVal sender as Object, ByVal e as System.EventArgs)

If me.opacity = 1 Then ' is displayed, execute the following code

Dim FM as Form1 = New Form1 () ' This Form1 creates an instance (instance), forms an object (objects), allocates a bit of memory.

Fm. Visible = True ' Displays the main form

Me.dispose () ' You think of him as VB6 's unload me.

Else

Me.opacity = me.opacity + 0.01 ' slowly show form

End If

End Sub

End Class

Guess what the form will produce ...

Add the following code in Form1

Imports System.ComponentModel

Imports System.Drawing

Imports system.winforms





Public Class Form1

Inherits System.WinForms.Form



Public Sub New ()

MyBase.New



Form1 = Me



' This is required by the Win Form Designer.

InitializeComponent () Initializes a component when the new procedure is executed (new form)

Me.borderstyle = Formborderstyle.none

Button2. Visible = False ' Hide Button2

TextBox2. Visible = False ' Hide TextBox2







' Todo:add any initialization on the InitializeComponent () call

End Sub



' Form overrides dispose to clean up the component list.

Overrides public Sub Dispose ()

Mybase.dispose

Components. Dispose

End Sub



#Region "Windows Form Designer generated code"



' Required by the Windows Form Designer

Private Components as System.ComponentModel.Container

Private WithEvents Button3 as System.WinForms.Button

Private WithEvents Button2 as System.WinForms.Button







Private WithEvents Button1 as System.WinForms.Button

Private WithEvents TextBox2 as System.WinForms.TextBox

Private WithEvents TextBox1 as System.WinForms.TextBox



Dim WithEvents Form1 as System.WinForms.Form



' Note:the following procedure is required by the Windows Form Designer

' It can be modified using the ' Windows Form Designer.

' Do not modify it using the ' Code Editor.

Private Sub InitializeComponent ()

Me.components = New System.ComponentModel.Container ()

Me.button1 = New System.WinForms.Button ()

Me.textBox2 = New System.WinForms.TextBox ()

Me.button3 = New System.WinForms.Button ()

Me.textBox1 = New System.WinForms.TextBox ()

Me.button2 = New System.WinForms.Button ()



' @design me.trayheight = 0

' @design Me.traylargeicon = False

' @design Me.trayautoarrange = True

Button1.location = New System.Drawing.Point (176, 40)

Button1.size = New System.Drawing.Size (24, 24)

Button1.tabindex = 2

Button1.font = New System.Drawing.Font ("XXFarEastFont-Arial", 12!, System.Drawing.FontStyle.Bold)

Button1.Text = "+"



Textbox2.location = New System.Drawing.Point (160, 72)

Textbox2.tabindex = 1

Textbox2.size = New System.Drawing.Size (256, 21)



Button3.location = New System.Drawing.Point (24, 104)

Button3.size = New System.Drawing.Size (64, 24)

Button3.tabindex = 4

Button3.text = "Exit"



Textbox1.location = New System.Drawing.Point (16, 8)

Textbox1.tabindex = 0

Textbox1.size = New System.Drawing.Size (200, 21)



Button2.location = New System.Drawing.Point (360, 104)

Button2.size = New System.Drawing.Size (32, 24)

Button2.tabindex = 3

Button2.text = "="

Me.Text = "Form1"

Me.autoscalebasesize = New System.Drawing.Size (6, 14)

Me.backcolor = System.Drawing.Color.YellowGreen

Me.clientsize = New System.Drawing.Size (456, 141)



ME.CONTROLS.ADD (Button3)

ME.CONTROLS.ADD (Button2)

ME.CONTROLS.ADD (Button1)

ME.CONTROLS.ADD (TextBox2)

ME.CONTROLS.ADD (TextBox1)

End Sub



#End Region



Protected Sub button3_click (ByVal sender as Object, ByVal e as System.EventArgs)

End

End Sub



Protected Sub button1_click (ByVal sender as Object, ByVal e as System.EventArgs)

If TextBox1.Text = "" Then

System. WinForms.MessageBox.Show ("Please enter the correct number", "VB.") NET Calculator ", MessageBox. OK Bitor MessageBox. Iconasterisk)

Exit Sub

End If

TextBox2. Visible = True ' Displays TextBox2

TextBox2. Text = "' Empty TextBox2

TextBox2. Focus () ' Let TextBox2 get the spotlight

Button2. Visible = True ' Displays button2



End Sub



Protected Sub button2_click (ByVal sender as Object, ByVal e as System.EventArgs)

Dim Valx, Valy, Valz as decimal ' declares three decimal variables



Try

Valx = TextBox1. Text.todecimal ' takes value a

Valy = TextBox2. Text.todecimal ' takes a value of two

Catch

System. WinForms.MessageBox.Show ("Please enter the correct number", "VB.") NET Calculator ", MessageBox. OK Bitor MessageBox. Iconasterisk)

Exit Sub

End Try

Valy = valx + valy ' calculation result

TextBox1. Text = Valy. ToString ' shows results

TextBox2. Visible = False ' Hide TextBox2

Button2. Visible = False ' Hide Button2

End Sub



Private Sub Form1_mousedown (ByVal eventsender as Object, ByVal e as System.WinForms.MouseEventArgs)

' The Drop-down list in the form in the MouseDown event is not found, write one yourself, plus (ByVal eventsender as Object, ByVal e as System.WinForms.MouseEventArgs)

Me.capture () = False ' release mouse capture, equivalent to API ReleaseCapture ()

Me.sendmessage (&ha1s, 2, 0) ' Well, this is which SendMessage API, but the first handle parameter is no longer used.

End Sub



End Class



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.