Win Forms VB App in Win 10 - focus issues - vb.net

I have an app built in VB in Visual Studio 2012 that works absolutely fine on my windows 10 desktop, but as soon as i use it on my windows 10 tablet i see a couple of issues :
1) any forms that were intended to be smaller than full screen are shown fullscreen anyway (almost as if you're not allowed any windows less than full screen) i can live with that if need be, but surely theres a way around it?
2) - the most important one - for some strange reason, i start my app, and when i click on a button let's say it opens form 6. once i finish what i'm doing the code closes the form 6, but the previous form is now hidden and all you see is the desktop. ie it's still running, it just lost its focus and must be selected again. I understand i could set the focus to the underlying form before closing form 6, but here's the problem : it can be one of several different forms calling form 6..... so how do i make the app stay aware of what form called form6 so that when done i can return focus there?
this doesnt seem to be a problem on the desktop so i've never encountered it before. i hope one of you experts has dealt with this before

I don't have enough points yet to comment. My guess for #1 is either resolution or DPI differences between the 2 screens, and I don't have experience with the latter.
As for #2 and keeping the form aware of who called it, this is how I do it, which may not be the best solution, of course. ;)
First any form that can have multiple callers has a variable defined like
Dim callingForm As New Form
I typically create an Initialize routine to handle as much as possible before loading a form and this routine is called with the parent form (Me) as a parameter.
Dim frm As New frmClient
frm.Initialize(Me)
frm.Show()
Me.Hide()
In Initialize, callingForm is set to the parent
Public Sub Initialize(parent As Form)
callingForm = parent
'whatever else you need to do to init...
End Sub
Then when you exit the form...
Private Sub exitForm()
'whatever other closing stuff you need to do...
callingForm.Show()
Me.Close()
End Sub
That should get you started.

fyi for anyone encountering this problem, i solved it myself and it didn't require any coding. It was a difference between how vb apps act when the tablet is in "TABLET" mode, and when tablet mode is switched off. So all that needed doing is switching off enhanced tablet mode in the windows 10 settings

Related

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

Why does my form appear behind everything?

I have a program where I need to do some initial work before calling the form, so I disabled the Application Framework setting and wrote my own Main function with a call to Application.Run(myForm) when it's time to run the form.
Everything was working fine, no problems, but now I have need of some other service before opening the form. Rather than add all that code to this program, it has all been moved into its own executable. This second program can edit files that the first program will use, so I need the first program to wait so that it will read up those changes (should they be made). I suppose could just as easily use the Shell function, but for various reasons I'm creating my own Process object and calling it/waiting on it through that.
Anyway, I make this call to the second program some time before the Application.Run call. The first program waits its turn, and I can interact with the second program successfully, no trouble at all. But when it's done, the window for the first program is hidden behind any other windows that are on the screen. This doesn't happen in XP, only in Vista (and maybe 7, but I haven't confirmed yet). I've already tried manually forcing the form to appear in front, minimize then maximize, get focus, etc, but nothing brings it to the front unless the user manually clicks on it with the mouse.
What am I doing wrong? Why does this behavior occur? I know it has something to do with waiting for the executable to finish, because if I don't force the first program to wait everything is fine (other than it not waiting). I can circumvent the issue by calling the second program in the Load event of the form, but then I have to read the file a second time to catch the changes instead of reading it once, and it also looks bad because the form is being drawn really slowly while the second program is sitting there.
If anyone has any input, I'd appreciate it.
This isn't really an answer to why you're experiencing this behaviour, but a simple workaround would be to temporarily set the form's TopMost property to True in the load event. Then, depending on how intrusive you want that to be, you could either reset it under a short timer or wait for say the MouseEnter event to fire.
There are another topic in this site about that, but I not got the link. This problem seems be a bug into .NET framework. The API below (VB.NET example) works for me in windows XP and 8.1. Don't tested in other versions of Windows.
<Runtime.InteropServices.DllImport("user32")> _
Public Shared Function SetForegroundWindow(hwnd As IntPtr) As Integer
End Function
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
SetForegroundWindow(Handle)
End Sub

My application won't terminate

VB.NET program.
While developing on Visual Studio Express 2010, there are two buttons: Start Debugging, and Stop Debugging.
Well, while debugging, I close my application (the red X button). However, when I look back at Visual Studio, it seems to still be in debugging mode (the Start button is disabled, and the Stop button is enabled). So I have to manually press the Stop button.
I remember it was not like this before.
Perhaps it is because my application uses multiple forms or something? Then I am probably missing something rather important... Any ideas?
You've got something open somewhere. You can force the entire app to quit by adding
End
in the form's FormClosed event:
Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
End
End Sub
Are you doing any threading? Shutting down your primary app leaving threads running will cause what you are seeing. Also app design can cause what you are seeing. You can start your vb.net app running a "main" procedure, but if you don't provide an exit for it, the app will continue to run.
Ensure that all hidden forms (if any) are closed, properly dispose all objects.
If you are hiding any form and forgot to close it in your application this scenario may happen. Or if you are using threading in your application and they are not terminating properly then also it happens.

WPF User Control is causing Out of Memory Exception

Looking for a free spell checking solution, I thought I was so smart in doing this but I guess not.
I have created a windows form based application and I want the form to add a user specified amount of user controls (with textboxes) on to a panel. The user can then click some button and the controls on this panel are cleared and new ones are added. The user does something and the process is repeated. Now, I wanted these textboxes to support spell checking and looked all over for a free solution.
WPF textboxes support spell checking where the ones in regular win forms do not. I thought I would be able to use these WPF textboxes by adding them to an ElementHost object which is, in turn, within a panel. This panel would be a user control.
So, in my application, I would be able to add instances of these user controls onto the form and make use of .NET's spell checking goodness. This actually worked but after using the application for a while, found that the application would eventually freeze on me due to out of memory errors. I have pinpointed the memory errors to these WPF controls since this problem does not happen with normal textboxes.
When the window is opened and the number of controls is specified, this is pretty much how the controls are added:
Dim xOffset As Integer = 0
For i As Integer = 0 To theNumber
Dim myUserControl As New SpecialUserControl()
myPanel.Controls.Add(myUserControl)
myUserControl.Location = New Point(7, 7)
myUserControl.Location = New Point(xOffset, 7)
xOffset = xOffset + 207
Next
Note that:
myPanel is a panel on a form
SpecialUserControl is the user control with WPF textbox (within an ElementHost object)
When the user pressed a button, the panel is cleared:
myUserControl.Controls.Clear()
The user can then repeat the process.
There are a lot of results on the internet when I tried to find a solution and I'm thinking that the problem I am having is due to the fact that the WPF control is not going away even after clearing the panel. Following this conclusion, I have tried different solutions regarding disposing these controls or setting them to nothing but the memory problem keeps occurring. If someone could give me some advice or ideas here, I'd really appreciate it.
I've decided that this may just be due to the fact that these user controls are being created faster than they can be collected. I've changed the program so that it doesn't create any of these special user controls if it isn't necessary. The program works fine with a more manageable number of WPF controls.

Start VB.NET GUI app using Sub Main or form startup object?

Is there any reason to start a GUI program (application for Windows) written in VB.NET in the Sub Main of a module rather than directly in a form?
EDIT: The program won't take any command line parameters and it will be executed as a GUI program always.
The primary reason for using Main() in VB .NET 1.x was for adding code that needed to run before any forms were loaded. For example, you might want to detect whether an instance of your Windows Forms app was already loaded. Or you might want to intercept any unhandled exception for the AppDomain:
AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf MyExceptionFilter
But the next version of VB and Visual Studio 2005 introduced a new Application model that made Main() unnecessary in most scenarios. You can now intercept the My.Application.Startup event to add code that needs to run before any forms are loaded.
Note that the code for the Startup event handler is stored in the ApplicationEvents.vb file, which is hidden by default.
You can do it either way, but you should really only keep code in the form that is directly related to the operations and user interface elements on that form. Application startup code isn't related to UI, normally concerned with splash screens, checking network connectivity, verifying a single instance only, setting up user configuration settings, and so on.
After the above items (or the appropriate initialization code for your app) are complete, Sub Main can create an instance of the main form, then show it so the user can begin interacting with your application.
This separates startup code from your form code. Later, when you're maintaining the application, you'll be glad you separated the two.
Yes, and I have done it a few times.
One reason is, that if your app is COM EXE (speaking now from a VB6 point of view) then you want to be able to detect in what context the EXE is being called (being launched or being spoken to by some other app).
For example:
Sub Main()
If App.StartMode = vbSModeAutomation Then
...
Else
...
End If
End Sub
Another is if you want your app to be able to handle any command line parameters.
For example:
Sub Main()
If App.PrevInstance Then End
If InStr(Command, "/s") > 0 Then
Form1.Show
ElseIf InStr(Command, "/p") > 0 Then
LoadPicture ("c:\windows\Zapotec.bmp")
End If
End Sub
(from one of my attempts to make a screen saver)
No, if you always want to show that form.
Yes, if you sometimes want to use your app without GUI, just using command line.
Yes, if I want to display different forms depending on some parameter (in a file, on a remote server, etc.).