如何用视觉基本语言调用公共功能按钮? [英] How to call public function to button in visual basic language?

查看:63
本文介绍了如何用视觉基本语言调用公共功能按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在vb上调用函数时遇到了一些问题

i have some problem to call function on vb

公共功能如下:-

Public Function GetUID(ByRef UIDBytes() As Byte) As Boolean
        Dim wBytes() As Byte = {&H6, &HC, &H27, &H1, &H0, &H2A, &H50}
        s.Write(wBytes, 0, wBytes.Length)
        System.Threading.Thread.Sleep(50)
        ReDim UIDBytes(11)
        s.Read(UIDBytes, 0, 12)
        Me.ClearComm()
        If CheckCRC(UIDBytes) = True Then
            Dim i As Integer
            For i = 0 To 7
                UIDBytes(i) = UIDBytes(i + 2)
            Next
            ReDim Preserve UIDBytes(7)
            Return True
        Else
            Return False
        End If
    End Function

我想将函数调用到按钮上,但是我不知道该怎么做.

i want to call the function to the button, but i don't know how to do it.. 

非常感谢您的帮助.

推荐答案

对于一个函数,功能可能应该创建并处理"s".我想是一个StreamWriter.也许这是一个内存流,因为它同时在写和读.

For one the Function should probably create and dispose of "s" which I suppose is a StreamWriter. Or maybe it's a memory stream since it is both writing and reading.

您为什么要休眠线程?通过查看您的代码,我不知道有什么原因.如果功能是在Forms代码中,则所有操作都会导致主UI毫无原因地变得无响应.

Why do you sleep the thread? I don't realize a reason for that from looking at your code. All that does is cause the main UI, if the function is in a Forms code, to become unresponsive for no apparent reason.

我也不知道Me.ClearComm是什么,但是我不认为它真的应该在Function中使用.我对这些事情不是完全了解,但是通常我不会使用函数代码来调用外部例程,除非它们是 传递给它ByRef或ByVal.而是将ByRef或ByVal传递给Function,以便它将使用任何东西将某些东西返回给调用方.

I also don't know what Me.ClearComm is but I don't believe it should be used within a Function really. I'm not totally knowledgable about these things but typically a Functions code is not used to call outside routines to my knowledge unless they are passed to it ByRef or ByVal. Rather you would pass ByRef or ByVal to the Function so it would use whatever those are to return something to the caller.

将来或更改(如果需要,可以编辑(将代码粘贴到代码块中)) 现在发布;

In the future or alter (edit to paste your code in a code block) your post now if you'd like;

虽然我不知道您将传递给函数什么字节,但是也许此代码可以工作.而且我不记得该函数中的UIDBytes的ByRef或ByVal是否允许该函数更改传递给函数UIDByes的BytesForUID.

Maybe this code would work although I've no idea what bytes you would pass to the Function. And I don't remember if ByRef or ByVal for UIDBytes in the Function allows the Function to alter BytesForUID passed to the Functions UIDByes.

另请参见 按值和引用传递参数(Visual Basic) 和 函数语句(Visual Basic).

Also see Passing Arguments by Value and by Reference (Visual Basic) and Function Statement (Visual Basic).

    Dim BytesForUID() As Byte = {&H24, &H1, &H27, &H34}

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
	MessageBox.Show(GetUID(BytesForUID).ToString) ' Should display true or false
    End Sub

    Public Function GetUID(ByRef UIDBytes() As Byte) As Boolean
         Dim wBytes() As Byte = {&H6, &HC, &H27, &H1, &H0, &H2A, &H50}
         s.Write(wBytes, 0, wBytes.Length)
         System.Threading.Thread.Sleep(50)
         ReDim UIDBytes(11)
         s.Read(UIDBytes, 0, 12)
         Me.ClearComm()
         If CheckCRC(UIDBytes) = True Then
             Dim i As Integer
             For i = 0 To 7
                 UIDBytes(i) = UIDBytes(i + 2)
             Next
             ReDim Preserve UIDBytes(7)
             Return True
         Else
             Return False
         End If
     End Function


这篇关于如何用视觉基本语言调用公共功能按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆