vb.net using SetParent API over IsMDIContainer? - vb.net

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

Related

Keep track of Window being Last Focus

The VB.Net program I am creating dynamically created Panels within a TableLayoutPanel and adds form elements to them. I need to keep track of what the last of these Panels to have focus was, and am hitting a bit of a brick wall.
I have tried creating an event class for when the Panel has focus:
Private Sub Self_GotFocus(ByVal sender As Object, ByVal e As EventArgs) Handles Me.GotFocus
GlobalController.Focus_Target = Me.Name
End Sub
The classes for each Panel Inherit from Windows.Forms.FlowLayoutPanel, which I why I have the call being Me.GotFocus. Additionally, the GlobalController class is just a class meant to hold global variables for the program.
Now the issue I am having, is that this event only seems to trigger when I actually am deleting the panel. When the panel is created, if I click on it, or any of it's form elements, the event never gets triggered (I debugged the program with a breakpoint).
I can't exactly figure out why this only triggers when I go to delete the panel, and not at any other time. Is there another event I should be using instead of GotFocus?
Use .Enter event in your panel since GotFocus is related only to focused control (not it's parent), mostly when UICues is set.
See MSDN GotFocus

Maximized state of child form Not working correctly when used in MDI container (Visual Basic 2013)

I am having this weird problem in Visual Basic (2013). I have a form in MDI container. Both of them, have property WindowState to Maximized. While executing, MDI container loads in maximized state (So far Ok!). But, when I open the child form, it Does NOT give the Actual Maximized state appearance (although Restore button on top right shows that it has loaded in maximized state, but it did not maximized).
I even tried with form1.WindowState = FormWindowState.Maximized both before and after form1.Show(), but no luck.
I have attached a screenshot too. Note the dead space of MDI container and Restore icon of child form (which means child form is in maximized state).
Few observations -
When I restore and then again maximize it, it DOES maximize correctly.
I also observed that this problem is occurs only for one time. I mean, if I have 2 forms both set to load in Maximized state. When I open the first form (no matter which), it loads like i showed in the screenshot, and after that when I open 2nd form, it loads in maximized state.
I could not figure out, what went wrong? How to correct this?
go to your form properties. Set windowsState = normal. then put this to code
Dim frmC As New frmChild
frmC.MdiParent = Me
frmC.WindowState = FormWindowState.Maximized
frmC.Show()
I found that setting the MDI child form to maximized within the New routine of the class works always OK.
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.WindowState = FormWindowState.Minimized
End Sub
OK, the last line may be one you may code under a condition...
Check your icon. It must contain a 16x16 page. If the smallest is 32x32, the problem you illustrate will arise.

Form maximize on another form

I have an MDI application which contains many child forms
My problem is, on clicking a particular menu, I am opening a form with maximize window in the MDI form. This works fine.
Now if I open another form above the first one, and if i want the second form to be of normal size, i am unable to do it.
Second form also opens with maximized window similar to first one. I want the second form to be of normal small size.
I want to show second form normally and first form maximized.
How can i do that?
Private Sub TESTToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TESTToolStripMenuItem.Click
Dim f As New newCalendar2("UGHARANI")
f.Show()
f.MdiParent = Me
f.WindowState = FormWindowState.Maximized
End Sub
Okay, I think I understand what you're trying to accomplish: you want the first (data) form to be a kind of background to your MDI application and have the other forms display on top of it, right?
Well one way to do it might be to remove borders from the background form –FormBorderStyle = None– and fill-dock it in the MDI parent form. Although it would end up coming to the fore and hiding all your other forms if a user clicked anywhere on it. But if it doesn't require any user interaction you could always use its Activate event to send it back to the background, using Me.SendToBack().

how to know my application lost focus in 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

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)