Set MdiParent from a ShowDialog form - vb.net

I've got a form, frmEmail which is called using f.ShowDialog (Where f is an instance of frmEmail).
How can I, when closing the form, open an instance of frmOrder but set fOrder.MdiParent = frmMain?
frmMain is my MDI form, frmOrder is a form I need to open, and frmEmail is the form I'm opening it from.
My code at the moment is
revoke = True
Dim f As New frmOrder(con, False, False, , orderNum)
f.MdiParent = ' Not sure what to put here?
f.Show()
Me.Close()
Do I need to pass in a variable that stores reference to frmMain? Is there another way to do it?
Obviously I can't use f.MdiParent = Me.MdiParent, because as I mentioned before, frmEmail is opened using f.ShowDialog so has no MdiParent.

You can still open a form in ShowDialog and set the "Owner" property.
Where you're opening frmEmail, instead of opening it like using f.ShowDialog(), use f.ShowDialog(Me.MdiParent) (If your MDI form is the parent of the form where you open frmEmail, otherwise just tweak it as relevant).
That way, when opening frmOrder, you set the MdiParent property to be frmEmail's owner property.
Dim f As New frmOrder(con, False, False, , orderNum)
f.MdiParent = Me.Owner
f.Show()

Related

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.

When user submits a form how can I make it close automatically?

this my code to open addUserform
Dim adduser As New frmadduser
adduser.MdiParent = Me
adduser.Visible = True
when user open ChangePassword form
Dim changepassword As New frmchangepassword
changepassword.MdiParent = Me
changepassword.Visible = True
I want to ask, how to close current form automatically after user open changepassword form. Iam use me.close, that's code not work.Thankyou
How you will close current form? if you are dealing with MDI Parent form , then it will not close. please make sure that which form you wanna close
for example if you are on Form1 and you want to open Form2 then you can do following steps.
form2.Mdiparent=MDIParent
form2.Show()
Me.Dispose()

Pass data between dialog form

i want to pass data from my FrmArmy to FrmMain and i use login form to open FrmMain..
Me.Hide
Dim FMain As New FrmMain
FMain.ShowDialog()
then i open FrmArmy from FrmMain..
Dim FArmy As New FrmArmy
FArmy.ShowDialog()
BtnArmy.Enabled = False
and what i want here is.. how to change BtnArmy in FrmMain to
BtnArmy.Enabled = True
when FrmArmy Closed
This Code Does not working..
FrmMain.BtnArmy.Enabled = True
Thanks.
FrmMain is a class, you need to disable it on the instance FMain
Dim FArmy As New FrmArmy
Me.BtnArmy.Enabled = False ' disable on show
FArmy.ShowDialog()
Me.BtnArmy.Enabled = True ' reenable after it hides (?)
We have no context, I assume this is in a Button Click event. This may not be needed, because they cant click stuff on other forms while the dialog is open, anyway.
But you have another issue: Dialogs are resources which are not automatically disposed. If these are called a lot, you could leak resources:
Using fArmy As New FrmArmy
Me.BtnArmy.Enabled = False ' disable on show
FArmy.ShowDialog()
Me.BtnArmy.Enabled = True ' reenable after it hides (?)
End Using ' auto dispose of Dialog

Unable To Close A Form Embedded In A Panel

I have an issue that is really starting to do my head in...
My application has a ListView control that is populated with items. When you double click on an item it creates a new instance of a form. It then creates a new panel and adds the form to the panel. However, for the life of me I cannot work out how to close the form inside the panel.
Inside my DoubleClick event:
Dim frm As New frmStorePage(_store.Code, _store.Name)
'Create a new panel with the store page
Dim pnl As New Panel
pnl.Name = _store.Code
pnl.BackColor = SystemColors.Control
pnl.Size = New Size(1522, 892)
pnl.Location = New Point(3, 3)
frm.TopLevel = False
frm.Name = _store.Code
pnl.Controls.Add(frm)
frm.Show()
pnlStores.Controls.Add(pnl)
pnl.BringToFront()
...
Inside my Close event:
Dim panel As Panel = CType(pnlStores.Controls.Find(lsvOpenStoreList.SelectedItems(0).Name, False)(0), Windows.Forms.Panel)
For Each control As Control In panel.Controls
If TypeOf control Is Windows.Forms.Form And control.Name = panel.Name Then
control.Dispose()
End If
Next
pnlStores.Controls.Remove(panel)
panel.Dispose()
pnlStoreList.BringToFront()
...
I have also tried declaring my form as a global variable but still cannot seem to close it.
The form has a number of timer events that get stopped when the form closes, however, even though the panel gets closed, the timer events are still running. It seems that the form is still active in the background.
Any help would be appreciated.
Due to a silly oversight, I failed to see that the code I was using to dispose of all the controls and close the form was not in the FormClosing event, but actually under a Command Button called Close. Therefore, my closing procedures were never getting called and thus the timers remained active.
Thanks

Getting a Modal pop form to show up in Access

i'm trying to use the following code to open a Form using VBA
Private Sub cmdEdit_Click()
Dim d As New Form_EditNote
d.txtDate.Value = EntryDate.Value
d.txtNote.Value = Note.Value
d.Visible = True
End Sub
the form opens but only for a split second .. then goes away... any ideas?
You are declaring your variable d, assigning it to your form, you make the form visible, then your function is over and all your local variables are destroyed. Your form is one of these local variables. You need to use DoCmd.OpenForm EditNote. That will keep it open
To make it modal, either set the modal porperty to true at design time if you always want it to be modal. Or in DoCmd.OpenForm set the WindowMode to acDialog