How do I Go To Original Form when I receive an Error? - vb.net

I am working to create it so when the application cannot find the file that the user selected, it will go back to the original form (where it was selected - frmLetters). So I am using a Try, Catch block but it just continues down the sub procedure.
I need it to stop the current sub procedure and go back to the original form for the user to choose the correct file. This is the code I am trying:
Try
objExcelBook = objExcelApp.Workbooks.Open(sInputExcelDataFile,, True)
Catch ex As Exception
MsgBox("Are You Sure you chose the correct file path?")
Me.Close()
frmLetters.Close()
frmLetters.Show()
End Try
I am much more familiar with VBA but the syntax is similar in most cases. I know try, Catch doesn't exist in VBA but if I wanted to return to the original form it is very simple.
Just so you know the reason I am closing frmLetters before opening it, is at this point it is only hidden.

First of all Create this method in your MDIParent. This should be the way that you display the form inside the MDI Parent. You should display your original form this way as well instead of using show.
Public Sub DisplaySubForm(ByVal f As Form)
For Each currentForm As Form In Me.MdiChildren
If f.GetType Is currentForm.GetType Then
currentForm.Activate()
Return
End If
Next
f.MdiParent = Me
f.Show()
f.WindowState = FormWindowState.Normal
f.BringToFront()
End Sub
To Return the form, you simply can do like (you can do it on your form)
MDIParent1.DisplaySubForm(frmSomething)
To close the form, you use
me.dispose

Related

Vb.net Trying to close a form opened in a Panel. But on closing it, the Parent from gets close/hide totally

I tried all options on google. But they show only the form.Close or form.Hide for each loop. I also have implemented same loop as follows:-
(I am adding the form into Panel (Panel_Details_Container)
For Each form_obj In Panel_Details_Container.Controls.OfType(Of Form).ToList()
form_obj.Close()
Next
Try
'-----------------------------------------------------------------------------------------------
Me.Cursor = Cursors.WaitCursor
'-----------------------------------------------------------------------------------------------
Dim Form_DE_Details_Obj As New Form_DE_Details()
'-----------------------------------------------------------------------------------------------
Form_DE_Details_Obj.ControlBox = False
Form_DE_Details_Obj.Text = String.Empty
'-----------------------------------------------------------------------------------------------
Form_DE_Details_Obj.TopLevel = False
'-----------------------------------------------------------------------------------------------
Form_DE_Details_Obj.TextBox_Selected_Unique_ID.Text = Selected_Unique_ID.ToString()
Form_DE_Details_Obj.TextBox_Selected_Imp_ID.Text = Selected_IMP_ID.ToString()
'-----------------------------------------------------------------------------------------------
Form_DE_Details_Obj.WindowState = FormWindowState.Normal
Form_DE_Details_Obj.StartPosition = FormStartPosition.WindowsDefaultLocation
'-----------------------------------------------------------------------------------------------
Me.Cursor = Cursors.Default
'-----------------------------------------------------------------------------------------------
Form_DE_Details_Obj.Visible = True
Panel_Details_Container.Controls.Add(Form_DE_Details_Obj)
Form_DE_Details_Obj.Show()
'-----------------------------------------------------------------------------------------------
Catch ex As Exception
MsgBox(ex.Message)
End Try
'-----------------------------------------------------------------------------------------------
Me.Cursor = Cursors.Default
'-----------------------------------------------------------------------------------------------
My Issue:-
1. The .Close worked for all forms which had less controls.
2. This particular form has lot of controls each 20 rows :-7 ComboBox, 3 TextBox, 3 checkbox, 1 button. So is it any memory issue? That closing and immediately reloading the form instance, parent form gets close automatically?
3. I confirmed this that the problem is of this form only by passing the other forms worked smoothly and whenever this form is called the parent form gets closed.
4. In title I wrote close/hide because I am not aware whether the parent form get close or hides. As after Parent form get close/invisible, the wait mouse cursor runs or a process on breakpoint stops at code side. But the parent as well newly created form are not at all available.
(These are few tips I am placing which I tried since now complete week)
Purpose of code:-
1. A lot of controls are saved on one form. And when needed the appropriate
form with restricted controls to that module only are loaded.
2. Due to splitting the controls to other individual forms, lot of memory is reduced.
My Workouts on resolving the issue already:-
1. Tried .Hide already. But this creates reloading a new form object instance every time and new instance for form along with its all objects are in memory. I tested this by giving breakpoint at from_obj.Hide() and everytime a new form instance count gets added in loop.
2. .Dispose also did not helped out.
3. I tried Application.DoEvents (to check if it reqires too much time to close form resources). But no resolution.
Thank you very much in advance to you all.

VB.Net Handling Multiple Forms into Panel

I have tried to find an answer to this already, but cannot find one that answers this question.
I have a Master Form which contains two panels. In the master Form I am trying to write a subroutine to handle the loading of a form into one of the panels.
One panel always contains the same form and the code which works for this is:
'Configure Toolbar Import
Dim toolbarHandler As _pnl_header = New _pnl_header()
toolbarHandler.Size = pnlHeader.Size
toolbarHandler.TopLevel = False
pnlHeader.Controls.Add(toolbarHandler)
toolbarHandler.Show()
The panel successfully shows the form _pnl_header as expected.
The second panel will change the displayed form depending on user input, so rather than having to write the above code for every eventuality i would like one Public Sub to handle them all...
I've started writing a sub along the lines of:
Public Sub LoadContentPanel(WhichForm As Form)
Try
Dim contentHandler As WhichForm = New WhichForm()
contentHandler.Size = pnlContent.Size
contentHandler.TopLevel = False
pnlContent.Controls.Add(contentHandler)
contentHandler.Show()
Catch ex As Exception
MsgBox("Unable to Handle Content Panel Change. Error: " & ex.Message, vbOKOnly + vbCritical, "Load Error")
End Try
End Sub
However this fails as 'WhichForm' is not defined - how is best to correct this? or is there a better alternative?
Thanks
Without going into what you are doing I can explain where the error comes from.
Here you declare argument variable WhichForm of type Form
Public Sub LoadContentPanel(WhichForm As Form)
. . . . .
Code is incorrect in the next declaration line. WhichForm is a variable and not a type. Hence
Dim contentHandler As WhichForm = New WhichForm()
is invalid at As WhichForm. Because after As you need a type name. If you did
Dim contentHandler As Form = New Form()
it would work.
It seems that all you need to do is remove Dim contentHandler As WhichForm... and rename argument WhichForm to contentHandler.

access make list field visible when clicking Button

In an access form, I try to make a list field visible when the fokus is on another textfield in which new data should be filled in. The backround is that one should know the last data inputs to create a new one.
As a first step I tried to make the list (liste91) visible when clicking on a button, but I failed using the following code.
Private Sub Befehl97_Click()
Forms!projects!liste91.SetFocus
Me.liste91.visible = True
End Sub
I get error in the line Me.list91
what is wrong?
thank you for your help!
You can't set focus to something not yet visible. Just switch the order:
Private Sub Befehl97_Click()
Me.liste91.visible = True
Me.liste91.SetFocus
End Sub

How to stop a DataGridView removing columns on subsequent form loads? [duplicate]

I have a custom form which is open as Form.ShowDialog()
This form acts as a confirmation form. It asks a question whether you want to accept or decline the previously entered input in ComboBox & TextBox.
If you click OK, the input is saved into Excel File.
If you click Cancel, the input is not saved.
The problem I am having is that:
When you click cancel. The form.ShowDialog() is closed. (Which is fine.)
But when the form.ShowDialog() is open again. It retains the focus on the Cancel Button. So if you try to confirm the entry with "Enter" key, you cancel it instead.
My question is. Why does the Form.ShowDialog() retain the focus on the buttons after closing?
The Form.ShowDialog() has accept button "OK" [tabindex = 1], and cancel button "Cancel" [tabindex = 2] which are set to Enter key, and Esc key.
(To note again)The focus of the buttons remains after closing the form.
The portion of the code using the Dialog:
ElseIf ComboBoxBP.SelectedItem = ComboBoxBP.SelectedItem And TextBoxBP.Text = TextBoxBP.Text Then
form.Label1.Text = ComboBoxBP.SelectedItem
form.Label2.Text = TextBoxBP.Text
form.ShowDialog()
If form.DialogResult = Windows.Forms.DialogResult.Yes Then
SiE()
ElseIf form.DialogResult = Windows.Forms.DialogResult.No Then
LabelBPBot.Text = "Canceled."
End If
End If
When you use .ShowDialog() closing the form does not dispose of it as with a normal form. This is because once a Dialog "closes" it actually just hides so we can get info from it before it actually goes away.
The second issue is that forms are classes (it says so at the top of every one of them:)
Public Class Form1
...
So, instances of them should be created. VB allows Form1.Show or Form1.ShowDialog() to use a "default instance" and it is a shame that it does.
Combine these 2 tidbits and what you have is a case where the form you showed last time is still around in the same state as when you last used it, including the last focused control. You are only using a "fresh copy" of the form the first time, after that, you are just reusing the old instance. Remedy:
Using Dlg As New Form1 ' form1 is the class, dlg is the instance
... do stuff
Dim res As DialogResult = Dlg.ShowDialog()
If res = Windows.Forms.DialogResult.OK Then
'... do stuff
End If
End Using ' dispose of Dlg
Eventually, you will run into similar issues using the default instance of the other forms (LForm.Show). Just Say No to Default Form instances.

I can't show a form that isn't top level with form.ShowDialog()

My startup form is a modal security form which works fine. But, if the user "logs out", the security form must be displayed again as a modal dialog. This last step is where everything goes wrong. It shows the form, in front of my other forms, but it's not modal...
First, I call a method that's written in a module, because I have to be able to call this method from every form I want.
Public Sub CallWaiterKey()
Dim oForm As frmWaiterKey = New frmWaiterKey()
Try
If mWaiterKey.Length > 0 And mWaiterKeyType.Length > 0 Then
If Convert.ToInt32(mWaiterKey) > 0 And Convert.ToInt32(mWaiterKeyType) = 2 Then
oForm.TypeOfKey = 2
ElseIf Convert.ToInt32(mWaiterKey) > 0 And Convert.ToInt32(mWaiterKeyType) = 1 Then
oForm.TypeOfKey = 1
End If
'here it goes wrong
oForm.ShowDialog()
End If
Catch ex As Exception
MsgBox(ex)
End Try
End Sub
When I call oForm.ShowDialog() (that's the frmWaiterKey), it comes up but isn't modal.
I can still click the buttons that are placed on frmMenu, the form from which I called CallWaiterKey().
Am I doing something wrong here?
Or should I make the call in an other way?
(My VB sucks so ignore syntax errors)
To achieve what you are asking, specify the hosting form.
Public Sub CallWaiterKey(ownerForm as Form)
Dim oForm As frmWaiterKey = New frmWaiterKey()
' ....
'here it goes wrong
oForm.ShowDialog(ownerForm)
' ....
End Sub
I don't use ShowDialog; but I believe that you need to specify the window owner to enforce the modality. If I'm wrong here, others will correct me.
oForm.ShowDialog(me)
** HOLD ON ** I will alter this in a second, I just recalled that you're calling from a module, me doesn't evaluate in a basic module.
Here is a MSDN reference