how to know my application lost focus in VB.NET - vb.net

I am using VB.NET to build my application. And in my application has a lot of Forms. It doesn't use MDI Parent Form, but I use another simple Window Form (I named it frmMain) that I suppose it is my MDI Parent Form. When frmMain load, windowState = Maximized. And when I open a Form (example: I named it frmCustomer) that I suppose it is my child Form, and I set its properties (frmCustomer.TopMost=True) when it load, so it always on the top. But When I change to open another application such as Ms. Word or Mozilla Firefox... the frmCustomer is still on the top. My question is that; how can I know my frmMain lost focus?

If you want the form to stay in front of the main form, but not other applications, the simpler solution would be to set the main form as the child form's owner. For instance:
childForm.Show(parentForm)
or
childForm.Owner = parentForm

Related

VB.NET - Navigating without loading another form

I'm currently developing a simple enrollment system for our school project. I have multiple modules that when clicked will open a new form with another set of sub modules.
However, I hate how forms load every time I open a new form. Is there like a way that when I clicked say for example "Enrollment" button, instead of loading a new form, it will change all the controls instead. Tried using User Control but it keeps crashing my program down after I created 2 user controls and load them in one form.
Main Form;
Enrollment Sub Form
Please help me guys.
As LarsTech mentioned I would use user controls instead of forms.
Each user control would be setup the same way as your form is, but can be loaded to a panel on the main form, allowing for a single form to show all content.
Dim myControl = New ControlYouCreated
panel1.Controls.Add(myControl)
myControl.bringtofront
This for example, will bring in the contents of the user control you created, into your form1 panel.

vb.net using SetParent API over IsMDIContainer?

I'm working with a set of custom controls (including a custom form), I have repainted the custom form to give it a metro style look and basically recreated the title bar of the form. Setting this form to be an MDI container causes the whole form to be repainted grey regardless of anything dictated in the OnPaint handler for the form.
I want to create a multi form application where all of the forms that are not the main form, appear inside the main form (the idea of MDI windows). I have also seen a SetParent API that achieves a similar effect without modifying its visuals, however, I have used this before to tie a cmd window to a form and it is not always reliable.
Would it be more efficient to use the MDI feature inside Winforms (if so, how do i overcome the complete recolour issue) or dump MDI and use the SetParent API to modify the child windows' parent?
EDIT: I have decided to go for using the SetParent API, it works everytime as opposed to my previous experiences with the console window. However, I need to create the window before I can set the parent and sometimes the user can see the screen flash briefly before it gets 'hooked' inside my main form. Is there a way to 'hook' it without it being visible to the user?
With MDI Parent Form if your issue is the background, then this is all you have to do, Set the IsMDIContainer property of the form to true, and then loop through each control on MDI Form to let them know this is the MDI Background, you can put this code in the form loading event,
Private Sub FormMDI_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctl As Control In Me.Controls
If TypeOf ctl Is MdiClient Then
ctl.BackColor = Me.BackColor
End If
Next ctl
End Sub
It might appear gray when program is not running, but it will show your desired background once the program runs

Opening winforms in the same screen

I have an application with multiple win forms. I have noticed is that if the user has multiple screen displays and changes the application to one screen, the other forms that are called by other buttons, will open in the main display and not where my main application or form is.
How can I change this?
You can use the Form.CenterToParent Method on your Forms, they will then open up centered on your the creating Form, Or you can use the Screen Class to get the Bounds of the Display that your Main application is running on, then pass it to your created forms.
Edit:
On second thought assigning the Owner might just be enough, I don't have a dual monitor computer booted up at this time to test though
Dim frm As Form1 = New Form1()
frm.Owner = Me
frm.CenterToParent()
frm.Show()
Edit
Just had a chance to check it out. It was as I thought assigning the Owner to the new Form or using the Form.Show(IWin32Window) will open the new Form on the same screen as the originating Form.
frm.Show(Me)
Looks like the CenterToParent property is protected now.
According to the MSDN link
Do not call the CenterToParent method directly from your code. Instead, set the StartPosition property to CenterParent.
If the form or dialog is top-level, then CenterToParent centers the form with respect to the screen or desktop

VB.NET Difference between Focus() and BringToFront()

I'm developing an application in WinForms and I have 2 MDI children and I'd like to know the difference between Form.Focus and Form.BringToFront
I would like to know which one to use when clicking on a respective form's titlebar in order to have it pop up while I'm drag/dropping the form
If you are setting the child form mdiParent property after you are calling Form.Show then it messes up the focus of all the child forms so for me, drag/drop of MDI children inside an MDI parent won't focus the form upon Drag start, but only after MouseUp
Focus() sets focus to U element.
BringToFront() brings your element on top of other elements (which overlap your element).
So if you use BringToFront() on your form, it will be brought to front over over windows in your MDI environment. But it doesn't mean that your form will gain focus.
If you use Focus(), your form will gain focus: for example you will be able to interact with the form via keyboard. But it does not mean that form will be brought to front over other forms.
So maybe you should use both methods.
When I open MDI Child Forms, I create the following function within the MDI Parent:
Public Sub ShowMDIChild(f As Form)
f.MdiParent = Me
f.Show()
End Sub
The "f" argument is the new form I wish to open:
MyMDIPParent.ShowMDIChild(New Hospitals)

vb.net Prevent Form 1 from activating when form 2 is clicked

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)