VB.Net WebBrowser Navigate Only Working Once - vb.net

Hoping someone can help me with this. I have two separate but related Forms, one of which contains a WebBrowser control. The user fills out some information on Form 1 and clicks a button with the following code:
If Form2Shown = False Then
Dim memoscreen As New Form2
Form2Ref = memoscreen
memoscreen.show()
Form2Shown = True
memoscreen.TopMost = OptionOnTop
Else
Dim memoscreen As Form2
memoscreen = Form2Ref
memoscreen.TopMost = OptionOnTop
memoscreen.QuickRefresh()
End If
The QuickRefresh sub in Form2 is the method that navigates. It is called both when the form is loaded as well as manually in the code above:
Public Sub QuickRefresh()
Dim HM As Form1
HM = Form1Ref
Me.Text = "retrieving information..."
Me.AxWebBrowser1.Navigate("SomeValidURL")
HM.Focus()
HM.SetHugoFocus()
End Sub
The problem I'm having is that the first time QuickRefresh is called (i.e. when Form2 is loaded) the navigation is successful and the page displays fine. If I then click the button on Form1 again, the page does not change. The Text attribute and window focus does change however, so I know the method is firing.
Some things I've tried/checked:
AllowNavigation is set to True on the WebBrowser control
Have tried looping while the browser is busy while calling Application.DoEvents()
Any suggestions would be appreciated. Thanks.

From your "Internet Options dialog > General Tab > Settings Button > Check for newer version of stored page" change that option to *Every Time I visit the webpage". That setting impacts how the webbrowser control deals with the refreshing.

Use the method refresh.
browser.Navigate("http://www.google.com") : browser.Refresh()

Related

Word VBA - easily remove form frame?

I'm trying to create a user entry form which both captures the users input and displays a status update message. The slickest way I think of doing it is to have my modal form for the user entry display over a modeless form. After the user enters their info and clicks OK, the info from the modal form is copied to the modeless form, the modal form is closed and status updates get pushed to the modless form as things change during processing:
Hopefully, with a lot of messing about with positions, it will look relatively seamless. My challenge is getting rid of the frame on my modal form. I've done a lot of searching and it seems to involve completely redrawing the form from base libraries - is there seriously no easier way to do it?
I would not use a 2nd form but just place a simple Frame on top of your form. When you want to show the "modal form", just set the visibility of the frame to True, and when you want to hide it, set it to False - all controls (in your case, the input field and the OK and Cancel button) that are placed on the frame are automatically shown or hidden.
If you have controls outside the "modal form" frame that you don't want to be active at that time, set them to enabled = False. You could handle this with a simple routine within your form.
In this example I have a frame FrameModal painted on top of the form. Note that this frame could be places over other controls.
Option Explicit
Private Sub UserForm_Activate()
showHideModalFrame False
End Sub
Private Sub buttonShowModal_Click()
' Show the "modal" dialog
showHideModalFrame True
End Sub
Private Sub buttonOK_Click()
' Do your stuff here...
Me.tbUpdates = Me.tbUpdates & vbCrLf & Me.tbInput
' Hide the "modal" dialog"
showHideModalFrame False
End Sub
Private Sub showHideModalFrame(show As Boolean)
Me.FrameModal.Visible = show
Me.buttonShowModal.Enabled = Not show
End Sub
Start the form
Click the show button:

What to use in test? Show or ShowDialog?

I'm writing a test for a Windows Forms User Control using Nunit and NCrunch. I want the test to do something like this:
<Test()> _
<TestCase("1")> _
<TestCase("2")> _
Public Sub Test(Value As String)
Dim CtlObj As MyUserControl
Using FormObj = New TEST_Form()
CtlObj = New MyUserControl()
CtlObj.Text = Value
FormObj.Controls.Add(CtlObj)
FormObj.Show()
' So test user can see the form.
Thread.Sleep(1000 * 5)
Assert.That(...)
End Using
End Sub
The above code shows the form but the area containing the user control is transparent and I see the images behind the form.
I've tried: Refresh, SuspendLayout & ResumeLayout. I've not found anything which will display the user control.
If I use ShowDialog the user control is displayed but of course the code waits for the user to click on something to close the form.
I want my test to run automatically w/o needing user action.
I updated my test code to use a BackgroundWorker. The BackgroundWorkder calls ShowDialog. The main code sleeps for several seconds and then closes the BackgroundWorker which shuts down the form.
But using a backgroundworker seems clunky.
Is there a way to show the form and usercontrol w/o the backgroundworker

How to stop a DataGridView removing columns on subsequent form loads? [duplicate]

I have a custom form which is open as Form.ShowDialog()
This form acts as a confirmation form. It asks a question whether you want to accept or decline the previously entered input in ComboBox & TextBox.
If you click OK, the input is saved into Excel File.
If you click Cancel, the input is not saved.
The problem I am having is that:
When you click cancel. The form.ShowDialog() is closed. (Which is fine.)
But when the form.ShowDialog() is open again. It retains the focus on the Cancel Button. So if you try to confirm the entry with "Enter" key, you cancel it instead.
My question is. Why does the Form.ShowDialog() retain the focus on the buttons after closing?
The Form.ShowDialog() has accept button "OK" [tabindex = 1], and cancel button "Cancel" [tabindex = 2] which are set to Enter key, and Esc key.
(To note again)The focus of the buttons remains after closing the form.
The portion of the code using the Dialog:
ElseIf ComboBoxBP.SelectedItem = ComboBoxBP.SelectedItem And TextBoxBP.Text = TextBoxBP.Text Then
form.Label1.Text = ComboBoxBP.SelectedItem
form.Label2.Text = TextBoxBP.Text
form.ShowDialog()
If form.DialogResult = Windows.Forms.DialogResult.Yes Then
SiE()
ElseIf form.DialogResult = Windows.Forms.DialogResult.No Then
LabelBPBot.Text = "Canceled."
End If
End If
When you use .ShowDialog() closing the form does not dispose of it as with a normal form. This is because once a Dialog "closes" it actually just hides so we can get info from it before it actually goes away.
The second issue is that forms are classes (it says so at the top of every one of them:)
Public Class Form1
...
So, instances of them should be created. VB allows Form1.Show or Form1.ShowDialog() to use a "default instance" and it is a shame that it does.
Combine these 2 tidbits and what you have is a case where the form you showed last time is still around in the same state as when you last used it, including the last focused control. You are only using a "fresh copy" of the form the first time, after that, you are just reusing the old instance. Remedy:
Using Dlg As New Form1 ' form1 is the class, dlg is the instance
... do stuff
Dim res As DialogResult = Dlg.ShowDialog()
If res = Windows.Forms.DialogResult.OK Then
'... do stuff
End If
End Using ' dispose of Dlg
Eventually, you will run into similar issues using the default instance of the other forms (LForm.Show). Just Say No to Default Form instances.

VB.NET Call Sub of another form

I know, that there are many questions related to this, but still I cannot find a workable solution.
Usually, it would work like this: A form creates an instance of another form in it's container like this:
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.TopLevel = False
PolInstIn.Name = "Sub From"
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
Me.GroupBox6.Controls.Add(PolInstIn)
PolInstIn.Show()
End Sub
Then it's easy to call a Public Sub from the sub form like this:
Call PolInstIn.MyPublicSubInSubForm1()
However, this doesn't work for me in this case. When I run MyPublicSubInSubForm1() it doesn't throw any error, but does no action. If I write a value to SubForm1 textbox and read it back, it reads, but I don't see it on the screen, so I suspect it is written to some other accidental instance.
I suspect it is because my parent form is also an instance of an form created in very similar way like SubForm1. Basically the ParentForm is a form loaded into tabPage and SubForm1 is a module loaded into ParentForm. It can exist in many copies (tabs).
Could you point to any simple solutions?
Regards,
Libor
I see this question got a lot of views, so here is an answer.
1) No visual response of child form (only results) - this could have happened if I created more then 1 instances of the form. The example is just an example, but if one use it (accidentally) this way, it may result in new definition of a child form every time (and consequent symptoms like the ones described). In practice, I split form loading from loading data into to the form (done by a public sub in that form).
2) If you want also a back reference (to i.e. parent grid form), define a Public ParentFormGrid as GridName (note ParentForm is a reserved name) and on loading a child form, set
PollInstIn.ParentFormGrid = Me
This way you can alway asccess the parent form, i.e. reload the grid when you save changes on a line edited in child form.
make Private Sub LoadDetail() to a public :
Public Sub LoadDetail()
It work on my project. Hopely its what you want
Dim PolInstIn As New SubForm1
Private Sub LoadDetail()
PolInstIn.Name = "Sub From"
PolInstIn.Show()
PolInstIn.TopLevel = False
PolInstIn.FormBorderStyle = Windows.Forms.FormBorderStyle.None
PolInstIn.Dock = DockStyle.Fill
PolInstIn.Update()
PolInstIn.Refresh()
Me.GroupBox6.Controls.Add(PolInstIn)
End Sub

Unable To Close A Form Embedded In A Panel

I have an issue that is really starting to do my head in...
My application has a ListView control that is populated with items. When you double click on an item it creates a new instance of a form. It then creates a new panel and adds the form to the panel. However, for the life of me I cannot work out how to close the form inside the panel.
Inside my DoubleClick event:
Dim frm As New frmStorePage(_store.Code, _store.Name)
'Create a new panel with the store page
Dim pnl As New Panel
pnl.Name = _store.Code
pnl.BackColor = SystemColors.Control
pnl.Size = New Size(1522, 892)
pnl.Location = New Point(3, 3)
frm.TopLevel = False
frm.Name = _store.Code
pnl.Controls.Add(frm)
frm.Show()
pnlStores.Controls.Add(pnl)
pnl.BringToFront()
...
Inside my Close event:
Dim panel As Panel = CType(pnlStores.Controls.Find(lsvOpenStoreList.SelectedItems(0).Name, False)(0), Windows.Forms.Panel)
For Each control As Control In panel.Controls
If TypeOf control Is Windows.Forms.Form And control.Name = panel.Name Then
control.Dispose()
End If
Next
pnlStores.Controls.Remove(panel)
panel.Dispose()
pnlStoreList.BringToFront()
...
I have also tried declaring my form as a global variable but still cannot seem to close it.
The form has a number of timer events that get stopped when the form closes, however, even though the panel gets closed, the timer events are still running. It seems that the form is still active in the background.
Any help would be appreciated.
Due to a silly oversight, I failed to see that the code I was using to dispose of all the controls and close the form was not in the FormClosing event, but actually under a Command Button called Close. Therefore, my closing procedures were never getting called and thus the timers remained active.
Thanks