对话框关闭时,VB.net表单刷新并禁用按钮

问题描述 投票:0回答:2

我正在开发vb.net应用程序。如果我有一个表单并单击发送邮件按钮打开另一个表单来输入邮件详细信息并发送具有确定和取消按钮的邮件。

要在代码下面打开form2,

 Private Sub Button1_Click()
    Dim obj As New Form2
    obj.ShowDialog(Me)
End Sub

在form1中有一个计算按钮,只有当邮件从form2发送时才应该启用。如果用户单击form2上的取消按钮,则计算按钮需要在form1中禁用。哪个没有发生。以下是我的代码,

 Private Sub Button2_Click()
        Dim obj As New Form1()
        obj.initinfo(System.Windows.Forms.DialogResult.Cancel)
        Me.Close()
    End Sub

在下面的form1上添加了代码

Friend Sub initinfo(result As DialogResult)
    If (result = DialogResult.Cancel) Then
        Me.Refresh()
        Me.Activate()
        Button2.Enabled = False
    End If
End Sub

上面的代码没有效果。请提供一些建议。

在此先感谢Sangeetha

vb.net winforms
2个回答
0
投票

如果按下Form2上的Button2(我认为是取消按钮),您想在Form1上禁用Button2。

在“属性”窗口中:

设置取消按钮属性Form2.Button2.DialogResult = DialogResult.Cancel 设置肯定响应按钮(比如说Button1)属性Button1.DialogResult = DialogResult.OK

让Form2知道哪个是:

Form2.AcceptButton = Button1
Form2.CancelButton = Button2

在Form1中,使用Form.DialogResult属性评估响应: (包含在使用块中以正确处理您创建的对象的()

Private Sub Button1_Click()
    Using obj As New Form2
        obj.ShowDialog(Me)

        If obj.DialogResult = Windows.Forms.DialogResult.Cancel Then
           Me.Button2.Enabled = False
        End If
   End Using
End Sub

其他方式。 在Form2上创建自定义结果属性并在窗体关闭时检查其状态。

Class Form2
    (...)
    Public Property UserChoice As Boolean
    (...)

    Private Sub Button1_Click()
        Me.UserChoice = True
        Me.Close()
    End Sub

    Private Sub Button2_Click()
        Me.UserChoice = False
        Me.Close()
    End Sub

    (...)
End Class

在Form1中

Private Sub Button1_Click()
    Using obj As New Form2
        obj.ShowDialog(Me)

        If obj.UserChoice = False Then
           Me.Button2.Enabled = False
        End If

        'Or -> 
        Me.Button2.Enabled = obj.UserChoice
   End Using
End Sub

1
投票

您正在Form2上创建Form1的新实例,此新实例不是指原始表单,而是创建新表单。

以下是正确的步骤:

1.为Form1上的Button创建一个属性。

Public ReadOnly Property BtnSubmit As Button
    Get
        Return button1
    End Get
End Property

2.在Form2中为Form1创建一个属性。

Public Property Form1Instance As Form1
    Get
    End Get
    Set
    End Set
End Property

3.现在在实例化form2时,将Me作为Form1Instance属性中的引用传递

Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim f2 As Form2 = New Form2
    f2.Form1Instance = Me
    f2.Show
End Sub

4.在Form2上,使用如下:

If (Not (Form1Instance) Is Nothing) Then
   Form1Instance.BtnSubmit.Enabled = false
End If

在你的情况下,它将是:

Form1Instance.initinfo(System.Windows.Forms.DialogResult.Cancel)

我希望你明白这个主意。

© www.soinside.com 2019 - 2024. All rights reserved.