How to automatically close the dialog box? - vb.net

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.

Related

Call a windows form dynamically by its title in vb.net

I'm stuck with something that I have tried to overcome before and couldn't.
Last time I worked around it, but I really need to nail this down.
I have a multi-instance form which lists details of jobs, so a user may have 6 or so instances of this form open, all with different info. Lets call that form A.
Within form A there is an option to assign that job to a user.
Clicking that link label opens a new form using showdialog where a combobox is populated with logged in users... Lets call that form B.
What I want to do is have the value selected on form B passed back to form A, however because form A has multiple instances, I'm not sure how to call it.
Form A is JobDetails.vb, and always has a form title of "Job XXXXX Details", so I was hoping there was a way I could use the title to call it.
Hope that all make sense! Thanks in advance, and apologies if its a dumb question. I feel like this should be something obvious, but I just can't achieve it.
As shown on the help page of ShowDialog, you can easily alter the main form when the dialog-form is closed. So you could do something like this:
Public Sub ShowMyDialogBox()
Dim testDialog As New Form2()
' Show testDialog as a modal dialog and determine if DialogResult = OK.
If testDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
Me.Text = String.Format("Jobs of {0}", testDialog.cmboUsers.SelectedValue)
Else
Me.Text = "Jobs"
End If
testDialog.Dispose()
End Sub

VBA Executing Sub Oddly

I have made this simple invoice control worksheet in excel and I use VBA to make it easier to visualize and add new items. I have made dozens of other VBA programmed Worksheets, and all of them have a "New" active x button, programmed just like this:
Private Sub ButtNew2_Click()
Dim Guia As Worksheet
Dim UltLin As Integer
Set Guia = Workbooks("Notas Fiscais.xlsm").Worksheets("Saída")
UltLin = Guia.UsedRange.Rows.Count + 1
Guia.Application.Goto Reference:="R" & UltLin & "C1"
FormNotasSaida.Show
FormNotasSaida.BoxDataEmiss.SetFocus
End Sub
Simple as that. Just select the first blank line so that the form loads blank. It works fine in any other Workbook. But in this one, if and every time I click this button, after closing the form, the next time (and only once) I load the form again in any possible way (either double clicking an item, pressing the "Show" button or pressing the "New" button again), it loads either blank or showing the last launched item (case you did so).
After closing it, I can click wherever or press the "Show" button whenever, they work fine, as they always have. The problem occurs exclusively once, after pressing the "New" button.
What am I possibly doing wrong, specially knowing that this method works perfectly in all other workbooks?
FormNotasSaida.Show
FormNotasSaida.BoxDataEmiss.SetFocus
Forms are a special kind of class modules with a designer and a predeclared ID attribute. This "predeclared ID" is what's at play here: you're using/reusing the default global instance of the class, and [unless you close it with the X button in the control box,] never unload it so you're always showing the same form.
A best practice is to create a new instance of the form every time you use it:
Set frm = New FormNotasSaida
frm.Show
frm.BoxDataEmiss.SetFocus

How to launch a Look up form, and return the value from a combo box

Hopefully I can explain what I want to do well enough...here it goes...
I have a data entry form...the user will be entering employeeIDs. Once in normal operation, most people will be entering only their own EmpID, and they should know it, so this won't be a big problem 99% of the time once this DB goes live.
However, I need some temps to enter historical data from paper sheets into the DB. These people will not know anyone else's EmpID. I'd like to set the Student field's OnDblClick event in the subform's datasheet to open a small form with a combo box. The combo box has a list of all Employee Names, and is bound to the EmpID. Once this user enters the name, and selects the person, I have a button they can click to return to the datasheet.
I can use a function to launch the form, no problem there. But how do I return the EmpID to the field in the datasheet that was double clicked?
When user double clicks in the Student field...I want the next form to appear, and then once they type in the name and select the correct person...and then click Found Them!...I need that bound value to return.
I'd love to say I have code to share right now...but the only code I have is to launch the look up form. I'm brain farting on how to pull the value back down.
The way to do this to launch your little dialog form as “acDialog”. This will cause the calling code to WAIT.
And then the “magic” part is when they click on “Found Them” you do NOT close the popup form, but simply set the form’s visible = false. This has the effect of the calling code that popped up this form that halted to continue (the form is kicked out of dialog mode when you do this). So now your calling code continues.
So your code will look like this:
Dim strF As String ' name of popup form
strF = "frmPopUp"
' open form, wait for user selection
DoCmd.OpenForm strF, , , , , acDialog
' if for is NOT open, then assume user hit cancel buttion
' (you should likly have a cancel button on the form - that cancel buttion will
' execute a docmd.close
If CurrentProject.AllForms(strF).IsLoaded = True Then
' grab the value of thee combbo box
strComboBoxValue = Forms(strF)!NameOfComboBox
DoCmd.Close acForm, strF
End If
As noted, the code behind the Found Them button DOES NOT do a close form, but sets forms visible = false (me.Visible = false), and this trick allows the calling code to continue at which point you can examine any value on the form. Remember to then close the form after you grab the value.
It looks like your data table is in a subform so there is a little more work but it does not have to be as complex as the above solution if you don't want it to be. #Andre451 was close but you need the extra step of identifying the form and subform. For the purpose of demonstration let's call the form Attendance and subform Entry then I'll call the second form LookUp. So the code for your double click in the subform field will of course look something like this :
Private Sub Student_DblClick(Cancel As Integer)
DoCmd.OpenForm "LookUp"
End Sub
You really don't need anything else fancy there. For the button on "LookUp" you will put this:
Private Sub Command2_Click()
Forms![Attendance]![Entry].Form![Student] = Forms!Lookup!Student
DoCmd.Close acForm, "LookUp"
End Sub
And that should get you what you want without any overhead or having to leave any ghosts open.

How to get window (modal/nonmodal) such is "Find and replace"

Can't find how to get new window (form) which will behave like "Find and replace" window in VB IDE 2008, where window is allways on top and I can work on it but I can also work with underlayed code bit find and replace don't hide when I set focus to code window.
The best solution will be if I would be oopen more than one such window.
This is how I try but opened window is modal!
Dim fl As New myWindow
With fl
.StartPosition = FormStartPosition.Manual
.aCallerLocation = Me.Location
End With
Dim ret As Integer = fl.ShowDialog(Me)
fl.Close()
fl = Nothing
Showing the form as a dialog form, is not necessary to make the form stay in front of the primary form. Using the ShowDialog method causes the form to be modal. What makes it stay in front is the fact that you are passing Me for the owner parameter. You can still pass an owner form, even if you are just calling the non-modal Show method:
Dim fl As New myWindow()
' ...
fl.Show(Me)
That way, the new form will stay in front of the primary form, but it will not be modal. Therefore, both forms will be usable and you can show as many of those non-modal child forms in front of the primary form as you like.

Values is not appearing in Current active Form

Using VB.Net (Windows Application)
I have one main form(Data entry form), i creating many form at run time.
Code for creating a multiple form at run time.
Button1 click
If IsNothing(frm) OrElse frm.IsDisposed Then
newfrm = New frmEntry
End If
newfrm.Show()
I have popup windows for selcting the value in the Data entry form.
Code for selcting the value from popup windows
Popup Window code
If e.KeyCode = Keys.Enter Then
frmEntry.txtbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End If
The above popup window code is working for Data Entry Form, but it is not working for new forms (at run time)
When i select the value from popup windows means, it is appearing in frmentry textbox, not in newfrm textbox.
Popup windows selected value should appear in current active form.
What wrong in my code.
Need VB.Net Code Help
If the form you open the popup from is what you need to change values in, have you considered passing a reference to the opening form to the popup when you open it? So that you have direct access to the form that has the controls that will need updated?
This constructor in the popup window:
Private mOpeningForm As frmEntry
Public Sub New(OpeningForm As frmEntry)
InitializeComponent()
mOpeningForm = OpeningForm
End Sub
This modified to use the reference to the form sent to the popup form:
If e.KeyCode = Keys.Enter Then
mOpeningForm.txtbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End If
This in the form when the window is being created at runTime:
If IsNothing(mEntryForm) OrElse mEntryForm.IsDisposed Then
mEntryForm= New frmEntry(me)
End If
mEntryForm.Show()
At the top level of the Data Form Class (The one creating the popups)
private mEntryForm as frmEntry
That will allow you to have a reference to the Instance of frmEntry from anywhere in the data form class. (Note that I changed the name of the popup form instance in for the button click event code too)