Launching a MDI menu option with Enter key fires KeyUp in MDI Child form - vb.net

Using Visual Studio 2012
Added an MDI Form to project which creates a bunch of default menu items and then added the following code for the File/Open menu item:
Public Class MDIParent1
Private Sub OpenFile(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click, OpenToolStripButton.Click
Dim frm As New Form1
With frm
.MdiParent = Me
.Show()
End With
End Sub
End Class
In another form, Form1, set KeyPreview = True and put in this code:
Public Class Form1
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.Enter Then
MsgBox("enter pressed")
End If
End Sub
End Class
Now, if you launch the MDI form and click the File menu (or ALT+F) then scroll down using arrow keys to Open and hit Enter on the keyboard, it launches Form1 but fires KeyUp(). In my actual application this ends up launching another child form to Form1 and I need to suppress the Enter key from the MDI menu launch but can't figure out how to do that.

Handle KeyPress or KeyDown instead of KeyUp.

Related

Child Forms and Parent forms mismatch in Vb.net

I have a parent form called MdiForm1 within it I open frm1 which is a child form. SO everything is great at this point - now I need to open another form within frm1 - lets call that frmX and here's where my issue arises - I previously had declared it as mdichild form, and did mdichild.show - however the issue comes up because when this form opens (it covers about 1/3 of frm1 - which is already open) and user clicks outside of the frmX - it simply disapears. So I tried to .showDialog() however am unable to do that because it's not Top level and is a mdiChild therefore won't let me .showdialog(). here's my code...
Private Sub cmd1_Click(sender As Object, e As EventArgs) Handles cmd1.Click
Dim NewMDIChild As New Frmx()
'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = MDI1
'Display the new form
NewMDIChild.ShowDialog()
NewMDIChild.Top = 310
NewMDIChild.Left = 36
NewMDIChild.Width = 897
End Sub
I get this error on .showDialog() and here's what it says....
Form that is not a top-level form cannot be displayed as a modal
dialog box. Remove the form from any parent form before calling
showDialog.
I tried to declare the frmX like this....
dim frmX as New Form
frmX.showDialog
'specifying Top/Left/Width but that doesn't do anything, basically opens an empty form elsewhere on the screen.
EDIT:It's a little confusing :/
This is what I Did - getting the same error. This is in frm1 on button click which is suppose to OPEN frmX in modal so that users clicking on frm1 will not make frmX disappear. It opens in the right location however when I click elsewher on frm1 --- frmX disappears
Dim frmxName As New FrmX()
frmxName.MdiParent = Me.MdiParent
frmxName.ShowDialog()
frmxName.Top = 310
frmxName.Left = 36
frmxName.Width = 897
My goal is to have frmX open until they click close on it!
Set the new form's MDI parent to the controlling form's MDI parent
In the MDI parent I called Form1. This form has the property IsMdiContainer = True
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myFrmX As New frmX
myFrmX.MdiParent = Me
myFrmX.Show()
End Sub
End Class
And in frmX with a button on it
Public Class frmX
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New Form
f.MdiParent = Me.MdiParent
f.Text = "frmY"
f.Show()
End Sub
End Class
Clicking the button creates new Forms, which are shown to be MDI children of the main form below
Or if you just want a dialog window, forgo the MDI business, and just show dialog
Public Class frmX
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New Form
f.Text = "frmY"
f.ShowDialog()
End Sub
End Class
Now frmY has focus until it's closed.

VB.Net code in class FormClosing error

I've created a VB.Net application which consists of a form and a series of classes. One class (GUI) contains all of the routines that handle the logic associated with the controls on my form. These controls are placed at design time.
My application runs in the system tray so I need to trap the FormClosing event so that the application is minimised rather than closed. Before moving everything into the GUI class my code worked but I'm now getting an error on this line:
Private Sub Monitor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
which is "Event 'FormClosing' cannot be found"
I'm completely stumped by this. I've changed changing the MyBase parameter to refer to the form by name, leaving it off and also adding:
Private WithEvents Monitor As System.Windows.Forms.Form
but none work.
EDIT: Here's a stripped down version of my GUI class containing the bits that handle minimising and restoring the form to the system tray
Note that there's no reference to the menu bar and menu items but VS doesn't complain about those.
Public Class GUI
' Make form controls accessible to this class (partial list only
' there's a lot of controls)
Private WithEvents tabControl As TabControl
Private WithEvents btn_status_start As Button
Private WithEvents btn_status_stop As Button
Private WithEvents btn_dataSource As Button
' **************************************************************************************
' * *
' * Control form behaviour so that it can be put into and taken from system tray *
' * *
' **************************************************************************************
Private Sub cms_menu_exit_Click(sender As Object, e As EventArgs) Handles cms_menu_exit.Click
' Handles the saving and shutting down of the program
' Save any settings
My.Settings.Save()
' Remove the icon
Monitor.Dispose()
' Finally close the program
Monitor.Close()
End Sub
Private Sub Notify_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles notify.MouseDoubleClick
' Code to restore the program window from the system tray when double clicked
Me.Restore()
End Sub
Private Sub Notify_MouseClick(sender As Object, e As MouseEventArgs) Handles notify.MouseClick
' Code to restore the program window from the system tray when single clicked
Me.Restore()
Monitor.Focus()
End Sub
Private Sub Restore()
If Monitor.WindowState = FormWindowState.Minimized Then
Monitor.WindowState = FormWindowState.Normal
End If
Monitor.Visible = True
End Sub
Private Sub Monitor_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
' Catch the click on the X on the main form and set program to minimise instead
If (e.CloseReason = CloseReason.UserClosing) Then
e.Cancel = True
Monitor.Hide()
End If
End Sub
Private Sub cms_menu_open_Click(sender As Object, e As EventArgs) Handles cms_menu_open.Click
' Maximise the main form if selected
Monitor.Show()
Monitor.Focus()
End Sub
End Class

Load Event of MDi child form not firing?

I have a simple form which calls an external class containing another form in a vb.NET application.
The 2 forms are set up as an MDi parent and child.
Does anyone know why when I call MDIChild.show() in the code of the parent, the load event does not fire in the child form??
Parent Code:
Dim ce As New Policies.Main
ce.MdiParent = Me
ce.Show()
Child Code
Public Sub Main_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
'Do some stuff in load event
End Sub
Right
Following on from the comments above. Open up visual studio and create a simple Winforms project. It will be created with a default instance of Form1.
In the solution explorer right click on the solution and select add and from the menu that appear select Windows form. A new windows form will be created with a default name of Form2.
We are going to treat form 1 as our parent class and form 2 as our child.
Go back to form one and drag a button onto it from the toolbox. Double click on the button once its on the form to open up its default button click handler.
Add the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.IsMdiContainer = True 'we need this so that Form1 can act as a container for other forms
Dim frm As New Form2
With frm
.MdiParent = Me
.Show()
End With
End Sub
Now return to form2. Doubleclick on it to bring up its default load event in the code editor. Add the following code.
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Hi, I'm being shown from the load event of form2")
End Sub
With that done press f5 to run this very simple ( and crude) example. Form1 will load. when you click the button a new instance of Form2 is created. Prior to the form being shown its load event is fired and that triggers te message box to display it's message. You do not need to call the load method directly.

Click a specific button on another form

Consider i am having two forms, form1 and form2
How can i click,mouse over(any events) a specific button on another form using coding in vb.net?
I'm assuming that Form1 launches Form2, since there's not a whole lot of information in the description.
When Form1 launches, there are two buttons: "button1" and "Launch Form 2" (forgot to change text on button1, sorry. :(
When you click "Launch Form 2", Form2 pops up:
Clicking the "button1" on Form1, a message box originating from Form1 pops up saying:
Clicking the "button1" on Form2, a message box ALSO originating from Form1 pops up saying:
Here's the code:
Form1
Public Class Form1
Private WithEvents frm2 As New Form2
Private Sub Form1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1Button.Click
RunSomeCode("Called from form 1!")
End Sub
Public Sub RunSomeCode(ByVal message As String)
MessageBox.Show(message)
End Sub
Private Sub Form1LaunchForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1LaunchForm2Button.Click
frm2.Activate()
frm2.Show()
End Sub
Private Sub frm2_SimulateForm1ButtonClick() Handles frm2.SimulateForm1ButtonClick
RunSomeCode("Called from form 2!")
End Sub
End Class
Form2
Public Class Form2
Public Event SimulateForm1ButtonClick()
Private Sub Form2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2Button.Click
RaiseEvent SimulateForm1ButtonClick()
End Sub
End Class
How it works
Form 2 has a public event called "SimulateForm1ButtonClick". That event can be raised whenever you want, from any code block. I just decided to raise it when I click the button on the form.
Form 1 has an instance of Form2 WithEvents. It's very important that you use the WithEvents keyword, or that public event in Form2 won't show up. :(
Form 1 has a sub that handles the "SimulateForm1ButtonClick" that is raised when Form2 clicks its button.
Now, here's another important detail: The code executed when button1 is clicked on Form1 is actually in a private sub called RunSomeCode(). This is important, because it makes the code accessible from any other part of Form1, namely the part that handles Form2's event.
I hope that helps you out a little bit. I'm not sure exactly what you were asking. :/
Code: http://darin.hoover.fm/code/dl/FormsSandbox.zip
If you are trying to fire the event, just use Form2.Button1.PerformClick() assuming that the button on form 2 is called 'button1'.

VB.NET ShowDialog Form not Ending

I converted this app from VB6. I have 2 forms. Form1 instantiates Form2 via a Menu Item.
I am having trouble getting Form2 to end when clicking close (X). If Form2 is 'idle' it closes fine; but if I am in a loop processing anything all the events fire, but it continues processing in Form2. I've tried messing with Dispose, Close, Application.Exit, Application.ExitThread. My last attempt was creating my own event to fire back to Form1 and dispose Form2 -- and it hits it but Form2 is still running. What is the deal? BTW if I use just Show vs ShowDialog -- Form2 just blinks and disappears.
Form1 does this
Dim f2 as Import
:
Hide()
f2 = New Import
AddHandler f2.die, AddressOf killf2
f2.ShowDialog(Me)
Show()
Private Sub killf2()
f2.Dispose()
f2 = Nothing
End Sub
Form2
Public Event die()
Private Shadows Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Dispose()
Close()
e.Cancel = False
RaiseEvent die()
End Sub
I think you've got your events crossed. You want form1, containing an instance of form2, to listen for form2's form_closing event. Then you can set f2 = nothing.
Form1 should fully enclose form2.
here's an example:
Public Class MDIMain
Private WithEvents _child As frmViewChild
Friend Sub viewChildShow()
_child = New frmViewChild
_child.MdiParent = Me
_child.WindowState = FormWindowState.Maximized
_child.Show()
End Sub
Private Sub _child_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles _child.FormClosing
_child = Nothing
End Sub
don't add anything to form2, try
Dim f2 as Import
Hide()
f2 = New Import
f2.ShowDialog(Me)
Show()
Private Sub f2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles f2.FormClosing
set f2 = nothing
End Sub
re: your comment
it goes back to form2 and continues processing the next statement in the the click event handler
that's a feature and it will cause this behavior. you need to make sure me.close or close me is the last statement in form2, that there's nothing else to execute.
What is this loop you speak of? The user interface (windows) is separate from any code that is running. In your class derived form Form, Code is allowed to run, both before the Form is created, and after the Form is destroyed. If the code tries to access user-interface objects then an exception might occur, but otherwise there is nothing stopping your code from running when there is no user interface.
If you want your "for" loop to exit then you must send it a signal somehow, e.g. by creating a boolean "quit" member variable. Set "quit=True" when your form closes, then have your "for" loop check whether it is true.