From the past to the future, from Visual and Basic to NET. (12 tricks from VB to VB.NET)

zhaozj2021-02-17  70

From the past to the future, from Visual and Basic to Net

Small god 2001.08.08

The initial VB was called "Thunder" plan as a MS. It was started in 1990. At that time, Gates also personally wrote a written written in the magazine, telling people "Gates on Basic's Future; 10 years later, VB) In development and change, it is now called "vb.net". Gates still writes in the magazine for VB, telling people "How VB Will Program The Future", I think Gates I have prevailing the Basic language. Not only him, there are many people who are also preferred in Bill and VB. In fact, half of this world is using this language, and in China is almost 80% of the program developers in use, but there is also a fact that VB's actual usage is declining, because of proficiency The programmers of multiple languages ​​are casually switched out in C, C , Java, and Script language. The mature programmers are no longer arguing the programming language, but according to the application itself, it needs to be implemented according to the industry standard. No matter what language you use, the key is whether your implementation complies with the standard or agreement of this industry.

Java brings us a cup of hot drink five years ago, the coffee inside made the entire network is exciting; if the VB in the transformation after five years, if the VB in the transformation is like a cup of ore, it is a bit of malnutrition. What about health? I hope it is still simple and visualized. Maybe Bill wants to tell you: Basic language is not only Visaul's and Basic, but now is NET.

New VB. What is NET? Will it be better than I am using VB6, let's take a look, and quickly master the 12 techniques.

1. Object-oriented changes, maybe it wants to be more objects, because it is not, just say that you are object-oriented. VB starts from 4.0 that it is object-oriented, and there is a somewhat suspicious .CLS file, although we use this .cls file with our DB or business layer components, there is no doubt and worry If you are used to you, you will feel that a copy is much better than C , really do you think?

Past VB

============================================================================================================================================================================================================= ====

Private M_VBProperty As Variant

Public property Let VBProperty (Byval Strdata As Variant)

m_vbproperty = strdata

End Property

Public property set vbproperty (Byval Strdata As Variant)

m_vbproperty = strdata

End Property

Public property get vbproperty () As Variant

IF isobject (m_vbproperty) THEN

SET VBPROPERTY = M_VBPROPERTY

Else

Vbproperty = m_vbproperty

END IF

End Property is currently VB. Net

============================================================================================================================================================================================================= ====

Private M_VBNETPROPERTY AS STRING

Public property VBNetProperty As String

Get

VBNetProperty = m_vbnetproperty

END GET

Set

m_vbnetproperty = VBNetProperty

End set

End Property

New adding readonly and writeonly keywords to implement function that you define read-only or only write, let and set are no longer supported. Previous set objb = Obja now produces a syntax error, you want to write in VB.NET: objb = obja

2. Reading files, the file access in VB is different, 3.0, 4.0, 5.0 are changed, and the FileSystemObject appearance allows us to have a seemingly constant object to remember, but #, the $ symbol is always letting I think of a black hole feeling. Dangerous and dark, fooled these very fast. The following code completes the open WIN.INI file from the system directory and display it into a Text control.

Past VB

============================================================================================================================================================================================================= ====

Private submmand1_click ()

DIM FNUM AS INTEGER

DIM S As String

DIM FNAME AS STRING

Dim WinPath As String

ON Error Goto ErrreadTextFile

Fnum = freefile

'get the windows folder name

WinPath = Environ $ ("SystemRoot")

IF WinPath = "" ""

Msgbox "Unable to Retrieve The Windows Path.", _

Vbinformation, "ERROR READING Windows Path"

EXIT SUB

END IF

'Create a File Name

FNAME = WinPath & "/win.ini"

'EnSure the file exists

IF DIR $ (FNAME) <> "" "" "

'Open the file

Open fname for binary as #fnum

IF err.number = 0 THENS = Space $ (Lof (fnum))

'Read the file

Get #fnum, 1, s

Close #fnum

Text1.text = s

Else

Text1 = "Unable to read the file" & _

FNAME & "."

END IF

Else

TEXT1 = "The file" & fname "

END IF

EXITREADTEXTFILE:

EXIT SUB

ErrReadTextFile:

MsgBox Err.Number & ":" & Err.Description

EXIT SUB

End suend Sub

Now VB. Net

============================================================================================================================================================================================================= ====

Private sub btnreadtextfile_click (byval sender _

As system.object, byval e as system.eventargs _

Handles btnreadtextfile.click

Dim WinPath As String

DIM S As String

DIM FNAME AS STRING

DIM SR AS StreamReader

DIM EX AS EXCEPTION

'get the windows folder name

WinPath = system.environment. _

Getenvironmentvariable ("SystemRoot")

IF WinPath = "" ""

MessageBox.show ("Unable to Retrieve THE" & _

"Windows Path.", "Error

Reading

Windows path ", _

MessageboxButton.ok, MessageBoxicon.information)

Return

END IF

FNAME = WinPath & "/win.ini"

IF file.exists (fname) THEN

Try

SR = file.opentext (fname)

S = Sr.ReadToend

Sr.close ()

TextBox1.text = s

Catch EX

Messagebox.show (ex.Message)

Return

END TRY

Else

MessageBox.show ("The File" & FName & _

"does not exist.")

Return

END IF

End Sub

New messagebox.show is better than MessageBox, and Sr.Close () is more objects than Close #filenum.

3. Multiple choices of ListBox. How do I know that those options in a multi-selected listbox are selected. Take a look at the implementation below, although I don't like the Redim of VB, but sometimes I can really solve the problem soon. In VB.NET, you can use the data you need directly. Past VB

============================================================================================================================================================================================================= ====

Private subdgetListSelections_Click ()

DIM I as integer

DIM SARR () AS String

DIM SELCOUNT AS INTEGER

For i = 0 to list1.listcount - 1

If List1.Selected (i) THEN

Redim Preserve Sarr (SELCOUNT) AS STRING

SARR (SELCOUNT) = List1.list (i)

SELCOUNT = SELCOUNT 1

END IF

NEXT

Call showStringArray (SARR)

End Sub

Private Sub ShowstringArray (Arr () AS String)

DIM S As String

DIM I as integer

DIM Arrcount AS Integer

ON Error ResMe next

Arrcount = ubound (arr) 1

ON Error Goto 0

IF arrcount> 0 THEN

For i = 0 TO Arrcount - 1

S = S & Arr (I) & VBCRLF

NEXT

Msgbox S

END IF

End Sub

Now VB.NET

============================================================================================================================================================================================================= ====

Private sub btngetListSelections_Click (Byval Sender_

As system.object, byval e as system.eventargs _

Handles btngetListSelections.click

Dim Sarr (listbox1.selecteditedItems.count - 1) _

As string

DIM ANITEM AS STRING

DIM I as integer

For Each AnItem in listbox1.selecteditems

SARR (i) = AnItem

i = 1

NEXT

End Sub

Private Sub ShowstringArray (Byval Arr () AS STRING

Messagebox.show (Join (Arr, _system.environment.newline))

End Sub

END CLASS

4. How to start another program in the VB program, wait and know when it exits. No VB. Before NET, this problem is a professional practice. Like many problems, VB does not solve it. It is necessary to call some rare known functions. MS collects such problems, called MS Knowledge Base, should now be Called MSDN Knowledge Base, maybe you have guessed that you have to use the shell function, the past VB is waiting to be taken like our investment stocks on how to wait for the launch program and get the exit. The past VB =============================================== ======= You can get help and code in the following URL. http://support.microsoft.com/support/kb/articles/q129/7/96.asp

Now VB.Net =============================================== ========= Private Sub btnLaunch_Click (ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles btnLaunch.Click Dim sysPath as String sysPath = System.Environment.GetFolderPath _ (Environment.SpecialFolder. System) shell (syspath & "/notepad.exe ", _ appwinstyle.normalfocus, true) messageBox.show (" You Just Closed Notepad ", _" Notepad Closed ") End Sub's new VB.NET has become synchronized. Two programs are in a process space.

转载请注明原文地址:https://www.9cbs.com/read-31052.html

New Post(0)