【问题标题】:How to refresh win forms using vb.net?如何使用 vb.net 刷新 win 表格?
【发布时间】:2011-10-15 00:08:06
【问题描述】:

我正在为申请注册表工作,我想知道用户何时打开注册表我需要检查当前日期是否正确以及当用户尝试更改系统日期时他应该弹出消息.所以我想每秒刷新一次表格,看看他是否改变了日期。 我该怎么做?

这是我的代码:

 btnRegister.Enabled = False
    Dim oReg As Microsoft.Win32.RegistryKey
    oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
    oReg = oReg.CreateSubKey(kstrRegSubKeyName)
    oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\" & kstrRegSubKeyName)
    Dim strOldDay As String = oReg.GetValue("UserSettings", "").ToString
    Dim strOldMonth As String = oReg.GetValue("operatingsystem", "").ToString
    Dim strOldYear As String = oReg.GetValue("GUID", "").ToString
    Dim strRegName As String = oReg.GetValue("USERID", "").ToString
    Dim strRegCode As String = oReg.GetValue("LOCALPATH", "").ToString
    Dim strCompID As String = oReg.GetValue("CompID", "").ToString
    Dim strTrialDone As String = oReg.GetValue("Enable", "").ToString
    oReg.Close()

    'If the keys should automatically be created, then create them.
    If strOldDay = "" Then
        CreateRegKeys(txtPassPhrase.Text)
    End If

    'If the keys are encrypted, decrypt them.
    'If EncryptKeys = True Then
    strOldDay = Decrypt(txtPassPhrase.Text, strOldDay)
    strOldMonth = Decrypt(txtPassPhrase.Text, strOldMonth)
    strOldYear = Decrypt(txtPassPhrase.Text, strOldYear)
    'End If

    'Define global variables.
    mintUsedTrialDays = DiffDate(strOldDay, strOldMonth, strOldYear)

    'Fill the progress bar
    lblApplicationStatus.Text = DisplayApplicationStatus(DiffDate(strOldDay, strOldMonth, strOldYear), mintTrialPeriod)

    'Disable the continue button if the trial is over
    If DiffDate(strOldDay, strOldMonth, strOldYear) > mintTrialPeriod Then
        'unregbutton.Enabled = False
        mblnInTrial = False
        btnRemind.Enabled = False
        oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
        oReg = oReg.CreateSubKey(kstrRegSubKeyName)
        oReg.SetValue("Enable", "1")
        oReg.Close()
    End If




    If strOldMonth = "" Then

    Else

        Dim dtmOldDate As Date = New Date(Convert.ToInt32(strOldYear), Convert.ToInt32(strOldMonth), Convert.ToInt32(strOldDay))
        If Date.Compare(DateTime.Now, dtmOldDate) < 0 Then
            'lblApplicationStatus.Text = DisplayApplicationStatus(mintTrialPeriod, mintTrialPeriod)
            lblApplicationStatus.Text = "The system clock has been manually changed, and the application has been locked out to prevent unauthorized access!"
        End If
    End If


    'If the trial is done then disable the button
    If strTrialDone = "1" Then
        mblnInTrial = False
        btnRemind.Enabled = False
        lblApplicationStatus.Text = "The system clock has been manually changed, and the application has been locked out to prevent unauthorized access!"
    End If

    'See if the user is already registered, if so re-process the info and check if the computer is all okay.,
    If strRegName = "" Then
    Else
        Dim strRN As String = Decrypt(txtPassPhrase.Text, strRegName)
        Dim strRC As String = Decrypt(txtPassPhrase.Text, strRegCode)
        Dim UserName As String = strRegName
        UserName = UserName.Remove(16, (UserName.Length - 16))
        If UserName = Decrypt(txtPassPhrase.Text, strRegCode) Then
            If Encrypt(txtPassPhrase.Text, cHardware.GetMotherBoardID.Trim.ToString) = strCompID Then
                mblnInTrial = False
                mblnFullVersion = True
                strRC = strRC.Insert(4, "-")
                strRC = strRC.Insert(8, "-")
                strRC = strRC.Insert(12, "-") 'Add dashes to make it look cool
                lblApplicationStatus.Text = "Licensed version to " + strRN + " with the key " + strRC
                txtVKClientName.Enabled = False
                txtKeyToValidate.Enabled = False
                txtVKClientName.Text = strRN
                txtKeyToValidate.Text = strRC
                btnRemind.Text = "Registered"
                frmMain.Text = "Aquamark v1.2(Registered)"
                btnRegister.Hide()
                Me.Close()
                frmMain.Show()
                oReg = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software", True)
                oReg = oReg.CreateSubKey(kstrRegSubKeyName)
                oReg.SetValue("Enable", "")
                oReg.Close()
            End If
        End If
    End If

【问题讨论】:

    标签: vb.net winforms refresh


    【解决方案1】:

    您可以订阅SystemEvents.TimeChanged event,当用户更改系统时钟的时间时会触发该SystemEvents.TimeChanged event。不要忘记在表单关闭时分离事件处理程序,因为这是一个静态事件。

    您可以将日期存储在字段中,并在系统时间更改时将新日期与其进行比较,或者在事件发生时禁用应用程序。

    【讨论】:

      【解决方案2】:

      不,您不必“refresh the form for every second and find whether he has changed the date or not. How do I do that”,解决方案要容易得多

      当数据发生变化时,窗口会向所有应用程序发送消息。收听该消息,并在数据更改时执行您想要执行的操作:

      //C#对不起,我不懂VB,但代码很简单,你应该毫无问题地转换它

      private DateTime _lastSystemClockChanged = DateTime.MinValue;
      
      protected override void WndProc(ref Message m)
      {
          switch (m.Msg)
          {
              case 0x1E://the time is changed. 
                  //to avoid receiving duplicate notification about time changed
                  if (_lastSystemClockChanged.AddSeconds(1) < DateTime.Now ||
                      _lastSystemClockChanged > DateTime.Now)
                  {
                      _lastSystemClockChanged = DateTime.Now;
                      //do what ever you want to do when time changed. note you should 
                      //not call a methods that will block here because of you will block 
                      //this message from arriving to other applications then
                  }
                  break;
          }
          base.WndProc(ref m);
      }
      

      【讨论】:

      • 感谢 jalal,你让我变得非常简单,即使是 C# 也没有问题,我可以用 VB 转换器管理它。
      猜你喜欢
      • 1970-01-01
      • 2011-09-14
      • 2016-07-10
      • 1970-01-01
      • 2012-12-11
      • 2012-09-24
      • 1970-01-01
      • 2015-10-30
      • 2019-04-11
      相关资源
      最近更新 更多