隐藏Visual Basic保存表单信息

时间:2014-01-14 20:50:22

标签: winforms vba visual-studio-2008

我正在使用Visual Basic 2008

我有2个表格 Main,EditCustomerInfo

主表单包含以下内容

 Public Class Main

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)   Handles Button1.Click
    EditCustomerInfo.ShowDialog()
End Sub

EditCustomerInfo包含一个文本框和以下

 Public Class EditCustomerInfo

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)   Handles Button1.Click

  If Not CustomerIDTextBox.Text = "" Then
        Me.Close()

    Else : Me.Hide()
    End If

End Sub

它做了什么: 因此,当我调试程序时单独使用此代码,它将我带到主窗体并允许我单击按钮以打开editcustomerinfo表单

当我在editcustomerinfo表单上时,我有一个文本框和一个按钮。如果在文本框中键入了某些内容并单击了该按钮,则表单会隐藏,如果单击该按钮时文本框中未输入任何内容,则表单将关闭。

我想做什么: 如果在文本框中输入了某些内容,我希望editcustomerinfoform上的按钮隐藏editcustomerinfoform,并在主窗体上创建一个按钮,允许用户将editcustomerinfo表单与文本框中键入的内容一起备份。

建议?

1 个答案:

答案 0 :(得分:1)

像这样的自动行为总是让我担心。您如何知道用户何时完成输入而没有丢失焦点事件。如果屏幕上没有其他控件,则用户不会轻易知道如何触发事件。话虽这么说,您可以使用计时器来延迟屏幕关闭从KeyPress事件。

Public Class EditCustomerInfo
    Private WithEvents userInputDelay As Timer = New Timer() With {.Interval = 1000} REM 1 second delay for next user input
    Public ReadOnly Property CustomerId As String
        Get
            Return CustomerIDTextBox.Text
        End Get
    End Property
    Private Sub CustomerIDTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CustomerIDTextBox.KeyPress
        userInputDelay.Enabled = False
        REM Reset the timer
        userInputDelay.Enabled = True

    End Sub
    Private Sub userInputDelay_Tick(sender As Object, e As KeyPressEventArgs) Handles userInputDelay.Tick
        If Not CustomerIDTextBox.Text = "" Then
            Me.Close()

        Else : Me.Hide()
        End If
    End Sub
End Class

在Main中添加一个按钮(Button2)。当EditCustomerInfo.Textbox1.Text值为null / blank / white space时,下面的代码将隐藏Button1。 Button2的可见性始终与Button1相反。

Private EditCustomerInfoInstance As New EditCustomerInfo

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    EditCustomerInfoInstance.ShowDialog()
    Button1.Visible = String.IsNullOrWhiteSpace(EditCustomerInfoInstance.CustomerId)
End Sub

Private Sub Button1_VisibleChanged(sender As Object, e As EventArgs) Handles Button1.VisibleChanged
    Button2.Visible = Not Button1.Visible
End Sub