TextBox1无法每1秒显示更改结果

时间:2018-10-07 03:45:53

标签: vb.net

我试图按“ SHOW”按钮后每1秒显示一次从DataGridViewTextBox1的选择结果,但是这里的问题是,它将仅在显示“ LANCE” 5秒钟后显示TextBox1。

我需要TextBox1才能显示YYYYY-> dhghgY-> jttr-> lkukm-> wewerf-> LANCE

如何解决这个问题?

我的代码如下

Imports System.Data.DataTable
Imports System.Threading
Public Class Form1
    Dim table As New DataTable("Table")
    Dim index As Integer


    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs)
        table.Rows.Add(TextBoxID.Text, TextBoxFN.Text, TextBoxFN.Text, TextBoxAGE.Text)

        DataGridView1.DataSource = table

    End Sub
    Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        index = e.RowIndex
        Dim selectedRow As DataGridViewRow
        selectedRow = DataGridView1.Rows(index)
        TextBoxID.Text = selectedRow.Cells(0).Value.ToString()
        TextBoxFN.Text = selectedRow.Cells(1).Value.ToString()
        TextBoxLN.Text = selectedRow.Cells(2).Value.ToString()
        TextBoxAGE.Text = selectedRow.Cells(3).Value.ToString()
    End Sub

    Private Sub btnUpdate_Click(sender As System.Object, e As System.EventArgs) Handles btnUpdate.Click
        Dim newDataRow As DataGridViewRow
        newDataRow = DataGridView1.Rows(index)
        newDataRow.Cells(0).Value = TextBoxID.Text
        newDataRow.Cells(1).Value = TextBoxFN.Text
        newDataRow.Cells(2).Value = TextBoxLN.Text
        newDataRow.Cells(3).Value = TextBoxAGE.Text
    End Sub

    Private Sub Form1_Load_1(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        table.Columns.Add("Id", Type.GetType("System.Int32"))
        table.Columns.Add("First Name", Type.GetType("System.String"))
        table.Columns.Add("Last Name", Type.GetType("System.String"))
        table.Columns.Add("Age", Type.GetType("System.Int32"))

        table.Rows.Add(1, "XXXX", "YYYYY", 21)
        table.Rows.Add(2, "SSDD", "dhghgY", 33)
        table.Rows.Add(3, "fhdt", "jttr", 53)
        table.Rows.Add(4, "jyj", "lkukm,", 19)
        table.Rows.Add(5, "reytr", "wewerf", 36)
        table.Rows.Add(6, "MAN", "LANCE", 63)

        DataGridView1.DataSource = table



    End Sub


    Private Sub btnSHOW_Click(sender As System.Object, e As System.EventArgs) Handles btnSHOW.Click



        For j As Integer = 0 To 5

            TextBox1.Clear()

            TextBox1.Text = DataGridView1.Rows(j).Cells(2).Value.ToString()

            Threading.Thread.Sleep(1000)


        Next


    End Sub
End Class

Push button "SHOW" and display on Textbox1

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您会在工作表上有一个 ActiveX文本框,您要在其中以 5个值的速度循环显示每秒一个

img

如果大致正确,则可以调整此代码以适应您的需求:

Option Explicit
Public schedTime As Date, lastActionID As Byte

Sub startTimer()
    Dim txt As String

    'do something for the one second timer
    Select Case lastActionID
       Case 0: txt = Format(Date, "dddd")
       Case 1: txt = Format(Date, "mmmm")
       Case 2: txt = Format(Date, "d")
       Case 3: txt = Format(Date, "yyyy")
       Case 4: txt = Format(Date, "H:mm am/pm")
    End Select

    'set value of activex textbox on worksheet
    ActiveSheet.OLEObjects("TextBox1").Object.Value = txt

    'set next action
    lastActionID = lastActionID + 1
    If lastActionID >= 5 Then lastActionID = 0  'produces numbers 0 to 4

    'restart timer
    schedTime = Now() + TimeValue("00:00:01")
    Application.OnTime schedTime, "startTimer", , True
End Sub

Sub stopTimer()
    On Error Resume Next
    ActiveSheet.OLEObjects("TextBox1").Object.Value = ""    'clear textbox
    Application.OnTime schedTime, "startTimer", , False     'cancel timer
End Sub

只需将代码放入新模块中,然后在工作表上创建一个名为TextBox1的ActiveX文本框。

  • 运行子startTimer以开始计时器过程。
  • 运行子stopTimer以停止计时器过程。

需要注意的几点:

  1. 对该控件的引用不是 fully qualified,因此将在ActiveSheet上查找该控件。

  2. 没有没有错误处理,也没有代码来处理与计时器相关的潜在问题

    例如,启动计时器,然后关闭工作簿而不停止它...立即尝试! hehehe,第一次看到这种情况时有点奇怪,是吗?

    无论您做什么,计时器都会一直运行 ,直到您将其停止。因此,最好在StopTimer模块的Workbook_BeforeClose事件中调用ThisWorkbook

  3. 像这样运行短间隔计时器(相隔几秒钟或更短)可能会影响工作表的性能,并且由于每秒钟都会中断所有事情,因此可能会使进一步的编码工作有些痛苦。