Finding the top most(z-order) control in a panel - vb.net

See my case is,
I am opening more than one forms (toplevel=false) inside a panel. All the forms opened inside that panel will be dock filled and brought to front during runtime. and my need in this situation is, how can i select the top most control(form) on that panel. Top most control means control(form) which is having greater z-order.
I am currently using this code by assumption,
panel.controls(0)
Can any body tell me, whether the above snippet is right or any alternate syntax available in dot net to achieve that.?

According to the MSDN:
The control with an index value of zero is at the top of the z-order, and higher numbers are closer to the bottom.
Therefore, I'd say your assumption was correct. Controls(0) will always be the top-most control. The only concern would be whether or not it is visible.

I don't see any other solution of looping through each Control and see what's the topmost one.
Something like:
Dim TopMostControl As Control = panel.Controls(0) 'Check if there are any control
For Each Control As Control In panel.Controls
If panel.Controls.GetChildIndex(Control) < panel.Controls.GetChildIndex(TopMostControl) Then
TopMostControl = Control
End If
Next

Related

Opposite of SetFocus

MSAccess VBA:
Assume, in an arbitrary form, the focus is set to MyControl on that form.
How to "unset" the focus without giving the focus to another control?
I'm lokking for a code like
MyControl.UnsetFocus
In your circumstance, you probably want to just set focus back to the parent form. This meets the conditions of unsetting focus without giving another control focus, and tabbing or clicking will activate the focus / tab-navigation again from that form.
Here's an example:
Forms![MyForm].SetFocus
Note that per the documentation for SetFocus, if you attempt to SetFocus to a Form with child controls that have Enabled set, this will cause focus to automatically bounce to the first eligible child control per the documentation.
Form.SetFocus method (Access) # Microsoft Docs
The option to give focus to the parent form does work as proposed by meklarian
Alternatively, I recently had to do something similar but I wanted to update a textbox value and simply go back to whatever had focus before. This is another case where something like an "unsetfocus" would be awesome, but unfortunately doesn't exist.
Should you need to do something like this, the following works well
Screen.PreviousControl.SetFocus

How to stay at the top of form on form load?

I am facing a problem with multiple forms.
When loaded, it doesn't show the tabs at the top which are part of the form and requires the user to scroll up.
I have done research. It may be related to the .SetFocus property.
Private Sub Form_Load()
Forms!frmEnrolementForm.Tab ("tabCtl0.SetFocus")
End Sub`
As I understand, user doesn't see the top part of window with tabs of tabcontrol. If so, reduce the size of tab control and whole window in order to fit in window on PC with minimal screen resolution. Also you can open desired tab page using code like
Forms!frmEnrolementForm.Page1.SetFocus
Here Page1 - is name of tabpage, not tab control. SetFocus is object method, not property.
It depends on the layout of your form, but you might be able to solve the issue by changing the "Tab Order" settings for the form.
It's under Form design tools > design > Tab Order, and you want the top/first items on your form to be at the top of the list. Ideally I'd say put all the items in a sensible order (e.g. starting with the top left and ending with the bottom right) for a good user experience, but the first item is the one that affects where the form opens.
Mine was opening in the middle of the page because my sub-form was set as the top item in the tab order, but I changed it to a button at the top of my form and now it always opens at the top.
I got this from user NeoPa in this forum answer.

MessageBox z-order

I've created a messagebox which I want it to stay on top of program when it appears on the screen. It worked normally when I hadn't set my Form to stay on top, but now my msgbox hides under it and it's hard to take it on top to actually see it. Can you set msgbox on top somehow?
If you're creating your own messagebox, use the ShowDialog instead of the Show method to make the messagebox be on top regardless of the Topmost setting. This gives you the advantage of returning different results with different buttons used, the same as the built in messagebox.

Set the visibility of separate GroupBoxes

I'm using VB.Net and in one of my WindowsForm I have created 3 GroupBoxes.
The problem is: I can't control groupbox.visible = true/false; so if I put the first GroupBox to false and the others to true... I still can't see the others.
I think I can't put one GroupBox on another group box, because when I do this, the overlapping group box will become the child of the below group box.
I have tried the Panel control but still have the same problem.
I think I cant put one groupbox on another group box, because when I do this, the overlapping group box will become the child of the below group box.
Yes, that seems like the most likely explanation. Windows has a hard rule about visibility: when a parent control is hidden/invisible, all of its children will also be invisible. This applies equally to all controls, which explains why the Panel didn't work for you either.
So if you want to change this state of your group boxes independently, you will need to make sure that they're children only of your form, not of each other.
This can be a real pain to get right in the designer using the mouse. Instead, use the "Document Outline" window, which shows you the forms on your control in a TreeView that emphasizes the hierarchy. Make sure that all of the group box controls are at the same level in that tree.

How to always display a control on the currently active tab of a TabControl?

I have a tab control with two tabs. Both tabs have controls which are unique to them, but there is one control which I would like to always appear on whichever tab is currently active.
I figure I just need to add some code to TabControl1_SelectedIndexChanged().
I tried
MyControl.Parent = TabControl1.TabPages(
TabControl1.TabPages.IndexOf(TabControl1.SelectedTab))
MyControl.Parent.Update() ' is this necessary?
and I also tried
TabControl1.TabPages(
TabControl1.TabPages.IndexOf(TabControl1.SelectedTab)).Controls.Add(SeMyControl)
but neither worked (the control moved once, but when I went back to the original tab, the control did not appear there.
googling found someone suggesting
TabControl1.TabPages(TabControl1.TabIndex).Controls.Add(MyControl)
but that looks dodgy as the control is never removed from the old tab, so repeated switching would probably add the control multiple times.
I feel that I am close, but not quite ... how do I do it?
No, that works fine since Controls.Add() changes the Parent property. Which automatically removes it from the tab page it was on before. A control instance can only have one parent.
The more straight-forward approach is to simply not put the control on a tab page but leave it parented to the form which a lower Z-order so it is always on top of the tab control. The only problem with that is that the designer will hassle you. It automatically sucks the control into the tab page when you move it on top of the tab control. One trick to fix that is to leave it off the tab control and change its Location property in the form constructor.
Using your second code snippet that you are concerned about because it doesn't remove it from the original tab, why not just remove it from the original tab before you add it to the new tab?
Maybe something like: TabControl1.TabPages(TabControl1.TabIndex).Controls.Remove(MyControl)