userform with webview-element doesn't load, program freezes - vb.net

I'm new to using Visual Studio as well as Visual Basic and still struggle with getting anything to run. The only experience I have is from working with VBA in Excel.
My goal was to create a Userform which links me to frequently used webpages, without using my usually used browser.
Because I need the form to show a webplayer, which does not support internet explorer in any version, the webbrowser-windows form doesn't suit my needs.
I did however find out that there is a webview-windows form which uses the edge-rendering engine and should load the webplayer correctly.
I have a start-page as a userform with two buttons. Number one opens a second window with a webbrowser-form.
Number two is supposed to upen a window with a webview-form.
While The webbrowser-form is loaded correctly, whenever I press button number 2 the program freezes and no userform is shown.
Any idea what I could try?
Thank you in advance,
Alex
PS: Did not do much of coding, just
Public Class Form1
Private Sub btnClickThis_Click(sender As Object, e As EventArgs) Handles btnClickThis.Click
Form2.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form3.Show()
End Sub
End Class
on the first userform, to load the userform 3, which contains the webview.
Userform 3 just has this on it.
Public Class Form3
Private Sub Form3_Shown(sender As Object, e As EventArgs) Handles Me.Shown
WebViewCompatible1.Navigate("https://google.de/")
End Sub
End Class

Related

Improve UI responsiveness on windows form application

I am currently working on a project and decided to create a user interface for it using visual studio with a windows forms application(Visual Basic).
The problem I'm facing is that the user interface doesn't respond as quickly and smoothly as I'd like it to.
Mainly, I am using pictures as buttons to make the user form look more modern.
However, when I hover my mouse over a "button" it takes a while until the "highlighted button" appears.
P1 is the picture of the "normal button" and P2 is the picture of the "highlighted button".
Here is the short code I have for now:
Public Class Main
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub PictureBox1_MouseHover(sender As Object, e As EventArgs) Handles P1.MouseHover
P1.Visible = False
P2.Visible = True
End Sub
Private Sub P2_MouseClick(sender As Object, e As MouseEventArgs) Handles P2.MouseClick
'Call cmdInit()
'Call cmdConnectRobot()
'Call cmdUnlock()
End Sub
Private Sub Main_MouseHover(sender As Object, e As EventArgs) Handles Me.MouseHover
If P2.Visible = True Then
P2.Visible = False
P1.Visible = True
End If
End Sub
Private Sub P4_Click(sender As Object, e As EventArgs) Handles P4.Click
End Sub
End Class
Another problem I'm facing is that when I call other subs, the user form becomes unresponsive while the sub is running.
I researched and found that I could implement multi threading or async tasks but I'm a bit lost and would be extremely grateful if someone could guide me or point me in the right direction.
Thanks in advance!!
In this case your UI is responsive, however the MouseHover event is only raised once the mouse cursor has hovered over the control for a certain amount of time (default is 400 ms), which is what is causing the delay.
What you are looking for is the MouseEnter event, which is raised as soon as the cursor enters ("touches") the control:
Private Sub P1_MouseEnter(sender As Object, e As EventArgs) Handles P1.MouseEnter
P1.Visible = False
P2.Visible = True
End Sub
You can then use that together with the MouseLeave event on the second picture box to switch back to the non-highlighted image:
Private Sub P2_MouseLeave(sender As Object, e As EventArgs) Handles P2.MouseLeave
P1.Visible = True
P2.Visible = False
End Sub
However switching picture boxes like this is not optimal. I recommend you to look into how you can use Application Resources, then modify your code to only switch the image that one picture box displays.
Here are the basic steps:
Right-click your project in the Solution Explorer and press Properties.
Select the Resources tab.
To add an image either:
a. Drag and drop the image onto the resource pane.
b. Click the arrow next to the Add Resource... button and press Add Existing File....
Now, in your code add this right below Public Class Form1:
Dim ButtonNormal As Image = My.Resources.<first image name>
Dim ButtonHighlighted As Image = My.Resources.<second image name>
Replace <first image name> and <second image name> with the names of your button images.
Now you only need one picture box for the button:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
P1.Image = ButtonNormal
End Sub
Private Sub P1_MouseEnter(sender As System.Object, e As System.EventArgs) Handles P1.MouseEnter
P1.Image = ButtonHighlighted
End Sub
Private Sub P1_MouseLeave(sender As System.Object, e As System.EventArgs) Handles P1.MouseLeave
P1.Image = ButtonNormal
End Sub
I'll start by saying i'm not a programmer by trade, and i'm sure someone will point out better ways of doing these things, but in regards to the threading question it's fairly simple to implement.
Imports System.Threading
Public Class Form1
Dim WorkerThread As New Thread(AddressOf DoWork)
'WorkerThread' can be any name you like, and 'DoWork' is the name of the sub you want to run in the new thread, and is launched by calling:
WorkerThread.start()
However there is a catch, the new thread is not able to interact directly with the GUI, so you cannot change textbox text etc... I find the easiest way to get changes made to the GUI is to drag a timer onto your form, and have the new thread change variables (pre-defined just below Public Class Form1), then use the Timer1 Tick event to monitor the variables and update the GUI if there are any changes.

VB .NET application not exiting when all forms close

I wanted to create an application with two forms, a main one called Source and a subsidiary called Viewer. Using buttons on each form, the user could switch to the other, even if that form had previously been closed. So I set my program to Close When Last Form Closes. Moreover, I added message boxes when each form closed, just to check that the user was sure.
But now my application won't exit after I close both forms! It shows up in Task Manager, and if I run it in MSVS, the debugger never stops! When each form is closed, My.Application.Forms returns an empty collection. And if I force quit it using End/Application.Exit, the program still quits — see the MWE below. What do I do?
MWE
Create a blank VB.NET Windows Forms project. In the "Application" tab of your project's settings, choose "Close When Last Form Closes." In the designer, create a Form Source with one button.
Public Class Source
Public Sub Switch(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Viewer.Show()
End Sub
Public Sub Free(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
e.Cancel=(MsgBoxResult.Cancel=MsgBox("Are you sure?"))
End Sub
'Public Sub Test(sender As Object, e As EventArgs) Handles Me.Closed
'Debug.Assert(Not My.Application.Forms.Count))
'If the next line is uncommented, the application will close, like we want
'If Not My.Forms.Viewer.Visible Then Application.Exit()
'End Sub
End Class
Then create an identical form called Viewer.
Public Class Viewer
Public Sub Switch(sender As Object, e As EventArgs) Handles Button1.Click
My.Forms.Source.Show()
End Sub
Public Sub Free(sender As Object, e As FormClosingEventArgs) Handles Me.Closing
e.Cancel=(MsgBoxResult.Cancel=MsgBox("Are you sure?"))
End Sub
'Public Sub Test(sender As Object, e As EventArgs) Handles Me.Closed
'Debug.Assert(Not My.Application.Forms.Count))
'If the next line is uncommented, the application will close, like we want
'If Not My.Forms.Source.Visible Then Application.Exit()
'End Sub
End Class
Try closing any form last. Neither will cause the app to quit.
Don't use Form.Closing and Form.Closed
In MSDN's page on Form.Closing, it remarks:
Caution
The Closing event is obsolete in the .NET Framework version 2.0 [and above]; use the FormClosing event instead.
The events were deprecated because
The Form.Closed and Form.Closing events are not raised when the Application.Exit method is called to exit your application. If you have validation code in either of these events that must be executed, you should call the Form.Close method for each open form individually before calling the Exit method.
The MSDN documentation nowhere states this, but the reverse is true: in order for your form to signal to the VB.NET runtime that the form closed, Form.Closing and Form.Closed cannot be subscribed to.
If you replace Me.Closing with Me.FormClosing and Me.Closed with Me.FormClosed in your MWE, it works.

Create And Implement a Custom Form in VB.NET

I am trying to create a customized form class (CustomBorderlessForm) in VB.NET.
My Progress So Far
I created a new Class and named it CustomBorderlessForm.vb
I then proceeded to write the following code:
CustomBorderlessForm.vb
Public Class CustomBorderlessForm
Inherits Form
Dim _form As Form = Nothing
Public Sub New(form As Form)
_form = form
MsgBox("Testing: New()")
End Sub
Protected Overrides Sub OnMouseMove(e As MouseEventArgs)
MyBase.OnMouseMove(e)
MsgBox("Testing OnMouseMove()")
End Sub
End Class
Form1.vb
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
End Sub
End Class
Results of progress
A message box displays "Testing: New()" on load
Nothing shows on mouse move
As you can see, my problem lies with the events
Questions
Is it possible to create a form object and use that instead of the pre-populating form?
If so, can I give this form custom properties, such as, a border and some boolean values (shadow...etc), just like any other custom object/class?
What am I doing wrong in my current approach?
Why isn't the OnMouseMove being overridden?
Am I initialising the class wrong?
Can it even be done this way?
After creating a form you also need to show it. Change your logic to:
Dim form As New CustomBorderlessForm(Me)
form.Show()
Before you do that, I'd recommend changing from MsgBox to Console.WriteLine(), otherwise you can run into a fun/frustrating little cat and mouse game.
EDIT
Based on the comments, if, from VS you did a "Add New, Windows Form" you can just right-click the project, select property and on the Application tab change the Startup object to your new form. VS only allows you to do this with forms it creates for you (by default, more on this later).
If you wrote that file by hand (which is absolutely fine) you can perform the Show() like I did above and call Me.Hide() to hide the "parent" form. Unfortunately the Load event is fired before the Show event so if you place this in Form1_Load() it won't work. Instead you can use the Shown event like this:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim form As New CustomBorderlessForm(Me)
form.Show()
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.Hide()
End Sub
Another option has to do with "Application framework". You can read about it here however it basically handles application events that other languages have to manually implement. If you go into your project properties you can uncheck the "Enable application framework" checkbox. This will give you more option in the "Startup object" dropdown. If you add the following code to your project one of the items in the Startup object dropdown menu should now be "Loader"
Public Module Loader
<STAThread()>
Public Sub Main()
Dim form As New CustomBorderlessForm(Nothing)
form.ShowDialog()
End Sub
End Module
You'll notice that the above bypasses Form1 completely. Also, instead of Show() I'm using ShowDialog() because otherwise the form shows and then the program ends.

How to write a single click event in Visual Basic?

I have created a form that allow users to close a form by clicking anywhere on the enlarged picture form (There are 3 objects to consider) and go back to the other form, which is called: "frmPhone". There's an actual picture on the form: "frmPhonePics" which is what I'm using to accomplish what I'm trying to do (was unable to insert an image on here. Sorry.) What I want to do is write a single click event to close the large picture form to allow the user to close it absolutely anywhere in the form, but I don't know how to do that. Here's the code I have so far:
Private Sub frmPhonePics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Click
frmPhone.Show()
Me.Hide()
End Sub
It sounds as though you have a picture on your frmPhonePics form. If you double click that (from the VBA editor), you should be taken to the code - for example, you might see
Private Sub Image1_Click()
End Sub
Now all you have to do is add your code there:
Private Sub Image1_Click()
Me.Hide
frmPhone.Show()
End Sub
Note - the order matters, since frmPhone.Show() will "hijack" the code flow until it's dismissed, and in your code Me.Hide will not execute (so the form will not close) until frmPhone has been dismissed.
You can map the click handler for various object to one thing, if that is what you are asking:
Private Sub frmPhonePics_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Click, Handles picLarge.Click, Handles otherThing.Click
frmPhone.Show()
Me.Hide() ' should be Me.Close?
End Sub
Not sure why it is MyBase.Click in your code instead of Me.Click. Is this a subclassed form?
I'd strongly suggest using a DoubleClick instead of a single Click. The chances of an errant click doing the wrong thing is very great.
The easiest way is right from the designer. Write the sub routine, then for each control, in the properties window, click the events icon(thunderbolt) and assign the sub routine to the double-click event.
Alternatively, dispense with the Handles clause completely and use a series of Addhandler statements in the Load event handler. If you put a unique string in the names of the controls or if it's all the controls, you can iterate through the controls and use one addhandler statement for all of them
For Each c As Control In Me.Controls
AddHandler c.DoubleClick, AddressOf Ctrl_DoubleClick
Next
Private Sub Ctrl_DoubleClick(sender As Object, e As EventArgs)
'Do stuff
End Sub

Windows form closing automatically when parent window gets focus

VB Windows form Application.. I am developing an application of which part of the program is around configuration settings allowing the user to enter configuration items. When the menubar item for configuration menu is clicked on the main form the menu opens. This is fine but the mainform should not become active again until the configuration menu has closed. This does not happen right now and the mainform simply comes to the foreground and the configuration form goes to the background... I realize that coding an event on the Child form to handle this would not work because the child window loses control and the main form gains control.. I thought of coding a function as follows on the main form but it does not seem logical because i would have to add to it for everyform and do checking to make sure the child is actually open before trying to close it..
Private Sub Form1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
MailSettingsWindow.Close()
RentalSettingsWindow.Close()
End Sub
I did away with the above sub routine and used the below code as per the recomendation of using showdialog which works just as i was looking for.
Private Sub MailingAndEmailSettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MailingAndEmailSettingsToolStripMenuItem.Click
Dim MailConfig As New MailSettingsWindow()
MailSettingsWindow.Showdialog()
End Sub
I did away with the above sub routine and used the below code as per the recommendation of using ShowDialog which works just as I was looking for.
My code is as follows:
Private Sub MailingAndEmailSettingsMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles MailingAndEmailSettingsMenuItem.Click
Dim MailConfig As New MailSettingsWindow()
MailSettingsWindow.ShowDialog()
End Sub