Prevent ListView from bringing old form to front - vb.net

I have a form (we'll call it Form A) that contains a ListView control. When I select an item in the ListView, the event handler creates and shows a Form B.
I want Form B to appear in front of Form A. Both forms are maximized when shown. The problem is, the ListView in Form A is making Form A take the focus back right after Form B is shown. The screen flickers temporarily, so Form B is initially in front. I know it's the ListView doing it because if I open Form B from another control, it works as intended.
I'm displaying Form B with Show(). Changing it to ShowDialog() fixes the problem, but I don't want it to be modal.
This is happening during the handler for the ListView1.ItemSelectionChanged event, and changing to ListView1.SelectedIndexChanged doesn't help.
Some code, if it makes it any clearer:
Private Sub ListView1_ItemSelectionChanged(sender As Object, e As ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
If e.IsSelected Then
Dim my_form As New MyForm
my_form.Show()
End If
End Sub

Set the ListViews Activation Property to "OneClick", then handle the ItemActivate Event and display your Form from there.

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

Blocking interaction between child and main parent form vb

At the moment, I have a project I'm working on with a menu that opens a new window. Annoyingly, when the new window is opened, it just does everything the main parent window does that the new window is opened up from. I want to be able to have the new window be able to do it's own thing and not just share what ever the parent is doing. Is there anything I can do at a button's click to give this form some kind of attribute that disallows this?
You can use seperate form. the working of each form will depend on what you specified in that particular form.. You can use the MDI form. and call all child forms under the mdi form. the child form will open in seperate window under MDI form.
you can simply call the child form as
Private Sub MembersToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MembersToolStripMenuItem.Click
Dim xfrm As New childForm
xfrm.MdiParent = Me
xfrm.Show()
End Sub

close form when application loses focus

My form is displayed as TopMost on my application. The problem I have is that whenever I minimize my application or it loses focus, the form remains displaying. I want to be able to minimize my application or move to another and also hide or close my form. Once the application regains the focus, then unhide or open the form again.
Here is what I worked out on the form's closing event:
Private Sub frmNavigation_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Static Minimize As Boolean
If Minimize = True Then
e.Cancel = True
Me.Hide()
End If
End Sub
I tried using the same code in the applications WindowDeactivate event but nothing happens.
You do not show how you create the instance of your frmNavigation. I am assuming that you are using the Show Method, so just use the version of Show that you pass in the top level window. That will assign the owner of the form, it will then stay on top of your Main Form and minimize and restore with it also. If this doesn't work please show how you are creating and showing your form.
frmNavigation.Show(Me)
I was able to find an answer to the question. MSDN had an article on this very issue.
it can be found here: http://support.microsoft.com/kb/186908#appliesto

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().

vb.net: setting focus on nested custom controls

I have a nested custom control that I need to set focus on. I have it all internally wired up to automatically set the focus when the form loads, but when it comes up on screen, the designated accept button for the parent form is in focus instead. Even when is disassociate the accept button it still does not set correctly. How can I ensure my desired control gets focus.
I think what you're asking is that you would like to make a textbox (or button, etc) on a user control have focus when the form loads. Try setting the ActiveControl of the user control to the textbox (or which ever control) and then calling focus on it. For example:
Private Sub myUserControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ActiveControl = myTextbox
myTextbox.Focus()
End Sub
Set the tab order. If your custom control is on the same level as buttons (has the same parent), make sure your control and any containter it is in has tab index 0.