VB.NET中的常用方法

一、如何使用dll库:

  dll库是动态链接库,一般是别人提供的,用来做二次开发,相当于别人把一些函数包装在dll中,已经生成可以链接文件,你只能调用,但是不能看到方法的实现。所以给你提供dll的人一般也会提供dll的库函数说明,说明中会告诉你里面的函数的返回值,参数等一些东西。

  1、要使用dll库中的方法,首先把dll复制粘贴在你的.net项目中的\bin\Debug路径下面。

  2、在项目中新建一个模块Module,然后在里面声明要用的函数。例如:我新建一个叫API的模块,然后声明函数,具体过程如下:

  

Imports System.Text
Module API

    '驱动加载函数
    Public Declare Function RDR_LoadReaderDrivers Lib "rfidlib_reader.dll" (ByVal path As String) As Integer
    Public Declare Function RDR_GetLoadedReaderDriverCount Lib "rfidlib_reader.dll" () As Integer

    '设备连接、断开函数
    Public Declare Function RDR_Open Lib "rfidlib_reader.dll" (ByVal connstr As String, ByRef hr As UIntPtr) As Integer
    Public Declare Function RDR_Close Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As Integer

    '获取错误代码
    Public Declare Function RDR_GetReaderLastReturnError Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As Integer

    '
    Public Declare Function RDR_GetTagDataReportCount Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr) As UInt32



    '以下是读取读卡器参数的函数
    Public Declare Function RDR_GetReaderInfor Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal Type As Byte, ByRef buffer As StringBuilder, ByRef nSize As UInt32) As Integer

    '以下是盘卡函数
    Public Declare Function RDR_CreateInvenParamSpecList Lib "rfidlib_reader.dll" () As UIntPtr
    Public Declare Function ISO15693_CreateInvenParam Lib "rfidlib_aip_iso15693.dll" (ByVal hInvenParamSpecList As UIntPtr, ByVal AntennaID As Byte, ByVal en_afi As Byte, ByVal afi As Byte, ByVal slot_type As Byte) As UIntPtr
    Public Declare Function RDR_TagInventory Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal AIType As Byte, ByVal AntennaCount As Byte, ByRef AntennaIDs As Byte, ByVal InvenParamSpecList As UIntPtr) As Integer 'AntennaIDs传入数组的第一个元素地址
    Public Declare Function RDR_GetTagDataReport Lib "rfidlib_reader.dll" (ByVal hr As UIntPtr, ByVal Seekpos As Byte) As UIntPtr
    Public Declare Function ISO15693_ParseTagDataReport Lib "rfidlib_aip_iso15693.dll" (ByVal hTagReport As UIntPtr, ByRef aip_id As UInt32, ByRef tag_id As UInt32, ByRef ant_id As UInt32, ByRef dsfid As Byte, ByRef uid As Byte) As Integer


    '以下是销毁盘点参数列表
    Public Declare Function DNODE_Destroy Lib "rfidlib_reader.dll" (ByVal dn As UIntPtr) As Integer

    '盘点卡的两个宏定义
    Public Const RFID_SEEK_FIRST = 1
    Public Const RFID_SEEK_NEXT = 2

    '连接卡
    Public Declare Function ISO15693_Connect Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal tagType As UInt32, ByVal address_mode As Byte, ByVal uid() As Byte, ByRef ht As UIntPtr) As Integer

    '读卡
    Public Declare Function ISO15693_ReadMultiBlocks Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal ht As UIntPtr, ByVal readSecSta As Byte, ByVal blkAddr As UInt32, ByVal numOfBlksToRead As UInt32, ByRef numOfBlksRead As UInt32, ByRef bufBlocks As Byte, ByVal nSize As UInt32, ByRef bytesBlkDatRead As UInt32) As Integer

    '写卡
    Public Declare Function ISO15693_WriteMultipleBlocks Lib "rfidlib_aip_iso15693.dll" (ByVal hr As UIntPtr, ByVal ht As UIntPtr, ByVal blkAddr As UInt32, ByVal numOfBlks As UInt32, ByVal newBlksData() As Byte, ByVal bytesToWrite As UInt32) As Integer









    Public Declare Function LSG_CmdGetReports Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal Flag As Byte, ByVal recordsToGet As Byte) As Integer
    Public Declare Function LSG_ParseSCEventData Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal slData As String, ByVal nSize As Long, ByRef dir As Byte, ByRef m_time As Byte) As Integer
    Public Declare Function LSG_CmdGetCurrentFlowOfPeople Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByRef inFlow As Long, ByRef outFlow As Long) As Integer
    Public Declare Function LSG_CmdResetFlowOfPeople Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal mFlg As Byte) As Integer
    Public Declare Function LSG_CmdReverseDirection Lib "rfidlib_LSGate.dll" (ByVal hr As Long) As Integer
    Public Declare Function LSG_CmdGetSystemTime Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByRef year As Long, ByRef month As Byte, ByRef day As Byte, ByRef hour As Byte, ByRef minute As Byte, ByRef second As Byte) As Integer
    Public Declare Function LSG_CmdSetSystemTime Lib "rfidlib_LSGate.dll" (ByVal hr As Long, ByVal year As Long, ByVal month As Byte, ByVal day As Byte, ByVal hour As Byte, ByVal minute As Byte, ByVal second As Byte) As Integer


    Public Declare Function MultiByteToWideChar Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
    Public Declare Function WideCharToMultiByte Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long
    Public Const CP_ACP = 0 ' default to ANSI code page
    Public Const CP_UTF8 = 65001 ' default to UTF-8 code page

End Module

 

声明完的函数我就可以在项目中调用了,例如:

     '设备连接串
        Dim connStr = "RDType=RD201;CommType=COM;COMName=" + CmboxcommCanBeUse.SelectedItem + ";BaudRate=38400;Frame=8E1;BusAddr=255"
        Dim connectOK As Integer = 99
        connectOK = RDR_Open(connStr, hr)

以上代码中的RDR_Open()函数就是在API模块中声明的,函数所在的位置是rfidlib_reader.dll中。

 

二、常用的一些方法:

Now()     获取当前日期时间,

Format() 格式化日期时间函数:

 Format(Now(), "yyyy-MM-dd-hh-mm-ss")

 

DataGridView用法:

GoodDataGridView.Rows.Add()为添加一行

GoodDataGridView.Rows.RemoveAt(1) 为删除第一行

GoodDataGridView(0, 0).Value = "序号" 为给某列某行的单元格设置值。其中(0,0)中前面的数字代表第几列,后面的数字代表第几行。
     GoodDataGridView.Rows.Add()
        GoodDataGridView(0, 0).Value = "序号"
        GoodDataGridView(1, 0).Value = "商品编码"
        GoodDataGridView(2, 0).Value = "商品名称"
        GoodDataGridView(3, 0).Value = "商品价格"

 

定义数组并初始化:

      '定义一组商品的属性的二进制编码,用来保存当前连接的标签的内容
        Dim goodsNumb() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim goodsName() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        Dim goodsPric() As Byte = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

 

 UTF8 编码与解码:

Imports System.Text

Module UTF8

    'UTF8编码函数
    Public Function StringToUTF8Bytes(ByVal strData As String) As Byte()

        Dim bytes() As Byte
        bytes = Encoding.UTF8.GetBytes(strData)
        Return bytes

    End Function

    'UTF8解码函数
    Public Function UTF8BytesToString(ByVal strData() As Byte) As String

        Dim retstring As String
        retstring = Encoding.UTF8.GetString(strData)
        Return retstring

    End Function

End Module

 

datagridview隔行显示不同的颜色:

datagridview1.RowsDefaultCellStyle.BackColor = Color.Bisque

datagridview1.AlternatingRowsDefaultCellStyle.BackColor =Color.Beige

 

datagridview设置标题字体样式:

设置ColumnHeaderDefaultCellStyle的Font属性
或者编程
datagridview.Columns[index].DefaultCellStyle.Font.Size=size

 

转载于:https://www.cnblogs.com/jiwangbujiu/p/5555850.html

VB.Net常用语法 一:Try………Catch………finally………end try 捕获错误 把一个可能出错的语句放在try后面,如果出错,执行catch语句, catch可以有多个,第一个catch不能捕获的错误,将被下一个catch语句 所捕获。在所有的处理结束后,执行finally语句。 在一个try………end try 过程中可以使用catch与finally两个中的一 个,或者两个一起用, 二:Protected, private, Public 封装控制 这三个关键字一般放在语句最前面,而会置于overloads与Overrides的 后面 public是工程级别外部访问 protected是本族级别以下内部访问 private是不提供非本地访问 三:ByVal sender As Object, ByVal e As System.EventArgs   这是最赏用事件传递参数。被封装于两个对象内, 如果你要传递相应的参数,要在这两个对象里面找,如当前鼠标位置为 e.X e.Y。在VB.Net中有些事件是要自已动手写的, 如窗体鼠标过程,一般加上这个参数在后面的括号内就解决了。 四:重载语法Overloads 如:class class1 public strtem as string overloads public sub subx()重载subx(A状态) strtem=”3” end sub overloads public sub subx(x as string) 重载subx(B状 态) strtem=x end sub end class 如果你如果调用subx()中间没有参数,则VB自动调用重载subx(A状 态), overloads public sub subx()重载subx(A状态) strtem=”3” end sub 结果就是strtem=”3” 如果你这样调用,subx(“这是一个不同的结果”),则VB自动调用重 载subx(B状态) overloads public sub subx(x as string) 重载subx(B状 态) strtem=x end sub 结果就是 strtem=“这是一个不同的结果” 您可以在函数中调用,包括在API中(当然VB.Net不再使用winAPI). 甚至一些至关紧要的过程也不例外:如new过程(此方法王国荣文章中 有介绍) 如: class class1 Overloads Public Sub New() 。。。。。。 。。。。。。 end sub Overloads Public Sub New(s as string) 。。。。。。 。。。。。。 end sub end class 同样有两种调用的方法,如dim as as class1=new class 调用前面 一个无参数的new过程, 而dim asb as class1=new class(“string”)调用第二个new 过程. 切记,如果一个过程或一个函数只有一种状态,不能这样运用。如果您试 图使用overloads关键字,则VB.Net会出现错误的提示要求你移去 overloads 五:Overrides改写 如常见的dispose过程: Overrides Public Sub Dispose() MyBase.Dispose components.Dispose End Sub Overrides表示完全的改写父类提供的过程与函数 不过要记住使用改写的前提是在父类的同一过程或函数的前面也使用了 overrides关键字 六:mybase.new 与mybase.dispose ublic Sub New() MyBase.New Form1 = Me InitializeComponent End Sub Overrides Public Sub Dispose() ' MyBase.Dispose components.Dispose End Sub 这两个过程是任何一个新建工程都会有的,mybase.new是调用当前类的 父类的new过程,同样你也可以加上参数配合父类的重载New过程。如: Overloads Public Sub New () MyBase.New() End Sub Overloads Public Sub New (string) MyBase.New(string) End Sub 七:Structure构造 自定义类型,如: Structure Type1 Dim x as y Dim a as b 。。。。。。。 end structure 这个取代VB中的使用type………end type方式自定义类型的方法。 八:return返回值 function functionname() return X。。。。。。 end funtion 他的作用与下面一样: function functionname() functionname = X。。。。。。 end funtion
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值