Tabbing on Form disappears when using Control.FromHandle - vb.net

VB2010: I have a form that I display within a mapping application that supports their framework through .NET. So when the user clicks a button on a custom extension then my form will be displayed.
Private frm As New frmDesigner 'since this is a floating form
Public Overrides Sub OnClick()
Try
''display the form normally
'Dim frm As New frmDesigner
'frm.ShowDialog()
'display the modeless form and the form will always be on top of the main app.
If frm.IsDisposed Then frm = New frmDesigner 'To handle user closing form
If frm.Visible Then
frm.BringToFront()
Else
Dim appCtr As Control = Control.FromHandle(CType(MyApp.hWnd, IntPtr))
frm.Show(appCtr)
End If
Catch ex As Exception
DisplayException(ex)
End Try
End Sub
This works great and I have been using this approach for a couple years now. The problem that I am seeing now is that the tabbing through controls on the form does not work. Once the form appears I press on the Tab button and the cursor does not tab through the controls. I can set focus on a textbox through the mouse and then attempt to tab over to the next control but that doesn't work either.
If I display the form normally through frm.ShowDialog then the tabbing does work. Is there something I am missing here to make the tabbing work properly?

Related

VB.net How to hide dialogue without close the application

I have problem about close() or dispose() function with my barcode reader (Windows Embedded Compact 7). In this case I can only hide() form.
I tried to show Form2 as dialogue but after I clicked the close button (to hide this form and go back to Form1) It made all of my application close
In Form1 (Main):
Public Sub showForm2()
Dim secForm As New Form2
secForm.ShowDialog()
End Sub
In Form2:
'close button
Private Sub closebt_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles closebt.Click
Me.Hide()
End Sub
Form can't be hidden if it is shown as Dialog. If you want to hide the form then use form.show() rather than form.ShowDialog(). Also here is a link
http://www.vbforums.com/showthread.php?759061-How-can-i-hide-my-second-form-dialog-without-bliking-form-not-closing-my-first-form
Go to properties page of the project. In the Application tab, there is a setting:
Shutdown mode
When startup form closes
When last form closes
Choose "When last form closes" to prevent closing the application when your main form closes.
in the index form add in form closing the fallow code:
Form1.Dispose()

Enabling/disabling MDiParent form after closing a .showDialog() form

I currently have a form that is declared as NewMDIChild = frm1. Within frm1 i open a new form that is about 1/3 of the size of frm1 - this forms name is frmX. This is how I open it....
Me.Enabled = False
'Pass globals to be used in search query
Dim frmX As New FrmX()
frmX.ShowDialog()
So I include this code with a button_Click on my Frm1. Everything works just fine. What I'm trying to do is on the button click on frm1 - I'm trying to disable frm1 - show frmX in .ShowDialog. On FrmX i have a CLOSE button, so that when user clicks CLOSE on it, I am able to enabled = true the frm1 that is in the background. I trying to do it on ACTIVATED event but it doesn't recognize it. How would I do something like that...
Not sure if i have to call a function or something from frmx or if there is an event in frm1 that can be used to enabled it. I tried GotFocus, MouseHove - nothing works
Once the form is disabled, most (if not all) the events are not going to fire. The next line to execute will be anything after the ShowDialog() so you can enable it there:
Me.Enabled = False
Using dlg As New FrmX
dlg.ShowDialog()
End Using
Me.Enabled = True
Note that when a form is shown using ShowDialog() it is not automatically disposed, so the code uses a Using block to do so.

VB.NET Calling forms by Variable

I'm playing around in vb.net and would like to create a menu for my application. The application has an Main which is the MDI Parent and then has the sub forms that open up as the user browses the menu structure. I would like to track which form the user came from so that when they close their current form, it opens up the previous form they had open.
What I have so far is a public variable "FormTracking" which is a list of Form. The open form function works fine, but when they close the form and it calls the "OpenPreviousForm" function it fails as the previous form has been closed/disposed. How can i create a new instance of the form as calling "NEW" doesn't seem to work
Public FormTracking As New List(Of Form)
Public Sub OpenForm(theNewForm As Object)
'First Declare the new form
Dim f As Form = DirectCast(theNewForm, Form)
'Set the Forms parent
f.MdiParent = frmMain2
'Add the form to the tracking
FormTracking.Add(theNewForm)
'Show the form
f.Show()
End Sub
Public Sub OpenPreviousForm()
If modMenu.FormTracking.Count > 1 Then
Dim f As New Form
f = modMenu.FormTracking(modMenu.FormTracking.Count - 2)
'Set the Forms parent
f.MdiParent = frmMain2
'Remove the last item
modMenu.FormTracking.RemoveAt(modMenu.FormTracking.Count - 1)
'Show the form
f.Show()
Else
MessageBox.Show("Whoops, run out of forms.... ", "modMenu - OpenPreviousForm")
End If
End Sub
When opening a form it it calls:
modMenu.OpenForm(menupage_1)
me.close
And when closing a form, i try to call
modMenu.OpenPreviousForm()
The error i am getting when calling the above code is
"An unhandled exception of type 'System.ObjectDisposedExeption' occured in System.Windows.Forms.dll
Additional Information: Cannot access a disposed object
I understand i need to create the object again, but not sure how to using the functionality as above. Alternatively, if someone can present a better way of doing an application menu, please feel free to let me know.
Cheers
Use Me.Hide instead of Me.Close so you do not dispose the form.

Why does my new form move behind the opener?

I'm not going to post a bunch of code here since I do not think it is a code issue.
Here is a link to my original question where I have shown the code if you are interested. Code
Just as a test I created a blank form window (Form1.vb) and no code gets passed to it and no code runs when it opens. If I do Form1.Show() from a MenuStrip Control or a Button Control, the window opens and stays on top. Now if I do Form1.Show() from a TreeView Control, the window opens and goes behind the window with the TreeView Control.
So my question is, what is different about the TreeView opening a form vs a button or other control?
I am using the basic VB TreeView Controll, and the new form is being called in AfterSelect method for the TreeView.
The AfterSelect works if you use your keyboard navigation to select a node, but it doesn't work when you use the mouse because the mouse capture is forcing the parent form to remain in focus. You would have to run your code after the AfterSelect event:
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) _
Handles TreeView1.AfterSelect
Me.BeginInvoke(New Action(Sub()
Dim f2 As New Form2
f2.Show(Me)
End Sub))
End Sub
Use the Form.Show(parentForm) option, this will always put the new form on top of the old one.
have you tried Form1.ShowDialog() ? or if you don't want to show it as a dialog you should use:
Form1.Show()
Form1.BringToFront()

Need To Close A Panel When Clicked Outside (Lost Focus)

I have an issue that I cannot seem to overcome.
In my application, I have a custom class that loads a form into a panel upon startup. Then when I click a button on my main form I show the panel as visible revealing the form to the user.
My problem is that I want to be able to hide the panel when a user clicks outside of it (back onto the main form).
so far I have tried Form_Deactivate, Form_Leave, Form_LostFocus, Panel_Leave and Panel_LostFocus events but nothing will seem to trigger an event consistently to hide the panel. The only thing that works is if the user clicks inside the form (on a listview control) once the form is visible and then clicks outside of the form.
Is there anyway I can ensure this event gets called everytime whether the user clicks the form or not?
So far my code looks something like:
Public Class cls_UserObjects
Private frm As frmUsers
Public pnl As Panel
Public Sub ShowUserPanel()
Try
frm = New frmUsers
frm.TopLevel = False
pnl.Controls.Add(frm)
frm.Show()
frm.Focus()
....
End Class
Then in my main form I call the code below to build the form into the panel:
class_Users.pnl = pnlUsers
class_Users.ShowUserPanel()
And pnlUsers.Visible = True to show it to the user
I just can't seem to close it. I understand that Panels don't support the LostFocus properly, however, I can't find away around this. Maybe it has something to do with how I am opening my form/panel but I was advised to use classes to open forms so I can have more control over the controls within my forms from outside calls.
Any help appreciated. Thanks
MouseLeave event works, the panel hides immediately once it leaves the panel.
Private Sub Panel1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave
Panel1.Visible = False
End Sub