Form Enabled in FormClosing Event Not Working - vb.net

I'll show you my code first:
Private Sub AddProductToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddProductToolStripMenuItem.Click
Me.Enabled = False
Dim frmAddProduct As New FormAddProduct
frmAddProduct.Show()
frmAddProduct.Owner = Me
End Sub
That is my Main Form to call AddProduct form, and this is my FormClosing in AddProduct
Private Sub FormAddProduct_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
ButtonReset.PerformClick()
Lock()
Me.Owner = Nothing
Me.Hide()
Dim frmMainIndex As New FormMainIndex
frmMainIndex.Enabled = True
End Sub
So I have set enabled = false in my main form when it call Add Product form, and enabled = true when I close my Add Product form, but enabled = true won't work.
When I close my Add Product, it's only hide Add Product form but not enabling main form, main form still not enabled. Is there something wrong with my code?

This line is your problem:
Dim frmMainIndex As New FormMainIndex
You are instantiating a new FormMainIndex. Whenever you use the New keyword you are creating a completely new and independant object. frmMainIndex is a completely different form than the first one which opened your FormAddProduct form.
Since you've set the FormAddProduct's owner to your FormMainIndex form, just set the owner's Enabled property to True instead:
ButtonReset.PerformClick()
Lock()
Me.Owner.Enabled = True
Me.Owner = Nothing
Me.Hide()
Also, your Me.Hide() call doesn't make any sense since your form is about to be closed.

Because you created a new instance of FormMainIndex, the frmMainIndex.Enabled was applied to this new instance, not the one that created your FormAddProduct. Why don't you show your FormAddProduct as a dialog. like this
Dim frmAddProduct As New FormAddProduct
frmAddProduct.ShowDialog(Me)

Related

Parent form sometimes does not close (Only in Windows 10)

My main form is frmInvoice. This sub is located inside frmInvoice.
This is one of the Subs that sometimes causes frmDark to not close. frmLookup does not display when this happens. frmDark just stays there covering frmInvoice. It's like it doesn't reach the call to frm.ShowDialog(frmDark), cause when I press the lookup key, it displays the frmLookup, but upon closing frmLookup, frmDark is still there.
No exception is being raised.
Note that this only happens in Windows 10. In Windows 8/7, this never happened. What am I missing?
This happens at different times. Sometimes I could press the lookup key for 20 times and it will display fine. Sometimes, after 1 press of the lookup key and this happens.
Private Sub ItemLookup()
Try
Using frmDark As New Form
With frmDark
.ShowInTaskbar = False
.Icon = Me.Icon
.FormBorderStyle = Windows.Forms.FormBorderStyle.None
.BackColor = Color.Black
.Opacity = 0.95
.WindowState = FormWindowState.Maximized
.Show(Me)
Using frm As New frmLookup
With frm
.Icon = Me.Icon
.ShowDialog(frmDark)
frmDark.Close()
If .DialogResult = Windows.Forms.DialogResult.OK Then
' Do stuff here
End If
End With
End Using
End With
End Using
Catch ex As Exception
ErrMsg(ex)
End Try
End Sub
UPDATE: I'm using .Net Framework 4.8
Thanks
I would suggest rearranging the code like so:
Dim lookupResult As DialogResult
Using frmDark As New Form With {.ShowInTaskbar = False,
.Icon = Me.Icon,
...}
frmDark.Show(Me)
Using frm As New frmLookup With {.Icon = Me.Icon}
lookupResult = frm.ShowDialog(frmDark)
End Using
End Using
If lookupResult = DialogResult.OK Then
'...
End If
Because that code exits the Using block that created frmDark, there should be no way that it can't close.
Also, instead of using a vanilla Form and configuring it on demand, I would suggest that you create a dedicated form type to use as the overlay in that scenario. You can then get rid of all the property assignments.
Having a dedicated overlay form would also allow you to reconfigure things significantly and, in my opinion, better. The overlay form could have a property of type Form. You main form could then create a frmLookup instance and assign it to that property, than call ShowDialog on the overlay form. In the Shown event handler of the overlay form, it could then call ShowDialog on the form in that property. When that call returns, it could assign the result to its own DialogResult property and close itself. The main form would then just get the result from calling ShowDialog on the overlay. That might look like this:
Public Class OverlayForm
Public Property DialogueForm As Form
Private Sub OverlayForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
DialogResult = DialogueForm.ShowDialog()
End Sub
End Class
and this:
Public Class MainForm
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using dialogue As New DialogueForm,
overlay As New OverlayForm With {.DialogueForm = dialogue}
If overlay.ShowDialog() = DialogResult.OK Then
MessageBox.Show("OK")
End If
End Using
End Sub
End Class

System.ObjectDisposedException when closing a form in vb.net

I am having a weird error and I am sure it is something very simple, but I can't for the life of me figure out what is going on. I thought I was opening and closing the forms properly, but it appears something is amiss. I am working with VS 2015. I have my program set to close when the last form is closed. There are two forms that I am using right now.
Dashboard
Public Class frmDashboard
Private Sub recExit_Click(sender As Object, e As EventArgs) Handles recExit.Click
Me.Close()
End Sub
Private Sub recMember_Click(sender As Object, e As EventArgs) Handles recMember.Click, lblMember.Click
'Create instance of Member Form
Dim memberForm As New frmMember
'Open an instance
memberForm.Show()
Me.Close()
'Using this code test if the form window is still open
For Each frm As Form In Application.OpenForms
MessageBox.Show(frm.Name)
Next
End Sub
End Class
Members Form
Public Class frmMember
Private Sub frmMember_Load(sender As Object, e As EventArgs) Handles Me.Load
ShowSpouse(rdoMarried.Checked)
End Sub
Private Sub ShowSpouse(ByRef isMarried As Boolean)
If isMarried Then
'Marital status is set to married lets show the extra spouse information
'Lets first make the form larger
Me.Size = New Size(793, 576)
'Now lets unhide the form
pnlSpouse.Visible = True
'pnlSpouse.Enabled = True
lblSpouse.Visible = True
'lblSpouse.Enabled = True
lineSpouse.Visible = True
'lineSpouse.Enabled = True
'Now lets move the buttons to their new location
btnApply.Location = New Point(624, 541)
btnExit.Location = New Point(710, 541)
Else
'Single is Selected
'Let's make sure all the spouse information is hidden
'Lets first make the form smaller
Me.Size = New Size(793, 358)
'Now lets hide the form and disable the controls
pnlSpouse.Visible = False
'pnlSpouse.Enabled = False
lblSpouse.Visible = False
'lblSpouse.Enabled = False
lineSpouse.Visible = False
'lineSpouse.Enabled = False
'Now lets move the buttons to their new location
btnApply.Location = New Point(624, 320)
btnExit.Location = New Point(710, 320)
End If
End Sub
Private Sub MaritalStatusChanged(sender As Object, e As EventArgs) Handles rdoSingle.CheckedChanged, rdoMarried.CheckedChanged
ShowSpouse(rdoMarried.Checked)
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Dim dashboard As New frmDashboard
'Open the dashboard
dashboard.Show()
'Close the member form
Me.Close()
End Sub
End Class
I havent added the database to it but eventually I will load data into the members form. When I click to open the members form it goes through and runs the code in the form load event for members form but then returns focus back to the dashboard form. Runs the Me.Close on the dashboard, but once it gets to End Sub thats when this exception gets thrown:
System.ObjectDisposedException was unhandled
Message: An unhandled exception of type 'System.ObjectDisposedException' occurred in System.Windows.Forms.dll
Additional information: Cannot access a disposed object.
Any help you guys can give would be greatly appreciated.

Winforms - MDI Parent refresh/re-activate

I have a MDI Parent form which has a menustrip for the application. My application startup file is the MDI Parent form which on load calls a child login form. Code as below:
Dim myForm As Form = New Login
Dim formResult As DialogResult = myForm.ShowDialog()
If formResult = Windows.Forms.DialogResult.OK Then
If LoginSucceeded = True Then
Me.tabMainMenu.Visible = True
ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
myForm.Dispose()
End If
End If
The menustrip has a Logout label which when clicked disables the menu strip and displays the login form again.
The boolean field LoginSucceeded determines a successful validation of the user credentials and sets the menu according to the access given to that user. My problem is the first time the main menu on the MDI parent is set properly based on the user's access. After logging out and logging in again, i wanted to set the main menu accordingly again which is not happening.
The Form_Load event on the MDI Parent is being executed only once.
Any tips of re-painting the MDI parent when it receives focus the 2nd time onwards.
Thanks,
ZK
My code for the Logoff is as below:
Dim blnLogout As DialogResult = MessageBox.Show("Are You Sure You Want To Logout?", "eApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If blnLogout = Windows.Forms.DialogResult.Yes Then
SetToolbarMenuStyle()
tabMainMenu.Visible = False
LoginSucceeded = False
blnShowLoginTab = True
Dim myForm As Form = New Login
myForm.MdiParent = Me
myForm.WindowState = FormWindowState.Normal
myForm.Show()
End If
Move your login code to its own method in your main form so you can call it multiple times:
Public Class Form1
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown ValidateLogin()
ValidateLogin()
End Sub
Private Sub LoginLogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoginLogoutToolStripMenuItem.Click
ValidateLogin()
End Sub
Private Sub ValidateLogin()
' disable appropriate main form elements so they can't access anything:
Me.tabMainMenu.Visible = False
Using myForm As New Login
If myForm.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
' login succeeded: re-enable main form elements
Me.tabMainMenu.Visible = True
ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
Else
MessageBox.Show("Login Failed")
End If
End Using
End Sub
End Class
You also don't need the "LoginSucceeded" variable. You can pass a success/failure back to the main form by setting DialogResult to OK in your Login form:
Public Class Login
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If True Then ' <-- perform your check
Me.DialogResult = Windows.Forms.DialogResult.OK ' only return OK if login has succeeded
End If
End Sub
End Class
Here are presumptions on your code, I'm guessing you added the code about on the Form.Load event. The Form.Load event only gets raised when the form is shown for the first time.
According to MSDN
Form.Load Event
Occurs before a form is displayed for the first time.
And now, when you Log-Off, you're setting the visibility of the form to false. So what I suggest is you move your code from the Form.Load event to the Form.VisibleChanged event.
According to MSDN
Form.Load Event
Occurs when the Visible property value changes.

Visual Basic SplashScreen not working with Version Select Form

I'll try and keep this brief without leaving out details. I'm working in Visual Stuido 2012 using .NET 4.5
I have a Splash Screen, a "version selection" form as my main startup form, and then from there it branches out in two ways based on the user's choice.
The Version Select can save the user's choice for the future, and checks if they have a saved setting, and if they do it skips the selection form and goes straight to their version. The problem I'm encountering is that when the user has a saved version, the splash screen remains up and never closes unless I force it to.
I tried using the MinimumSplashScreen time in the application events but that hasn't helped. This only happens if the user has a version saved.
Any thoughts on this? I can post more details as needed. Thanks in advance
From Comments
Private Sub Version_Selection_Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.VersionSelected = "OSRS" Then
'Code to close initial form and load old school
Dim OSmain As New OldSchoolMain
OSmain.Show()
Me.Close()
ElseIf My.Settings.VersionSelected = "RS3" Then
'Code to close intital form and load RS3
End If
End Sub
Private Sub btnConfirmSelection_Click(sender As Object, e As EventArgs) Handles btnConfirmSelection.Click
If radOSRS.Checked = True Then
If cboxSaveVersion.Checked = True Then
My.Settings.VersionSelected = "OSRS"
End If
Dim OSmain As New OldSchoolMain
OSmain.Show()
Me.Close()
ElseIf radRS3.Checked = True Then
If cboxSaveVersion.Checked = True Then
My.Settings.VersionSelected = "RS3"
End If
Dim RS3main As New RS3Main
RS3main.Show()
Me.Close()
End If
End Sub
Looking at your code, I think the problem with your Splash Screen is that you are closing the startup form before it finishes. I would try using the Form Shown event since that doesn't get fired until the SplashScreen has finished, I also am Minimizing the form to try to keep it from flashing briefly on the screen. See if something like this works for you.
Private Sub Splash_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
If My.Settings.StartupForm = "Form1" Then
Dim frm As New Form1
frm.Show()
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Close()
ElseIf My.Settings.StartupForm = "Form2" Then
Dim frm As New Form2
frm.Show()
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Close()
End If
End Sub

Remove the windows form controls on exit

I'm adding the form controls on loading the form manually:
Me.FieldI = New TextBox()
Me.FieldI.Location = New System.Drawing.Point(50, 10)
Me.FieldI.Name = "FieldI"
Me.FieldI.Size = New System.Drawing.Size(40, 20)
Me.FieldI.TabIndex = 5
Me.Conversion.Controls.Add(Me.FieldI)
[..]
When I close the form window and reopen it, the control is still there (with the old .Text content , because its an textbox in this case).
I would like to remove the controls that have been created while form loading on the form close event, to prevent doubling the elements on my form.
How can I achieve this?
edit
Form closing code looks following (just showing up the main form back):
Private Sub Form1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
Main.Show()
End Sub
The problem here is that the form is not being disposed, so when you open it again the controls are still there from the last time it was opened.
Try the following:
Using frm = New subForm()
frm.ShowDialog()
End Using
The variable frm will be disposed after the using.
Also...
You can also provide feedback from a dialog, to check whether the form was successful or not. For example:
Dim frm As New subForm()
If frm.ShowDialog = DialogResult.OK Then
'YAY!
Else
'Something failed
End If