Reputation: 591
I am developing vb.net application. Where i have a form and click on send mail button opens another form to enter mail details and send mail which has ok and cancel button.
To open form2 below code is used,
Private Sub Button1_Click()
Dim obj As New Form2
obj.ShowDialog(Me)
End Sub
In form1 there is calculate button which should enable only if mail has sent from form2. If user click on cancel button on form2 then calculate button need ro be disabled in form1. Which is not happening. Below is my code,
Private Sub Button2_Click()
Dim obj As New Form1()
obj.initinfo(System.Windows.Forms.DialogResult.Cancel)
Me.Close()
End Sub
On form1 below code is added
Friend Sub initinfo(result As DialogResult)
If (result = DialogResult.Cancel) Then
Me.Refresh()
Me.Activate()
Button2.Enabled = False
End If
End Sub
The above code has no effect. Please provide some suggestions.
Thanks in advance Sangeetha
Upvotes: 0
Views: 665
Reputation: 32278
You want to disable Button2 on Form1, if Button2 (which I suppose is the Cancel button) on Form2 is pressed.
In the Properties window:
Set your cancel button property Form2.Button2.DialogResult = DialogResult.Cancel
Set the positive response button (let's say Button1) property Button1.DialogResult = DialogResult.OK
Let Form2 know which is which:
Form2.AcceptButton = Button1
Form2.CancelButton = Button2
in Form1, evaluate the response using the Form.DialogResult property:
(enclose in a Using block to properly Dispose() of the object you created)
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
Another way.
Create a custom result property on Form2 and check its status when the Form closes.
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
In 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
Upvotes: 0
Reputation: 3424
You are creating a new instance of Form1 on Form2, this new instance is not referring to the original form, but creating a new form.
Following are the correct steps:
1.Create a property for the Button on Form1.
Public ReadOnly Property BtnSubmit As Button
Get
Return button1
End Get
End Property
2.Create a property for the Form1 in Form2.
Public Property Form1Instance As Form1
Get
End Get
Set
End Set
End Property
3.Now while instantiating form2, pass Me
as reference in Form1Instance property
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.On Form2, use like this:
If (Not (Form1Instance) Is Nothing) Then
Form1Instance.BtnSubmit.Enabled = false
End If
In your case it will be:
Form1Instance.initinfo(System.Windows.Forms.DialogResult.Cancel)
I hope you get the idea.
Upvotes: 1