Get the last open form - vb.net

I want to know the name of the form before the last and the last form Name that open..
MY CODE TO CLOSE ALL
For Each Form In My.Application.OpenForms
Form.Close()
Next

form before the last and the last
Forms are added to the OpenForms collection in the order that they are opened
This means you can make a shortcut of getting the last 2 with a bit of LINQ:
Dim forms = My.Application.OpenForms.Reverse().Take(2).ToArray()
Now the last form is in forms(0) and the beforelast form is in forms(1)

Related

MS Access: Combobox Truncating Leading Zeros when using DLookup

I am attempting to set the value of a combobox via a DLookup call, that is inside of a related textbox's Change sub.
The issue I am having is that during the DLookup call, the leading zero that is present on many of my strings (document ids) that I am searching for is being removed from the combobox, prior to calling its own Change sub.
My code looks like this:
Private Sub txtImport_Change()
Dim id As String
Me.txtImport.SetFocus
id = Me.txtImport.Text
Me.cboId.SetFocus
Me.cboId.Text = DLookup(id,"DocumentTable")
cboId_Change
End Sub
For background,
I am doing this to auto-populate a new form based on a value selected on a previous form. Form #2 is going to be an in-depth informational form about a specific document that is selected on Form #1.
The driving control on the new form (which can be done manually, if the user chooses to skip Form #1), is via the combobox: cboId's selection. I would like to keep this functionality for the below use-case.
So, in the use-case where a user is utilizing Form #1, I am trying to set cboId's value on Form #2 via a DLookup on a string that is being passed between the two forms (the docId selected in Form #1 is sent to txtImport on Form #2 when that form loads after a button press).

vb.net Move between two form with option

at first i am sorry for my weak english but i will try my best to explain my issue
i am new in programming so i got idea but i don't know if can be happen or not
the idea is , i have 2 form separated each form have there cods , but i want to add button in form one its open the second form, to add thing to the second one , but i need to return to first form when i close the second form
can i do something like that ??
for clarification the second form i can open it from other ways and have own function and the first form too
UPDATING !!!
at first thank for all your answers and not useful vote ,second yes i am new but not that much i know how to use formname.show() and formname.close() etc..... ,the issue not about usual ways to open and close forms, for example in form one i input information about user one of the option is where he live, the country choosing from dropbox ,next to the dropbox there button named add new country the button open new form to add new country when i add new country and closed the form back to the first form , and just to be know there button in the third form (setting) to open the form add new country ,IN SHORT WORD I DON'T NEED TO OPEN THE FIRST FORM EVERY TIME I CLOSE THE SECOND ONE , ONLY RETURN TO THE FIRST FORM IF I CAME FROM THE BUTTON THAT I CLICKED IN FORM ONE
put this on action
formname.show() 'for opening a new form
formname.close() 'for opening closing the form
formname.showdialog() 'Where you cant click the main form``
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'show form 2
Form2.Show()
'move text from one form to another
Form2.TextBox1.Text = TextBox1.Text
End Sub

How to automatically close the dialog box?

I have a WinForm application that have an automatic logout after some time without activity performed by the user, that application have some dialog box like
SearchCustomer.ShowDialog()
If the user left that dialog opened and the time expire the user is automatically logged out but that dialog box remains opened, so anybody can use that dialog even with the user logged out.
There's any way to close those dialog box from main Form?
Edit:
There's another Dialog box opened in a different way
AddCustomer.Show()
AddCustomer.BringToFront()
Edit 2 Solution based on jmcilhinney answer
For Each openForm In openForms
Dim H1 As Integer = openForm.GetHashCode()
Dim H2 As Integer = Me.GetHashCode()
If H1 <> H2 Then 'No igual
openForm.Close()
End If
Next
I haven't tested but I would think that you should be able to do something along these lines:
Dim openForms = My.Application.OpenForms.Cast(Of Form)().ToArray()
For Each openForm In openForms
openForm.Close()
Next
Here is an option.
Open your form with the ownership attribute.
AddCustomer.Show(Me)
'This open the form and gives the referring form ownership.
'It Also gives focus to the child form, keeping it on top of the referring form
When the referring form closes, so does the child form.

MS Access Copying value from a form into another form text box

Here's my problem: I have the below code which copies the employee username from FormB and puts it in FrmEmployee when i click on a record in FormB (which is opened through a cmd button on FrmEmployee and lists all employees in the company and their shift pattern).
Private Sub Form_Click()
Forms!FrmEmployeee.Form.Username = Me.Username
End Sub
This works but it will only set the current username field to what is selected on FormB. FormB is in a datasheet view and will hold multiple values for the employee username. So what I want to do is expand the above code somehow so it takes me to the next record on FrmEmployee which means when i select another user from FormB this value will be copied into the next Username record, allowing me to build a list of employees.
From my understanding, what you need actually is to store multiple line/items from your FORM B.
So my suggestion is changing your textbox on FrmEmployee into a ListBox. So everytime you out the cursor on the active record, it add the record into the listbox

Referencing cell values on DataGridView from another form

This is bugging me. I have a "main" form named frmMain with a DataGridView object named objDataGrid.
When a user double clicks on a row or clicks another button, I hide the main form, open a new form and want to reference the values in the row selected by the user but I keep getting an error when I try to access some, but not all, of the datagridview's properties.
For example here is the code that opens the form:
On Error Resume Next
Me.Hide()
frmGenerate.Show()
In the frmGenerate's load event I do the following:
Dim frmMain As frmMain = Nothing
frmMain = New frmMain
Any time I try to grab a value from the grid I get various errors.
frmMain.objDataGrid.Item(1, 2).Value
frmMain.objDataGrid.Rows(2).Cells(4).Value
frmMain.objDataGrid.SelectedRows(0).Index
frmMain.objDataGrid.Item(1, frmMain.objDataGrid.SelectedRows(0).Index).Value
These return index out of range errors even though I know for sure the indexes are correct.
Any ideas?
Thanks.
Why are you creating a new instance of frmMain? You need to refer to the existing one where they've selected a row.
You could pass just the row of the datagrid or PK field values instead of referencing the other form.