Load Event of MDi child form not firing? - vb.net

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.

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 Adding an item to listbox on a different form giving null reference exception

I am getting a an Null reference exception when trying to add an item to a listbox in a different form.
This is my error at run time.
An unhandled exception of type 'System.NullReferenceException'
occurred in ... Additional information: Object reference not set to an
instance of an object.
I am trying to connect the Mainform by initializing it at the top of the class of the secondForm. after I have my data i want to add it to a listbox it the mainform.
Public Class FormHairdresser //The second form
Dim varMainForm As FormMain //connecting the forms ?
Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click
hairdresser = HairdresserChoices(HairdresserID) // get the data
varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.
All i had to to was write the form name instead of ininalised variable.
FormMain.lstListbox.Items.Add("item")
Instead of
Dim varMainForm As FormMain
varMainForm.ListBox.Items.Add("item")
You cannot simply create a new instance of your main form (as has been suggested and expect that to work, you need an actual reference to the mainform that you have created. To help you see the logic involved;
Create a new Winforms project. In the default Form1 add a textbox and a button.
Now add a new form to this application (you can leave it with its default name of Form2. To this form add a TextBox (call it myTextBox) and a button.
Now go back to your first form and doubleclick the button to access the click handler in code. Add the following:
Dim frm as New Form2
frm.Show
Press f5 and click the button and you'll see a new form 2. So far so good.
Now open up the code for Form2 and add the following code so that it ends up looking like this:
Public Class Form2
Private frm As Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frm.TextBox1.Text = myTextBox.text
End Sub
End Class
build, press f5 and click the button on form1, in the new form2 enter some text in the text box and click the button, you get your null reference exception. The reason you get this is because at the moment the private field frm inForm2 refers to Nothing.
Now open up the code in Form2 and add a constructor and the following code so that it ends up looking like this:
Public Class Form2
Private frm As Form1
Public Sub New(byval frm1 As Form1)
'first we should make sure that we have a parameter to play with
If Not IsNothing(frm1) Then
frm = DirectCast(frm1,Form1)
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frm.TextBox1.Text = myTextBox.text
End Sub
End Class
Finally go back to your first forms buttonclick handler and change the code slightly so that it looks like this;
Dim frm as New Form2(Me)
frm.Show
Build and run your application, now when you enter text into the textbox in form2 and click the button it will appear in the textbox in Form1.
The reason why this happens is because you have passed an actual reference to the form1 that was originally created when the application started to form2. By casting that reference to your private field used to represent form1 in form2 you can then use it to properly refer to things on form1. This is a very simple concept but one which you need to learn before you will make progress programming.
the Problem is with your initialization of the formmain.with out proper initialization the object you are creating is nothing other than Null.To avoid this we use New Operator.The New operator can often be used to create the instance when you declare it.
So the initialization will look like
Dim varMainForm As New FormMain
Hope this Helps.For more Reference Object Initialization Errors
update:
Dim varMainForm As FormMain //connecting the forms ?
Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click
hairdresser = HairdresserChoices(HairdresserID) // get the data
varMainForm = New FormMain
varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.
Try This.
Public Class FormHairdresser //The second form
Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click
Dim varMainForm As FormMain
hairdresser = HairdresserChoices(HairdresserID)
varMainForm.lstListBox.Items.Add(hairdresser)

Create And Implement a Custom Form in VB.NET

I am trying to create a customized form class (CustomBorderlessForm) in VB.NET.
My Progress So Far
I created a new Class and named it CustomBorderlessForm.vb
I then proceeded to write the following code:
CustomBorderlessForm.vb
Public Class CustomBorderlessForm
Inherits Form
Dim _form As Form = Nothing
Public Sub New(form As Form)
_form = form
MsgBox("Testing: New()")
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
MsgBox("Testing OnMouseMove()")
End Sub
End Class
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
End Sub
End Class
Results of progress
A message box displays "Testing: New()" on load
Nothing shows on mouse move
As you can see, my problem lies with the events
Questions
Is it possible to create a form object and use that instead of the pre-populating form?
If so, can I give this form custom properties, such as, a border and some boolean values (shadow...etc), just like any other custom object/class?
What am I doing wrong in my current approach?
Why isn't the OnMouseMove being overridden?
Am I initialising the class wrong?
Can it even be done this way?
After creating a form you also need to show it. Change your logic to:
Dim form As New CustomBorderlessForm(Me)
form.Show()
Before you do that, I'd recommend changing from MsgBox to Console.WriteLine(), otherwise you can run into a fun/frustrating little cat and mouse game.
EDIT
Based on the comments, if, from VS you did a "Add New, Windows Form" you can just right-click the project, select property and on the Application tab change the Startup object to your new form. VS only allows you to do this with forms it creates for you (by default, more on this later).
If you wrote that file by hand (which is absolutely fine) you can perform the Show() like I did above and call Me.Hide() to hide the "parent" form. Unfortunately the Load event is fired before the Show event so if you place this in Form1_Load() it won't work. Instead you can use the Shown event like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
form.Show()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Hide()
End Sub
Another option has to do with "Application framework". You can read about it here however it basically handles application events that other languages have to manually implement. If you go into your project properties you can uncheck the "Enable application framework" checkbox. This will give you more option in the "Startup object" dropdown. If you add the following code to your project one of the items in the Startup object dropdown menu should now be "Loader"
Public Module Loader
<STAThread()>
Public Sub Main()
Dim form As New CustomBorderlessForm(Nothing)
form.ShowDialog()
End Sub
End Module
You'll notice that the above bypasses Form1 completely. Also, instead of Show() I'm using ShowDialog() because otherwise the form shows and then the program ends.

Launching a MDI menu option with Enter key fires KeyUp in MDI Child form

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.

How can I make a custom .Net Windows Form that acts like a MessageBox?

How can I make a form that behaves like a message box?
For example, I have 2 forms named form1 and form2.
form1 contains my basic information (Name, gender, address and 1 button (educational background)).
form2 contains my educational background (school, school_add, button(OK) and button (Cancel)).
If I open form1 and I click the educational background button, then form2 will appear. What I want is that if the user tries to click form1 while form2 is still open, then form2 will blink and the user cannot manipulate form1. I want to require the user to click the Cancel or the OK button on form2 before returning to form1, just like how a message box prevents you from using the form behind it.
How can I do this?
What you are looking for is called a Modal form.
It's very easy to do: just call .ShowDialog(Me) instead of .Show().
' Form1 button handler
Private Sub buttonClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles button.Click
Dim f As New Form2()
f.ShowDialog(Me)
End Sub
See also http://msdn.microsoft.com/en-us/library/39wcs2dh(v=vs.100).aspx
Load your form using .ShowDialog instead of .Show
Form2.ShowDialog