vb.net 在 VB 中加载窗体窗口后如何运行函数/子?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15974936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:49:17  来源:igfitidea点击:

how to run a function/sub after loading the form window in VB?

vb.net

提问by user2234234

I have a function that gets User ID from USB badge reader, used to log in an application. when I run the app, the log in window does not appear until I swipe the tag. I need to know if it`s possible to load the windows, then to start running the function that gets the data from the USB.

我有一个从 USB 徽章阅读器获取用户 ID 的功能,用于登录应用程序。当我运行该应用程序时,在我滑动标签之前不会出现登录窗口。我需要知道是否可以加载 Windows,然后开始运行从 USB 获取数据的函数。

Thanks :)

谢谢 :)

Private Sub SerialPort1_DataReceived()
    'Threading.Thread.SpinWait(1000)
    OpenPort()
    If SerialPort1.IsOpen() Then

        byteEnd = SerialPort1.NewLine.ToCharArray
        'read entire string until .Newline 
        readBuffer = SerialPort1.ReadLine()
        readBuffer = readBuffer.Remove(0, 1)
        readBuffer = readBuffer.Remove(8, 1)
        WWIDTextBox.AppendText(readBuffer)

    End If
End Sub

Private Sub Form1_Activated(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Activated
    SerialPort1_DataReceived()
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'SerialPort1_DataReceived()
End Sub

回答by Steven Doggart

The problem is that you are calling the ReadLinemethod, which is a blocking (synchronous) method. In other words, when you call it, the method does not return the value until it has the value to return. Because of that, it stops execution on the current thread until a complete line is read (when the badge is swiped). Since you are on the UI thread when you call it, it will lock up the UI until the badge is swiped.

问题在于您正在调用该ReadLine方法,这是一个阻塞(同步)方法。换句话说,当您调用它时,该方法不会返回值,直到它有要返回的值。因此,它会停止在当前线程上执行,直到读取完整行(刷卡时)。由于您在调用它时处于 UI 线程上,因此它会锁定 UI,直到刷卡徽章。

Instead of calling your SerialPort1_DataReceivedmethod from the UI thread, you can do the work from a different thread. The easiest way to do that is to drag a BackgroundWorkercomponent onto your form in the designer. Then you can add code like this:

SerialPort1_DataReceived您可以从不同的线程完成工作,而不是从 UI 线程调用您的方法。最简单的方法是将BackgroundWorker组件拖到设计器中的表单上。然后你可以添加这样的代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BackgroundWorker1.RunWorkerAsync()
End Sub

Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    OpenPort()
    If SerialPort1.IsOpen() Then
        byteEnd = SerialPort1.NewLine.ToCharArray
        Dim readBuffer As String = SerialPort1.ReadLine()
        readBuffer = readBuffer.Remove(0, 1)
        readBuffer = readBuffer.Remove(8, 1)
        e.Result = readBuffer
    End If
End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    WWIDTextBox.AppendText(CStr(e.Result))
End Sub

回答by Ivan Perez

Working on VS2013, I came across the same issue, I needed to to a datagridview refresh (colors in the gridrows). This worked for me.

在 VS2013 上工作时,我遇到了同样的问题,我需要刷新 datagridview(网格中的颜色)。这对我有用。

Sub MyForm_VisibleChanged(sender As Object, e As EventArgs) Handles Me.VisibleChanged

            If Me.Visible Then
                'do action...
            End If

End Sub

回答by Pandian

Try Form ActivatedEvent

尝试表单激活事件

Private Sub Form1_Activated(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Activated
 'Call your function here
End Sub 

It call the function After the Form Loads...

它调用函数After the Form Loads...

回答by user4368893

Private Sub loadCombo()
    Dim sqlconn As New OleDb.OleDbConnection
    Dim connString As String
    connString = ""
    Dim access As String
    access = "select slno from atable"
    Dim DataTab As New DataTable
    Dim DataAdap As New OleDbDataAdapter(access, connString)
    DataAdap.Fill(DataTab)
    ComboBox1.DataSource = DataTab
    ComboBox1.DisplayMember = "slno"

End Sub