When my program runs it closes form1 after a few seconds. Depending on what happened during form1's lifespan I may want to open form2. Normally I would just make a new instance of form2 and use the show() method. But form2 is then a child of form1 and then also closes. Does any body have an idea on how to get this to work? thanks.
in the properties of the project make sure to select "When last form closes" in the shutdown mode dropdown
Before you close form1 set form2.Owner property to Nothing. This will need to be done before the FormClosing event of form1, since form2 will already be scheduled for closing by then.
But this only works if form2 actually is a child of form1.
try looking for the MVC or MPV patterns. This is than best done in the controller or presenter and will not give you the problems that you describe.
Related
I have 3 forms
MDI parent for TCP client
MDI parent for serial
The main menu form
I want to set the main menu form as the startupform then I want the 2 MDI parent to be loaded on the background. I don't want to show them, but I want to load them so I can trigger the on_load event of those 2 mdi parent which is to create serial/client child and connect automatically.
I've tried, but it didn't work.
Private Sub frmMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
frmTcpMDI.show
frmSerialMDI.show
End Sub
I even tried adding a timer then starting it upon the show event of my startup form, the tick event is to open my frmTcpMDI and frmSerialMDI, still, it is not working.
Okay basically, if I understand what you want, you need a way to load your forms but not display them.
You want to run some code when the Load event occurs. However, as stated here by Microsoft :
Occurs before the control becomes visible for the first time.
Which basically screws you up...
So there is a few workaround for that.
Option 1 : You decide to run your code in the Sub New()
If you put all the code you want to run in the Sub New() of your child forms, you don't need to call Form.Show(), you just need to create the forms, and your code is running. When you actually need to display the form you call the Show() method and it's done.
Option 2 : You can't run your code in the Sub New()
If, for some reason, you can't run your piece of code in the Sub New() method, you still can do it with the load event. Just hide the form after showing it... Looks silly but will work.
First notice, still from Microsoft :
The Load event occurs when the handle for the UserControl is created. In some circumstances, this can cause the Load event to occur more than one time. For example, the Load event occurs when the UserControl is loaded, and again if the handle is recreated.
Which mean you have to be carefull to run your code only once...
Then, from your parent :
Dim myNewForm = new frmTCPMdi()
myNewForm.Show()'will call the Load event
myNewForm.Hide()'will hide the form, so it is loaded but invisible...
Honnestly, I think Option 1 looks better, but sometimes and for some reason you can't always go the easy way...
I have a button in form1 that opens form2. I do this using frm2.show(). It works totally fine for the first time that I open frm2, but when I close frm2 and click on the button in frm1 to open frm2 again, I get this error:
Can someone tell me how to solve it?
Edit: I have a module where I have my database connection and my declaration for the forms:
Public frmGame As New Game
Public frmPlay As New Play
Public frmFinish As New GameFinish
Public frmLogin As New Login
Public frmManage As New Manage
Public frmInsert As New Toevoegen
Where I open the form is just when I click on a button in form1.
Sounds to me like you are Close()ing frm2. Closing a form should dispose it and release its resources, so you can't simply Show() it again. Instead, you need to create a new instance of the object, like this:
frm2=new Form2()
frm2.Show()
If that doesn't work (perhaps because you don't want to re-initialize the form's data members), you could use Hide(), rather than Close() to temporarily hide the form during your program's execution.
If you need to prevent the form from being closed with the X button, you can do this with a few different methods:
The best way to go may be to hide or disable the close button. Read up on This post to get a better idea of how to do that.
You can use the FormClosing event, either from inside frm2 or from the main window. Set the Cancel property on the FormClosingEventArgs object that gets passed in. The problem here is that you will need to provide an additional code path to close the form when you actually want it to close. The CloseReason property of the FormClosingEventArgs object should give you a way to handle that properly. Needless to say, this is probably the riskiest way to do things, since you need this window to close when the application shuts down, but you're also intentionally block that from happening.
You could catch the FormClosed event in the the form that spawns frm2, then create a new instance of the window. Again, you'll need to provide a code path to allow the window to close when the application shuts down.
In all, method 1 is probably the safest. You can close the window with .Close() when the application shuts down, but the user can't close it with the X button.
OK. I have a code, and it should work, it DOES close the login form, but doesn't open the menu form.
If username.Text = "lolman8776" Then
If password.Text = "#########PASSWORD HIDDEN FROM THE INTERNET#########" Then
Form2.Show()
Me.Close()
End If
End If
What I don't understand, is that this code has no syntax errors and SHOULD WORK!
It shows form2 and THEN it closes itself, but Form2 never shows up.
I'm running VB.Net 2013 Community, as it was a free download. (I also registered it so it wasn't trial).
I have tried removing Me.Close() from Form1 and putting a line of code into Form2 that would close Form1:
Form1.Close()
But, still nothing. I do not know why ANY METHOD won't work. I've searched high and low for an awnser, but to no success. Does anyone have a solution?
According to your comment: "I tried not even hiding/closing form1, but just showing Form2 will cause the app to break."
I believe your issue lies around not instansiating your Form. The Show() method is not static and cannot be called directly on Form2. It must be called on an instance of your Form. For example:
Dim myForm As New Form2()
myForm.Show()
Please refer to this for a little more information.
*edit: OK, so this is my real problem, below scenario happens only when the form is MDIChild.. thanks for anyone that could provide me with the code
I have a form with labels, panels, buttons etc. Where I'm having problem is, while form2 is my active window/form and I clicked on a control inside form1, the form1 does not activate itself. What I would like to happen is for form1 to activate even when it's not the form I clicked, only the control inside it (any control)..
I'm thinking that if I clicked a control on the form, there's an event fired on the form. If I could only know of that certain event, that would help - maybe (coz I could just add Me.activate on that event if it exists). I've tried searching for series of events when a control (ex. label) is clicked but to no avail. I hope that someone could help me with this one.
Thanks in advance.
*edit
i will just try to make my question more understandable..
How can I activate the form when only the control is clicked (say, label or textbox)? My forms does not activate or focused when I click inside it except the form itself..
I can do this on one control..
Private Sub Label1_Click - Handles Label1.Click
Me.Activate()
End Sub
But what if I have 20 controls (labels, buttons, textbox, combobox, etc)? See? =)
EDIT: this answer does not apply to MDI applications.
I think what you really want to know is which one of your forms is currently the foreground window (if any). The first thing you need to understand is that a form instance lives inside a window, but the window's behavior is controlled somewhere higher up. Similar to how a form instance is identified by a variable pointing to the instance, a window can be identified by what's known as a window handle.
Knowing this, the proper way to find out whether a form is the "active" form is to:
find out the window handles of the windows containing our instances of Form1 and Form2
find out the window handle of the foreground window (which can be any window)
compare the value found in step 2 to all of the values found in step 1
Perhaps you'd then like to fire an event if the foreground window changes, but I'll leave the actual implementation up to you. There are probably several ways to perform step 1 and 2, but I can't give any solutions off the top of my head. Hopefuly I've put you back on the right track.
EDIT
Alternatively, you can use the form's Containsfocus property. If its value is True, you can safely assume that your form is the foreground window. I didn't find out about this property until after I wrote my own implementation, which I'll show you anyway:
One module containing only a windows API call
Friend Module NativeMethods
Friend Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
End Module
Calling this method will return the window handle of the foreground window (if any).
One module containing the extension method for the Form class
Imports System.Runtime.CompilerServices
Public Module FormExtensions
<Extension>
Public Function IsForeground(f As Form) As Boolean
Return (f.Handle = NativeMethods.GetForegroundWindow)
End Function
End Module
Calling this method returns whether the specified form f has the same window handle as the foreground window.
Usage example
You could use a Timer that periodically checks whether a form is the foreground window.
Public Class Form1
Private WithEvents timer As New Timer With {.Enabled = True}
Private Sub timer_Tick(sender As Object, e As EventArgs) Handles timer.Tick
If Me.IsForeground() Then
Console.WriteLine("this instance of Form1 is the foreground window")
End If
End Sub
End Class
Like I said before, you can use Me.ContainsFocus instead of my extension method and it will work just fine.
In non-MDI forms, the form is automatically activated when you click any control inside it.
im having a similar problem like the solution here Prevent main form from appearing when showing another form . but some of the suggestions were to minimize the main app so it doesnt show, which i cant do because my main app is supposed to be a desktop to be underneath all other apps to replace the windows desktop. And the second forms are supposed to be sticky notes. so i cant minimize the main window cause it has the user background and other controls. i tried making the parent of the notes a Nothing pointer, a pointer to the desktop, creating the form through a dll but i had no success.
My main problem is that when i click a note (form2) form1 comes up, even with form1 having the WS_EX_NOACTIVATE in the createparams. form1 does the form2.show() but they shouldn't be attached.
Another reason im having trouble with the solutions preseted in that post is that they are for delphi and im doing it in vb.net.
All i need is being able to click on the controls and write in the note without bringing the main form behind the note. either making them independent, or making the note not focusing the first form or being able to operate the note without it activating. i dont know. my last resource is to attach my main form to the desktop but i've heard is the worst thing you can do because it can cause problems hanging the system.
If you want both forms to co-exist but don't each to interfere with the other: In this case, then you might want to have a third Form that calls both Form1 and Form2 to be opened, and let me suggest and MDI Form with Form1 and Form2 as children forms of the MDI form
'============== my previous post ========================
You can force the user to first dismiss Form2 then allow him to go back to form1 by showing Form2 as Modal form. Here is how to display Form2 as modal
Dim f2 as New Form2
f2.ShowModal()
If that does not work, try this
Dim f2 as New Form2
f2.Show(True)