example of GeckoFX - vb.net

I downloaded GeckoFX (ver 16), the XULRunner Dotnet wrapper to use in a winForms (VB.NET) application, but there are no instructions on usage anywhere (just the Initialize command).
I added the control onto my form and in the Form load event, put in the following:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Xpcom.Initialize(My.Application.Info.DirectoryPath & "/xulrunner")
InitializeComponent()
Me.GeckoWebBrowser1.Enabled = True
Me.GeckoWebBrowser1.Navigate("http://www.google.com")
End Sub
Nothing happens. The control is not visible, no navigation takes place.
Just a simple project (C# is fine too) that shows the control actually working would be nice (please do not answer with another URL that points to GeckoFx's wiki page as it is useless and no examples anywhere are shown)

Imports Gecko
Public Class Form1
Private myBrowser As GeckoWebBrowser
Public Sub New()
InitializeComponent()
Xpcom.Initialize(My.Application.Info.DirectoryPath & "\xulrunner")
myBrowser = New GeckoWebBrowser()
myBrowser.Parent = Me.SplitContainer1.Panel2
myBrowser.Dock = DockStyle.Fill
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
myBrowser.Navigate(TextBox1.Text)
End Sub
End Class

Just do it with Events. Im talking for withEvents:D
Imports Gecko
Public Class Form1
Private WithEvents myBrowser As GeckoWebBrowser
Public Sub New()
InitializeComponent()
Xpcom.Initialize(My.Application.Info.DirectoryPath & "\xulrunner")
myBrowser = New GeckoWebBrowser()
myBrowser.Parent = Me.SplitContainer1.Panel2
myBrowser.Dock = DockStyle.Fill
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
myBrowser.Navigate(TextBox1.Text)
End Sub
End Class

Related

Switching forms in vb.net

I am a volunteer for the National Park Service trying to convert an interactive display originally created 20 years ago in a language called ToolBook into Visual Basic. The program consists of several projects under a single solution. The starting project, called "MainMenu", can be thought of as a library, with buttons that bring up “books.” The project called Geology is an example “book” and GeologyMenu can be thought of as the index of a book. The buttons on GeologyMenu connect to “chapters” that explain and show examples of geologic processes in the park. The “chapters” are within the project “Geology” and work fine within the project. All forms used in the program have timers that allow the program to re-set itself to MainMenu when not in use.
In a previous post, with the help of Idle Mind (thank you again), the following code for works fine for going from MainMenu to GeologyMenu and in the reverse direction as long as no button is pushed on GeologyMenu. However, if I go to a “chapter” I can no longer get back to the MainMenu from the GeologyMenu. Here is the relevant code:
MainMenu
Public Class frmMainMenu
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New Geology.frmGeologyMenu
AddHandler formNew.FormClosed, AddressOf formNew_FormClosed
TimerMain.Stop()
formNew.Show()
Me.Hide()
End Sub
Private Sub formNew_FormClosed(Sender As Object, e As FormClosedEventArgs)
lblTime.Text = 8
TimerMain.Start()
Me.Show()
End Sub
GeologyMenu
Public Class frmGeologyMenu
Public Sub frmGeologyMenu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblTime.Text = 6
TimerGeologyMenu.Enabled = True
Me.Show()
End Sub
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
TimerGeologyMenu.Stop()
frmErosionP01.Show()
Me.Hide()
End Sub
The code below for takes the viewer to the Erosion “chapter”
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
TimerGeologyMenu.Stop()
frmErosionP01.Show()
Me.Hide()
End Sub
Erosion “Chapter” . This is the code for the button on every form in Erosion that takes the program back to GeologyMenu
Public Class frmErosionP02
Private Sub BtnGeologyMenu_Click(sender As Object, e As EventArgs) Handles btnGeologyMenu.Click
My.Computer.Audio.Stop()
frmGeologyMenu.lblTime.Text = 10
frmGeologyMenu.TimerGeologyMenu.Enabled = True
frmGeologyMenu.Show()
Me.Close()
End Sub
The code for forms within Erosion takes me back to GeologyMenu, but then MainMenu won’t show when I close GeologyMenu and I don’t understand why or how to fix it. Thank you in advance for your help!
Simply, pass the previous menu/Form to the new one in a parameterized constructor and keep it in a class variable, then handle the Form.Closed event of the new menu to show the previous one.
Example for the relevant code:
Public Class frmMainMenu
Inherits Form
Private Sub BtnGeology_Click(sender As Object, e As EventArgs) Handles btnGeology.Click
Dim formNew As New frmGeologyMenu(Me)
Me.Hide()
formNew.Show()
End Sub
End Class
Public Class frmGeologyMenu
Inherits Form
Private PreviousMenu As Form
Private Sub New()
InitializeComponent()
'...
End Sub
Sub New(prevMenu As Form)
Me.New()
PreviousMenu = prevMenu
AddHandler FormClosed,
Sub(s, e)
PreviousMenu.Show()
End Sub
End Sub
Private Sub BtnErosion_Click(sender As Object, e As EventArgs) Handles btnErosion.Click
Dim frmErosion As New frmErosionP02(Me)
Me.Hide()
frmErosion.Show()
End Sub
End Class
Public Class frmErosionP02
Inherits Form
Private PreviousMenu As Form
Private Sub New()
InitializeComponent()
'...
End Sub
Public Sub New(prevMenu As Form)
Me.New()
PreviousMenu = prevMenu
AddHandler FormClosed,
Sub(s, e)
PreviousMenu.Show()
End Sub
End Sub
End Class

form opening on load

My program was opening a different form to what I wanted it to. The answers solved it.
Basically I wanted to stop a form opening when the program started, but when it opened manually (on a button press), it updated the data. The second part of the problem has not been solved, but the first part has been.
You can try something like this:
Public Class HomeForm
Private WithEvents m_DataChangeForm As DataChangeForm
Private Sub HomeForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
m_DataChangeForm = New DataChangeForm()
m_DataChangeForm.Show()
End Sub
Private Sub OnDataSourceChanged(sender As Object, args As EventArgs) Handles m_DataChangeForm.OnDataSourceChanged
MessageBox.Show("Data source changed!")
End Sub
End Class
Public Class DataChangeForm
Inherits Form
Public Event OnDataSourceChanged(sender As Object, args As EventArgs)
Private WithEvents m_Button As Button
Public Sub New()
m_Button = New Button()
m_Button.Text = "Change"
m_Button.Parent = Me
End Sub
Public Sub buttonClick(sender As Object, args As EventArgs) Handles m_Button.Click
RaiseEvent OnDataSourceChanged(sender, args)
Me.Close()
End Sub
End Class
Reason your form is displayed before HomeForm is becaouse you call ShowDialog, it blocks until DataChangeForm is closed.
You should move your code from "Load" to "Shown" Event.
Private Sub Homefrm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Using fp = New dataChangefrm(m_database)
If fp.ShowDialog() = DialogResult.OK Then
uwgHome.DataSource = Nothing
loadData()
End If
End Using
Me.Location = New Point(0, 0)
loadData()
End Sub
Please have a look on the Handle in the first line. It depends on your Project.

How to handle an button click even from a .dll

Could someone please guide me as to how I would get this actually working? Currently it gives me errors about WithEvents - although simplified it shows the form - but I have no clue what that actually means. It's an toolbox I'm making just to allow the user to better interact with some of my other code.
All I need is the form visuals/guts to be custom, but then the code to be done within my application (which doesn't have visual editing capabilities).
Is this a case where I need to use interfacing/partial classes/inheritance, or can this easily be accomplished with just some minor tweaking to what I have?
(Form created in visual studio form designer and then changed to a class library. Application code written in Autodesk Inventor "rule" environment)
Thanks!
Application code:
AddReference "C:\Users\Documents\Visual Studio 2013\Projects\WindowsApplication1\WindowsApplication1\bin\Release\SectionSymToolBox.dll"
Imports System.Windows.Forms
Public Class SectionSymRule
'Public dlg As New System.Windows.Forms.For
Public Shared ToolBox As New SectionSymToolBox.SectionSymToolBox
Dim WithEvents EClass As New EventClass
Sub Main()
ToolBox.Show()
End Sub
End Class
Form code:
Public Class SectionSymToolBox
Private Sub Main()
End Sub
Public Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Swap Symbols
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Flip Symbol
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Flip Text
End Sub
Public Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'<
End Sub
Public Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
'>
End Sub
End Class
Is this what you are looking for?
AddHandler ToolBox.Button1.Click, AddressOf HandlerMethodHere
Where HandlerMethod is a method that has the same signature as Button1.Click so for example:
Private Sub HandleButton1Click(sender As Object, e As EventArgs)
'Code here
End Sub
Obviously, replace Button1 with the name of the button.

How access methods of another classes?

I'm with troubles for access methods of another class "SocketClient" in a "Form2", but in "Form1" works very fine! In other words, I can send data from "Form1" using methods of "SocketClient", but the same thing I don't can do from "Form2".
How solve it?
Here is my code:
"Form1"
Public Class Form1
Public WithEvents C As New SocketClient
Public Yy As String = "|SPLIT|"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
C.Send("ref" & Yy & "data here")
End Sub
End Class
"Form2"
Public Class Form2
Public frm1 As Form1
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
' The error is here, don't sends data to server application
frm1.C.Send("ref" & frm1.Yy & "Name: " & TextBox1.Text)
End Sub
End Class
Assuming you have Form1 as the main form and you are creating Form2 from that main form, try passing a reference via the constructor:
Public Class Form2
Private frm1 As Form1
Public Sub New(mainForm As Form1)
Me.InitializeComponent()
frm1 = mainForm
End Sub
End Class
From your main form (form1), you would pass the reference:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form2 As New Form2(Me)
form2.Show()
End Sub

Closing form after printing a web browser control contained in it

I'm taking some random print through the html and web browser control in vb.net winforms
Here is my code
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myWebBrowser As New WebBrowser
AddHandler myWebBrowser.DocumentCompleted, AddressOf DocumentCompleted
myWebBrowser.Navigate("http://www.bing.com")
End Sub
Private Sub DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
With DirectCast(sender, WebBrowser)
If .ReadyState = WebBrowserReadyState.Complete Then
.Print()
End If
End With
End Sub
End Class
I want the form to be closed after the print. Now if I write Me.Close() after .Print() nothing is being printed. what should I do to achieve this?
Any help is appreciated.
::Update::
after #Noseratio's suggestion I tried to handle the event onafterprint in my html and tried to invoke Me.Close() using ObjectFoprScripting set to my form. But that is firing the close method without any print.
Here is my code
script tag in my html page
<script>
function window.onafterprint() {
window.external.Test('called from script code');
}
</script>
VB.net Code of my form
Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
WebBrowser1.AllowWebBrowserDrop = False
WebBrowser1.IsWebBrowserContextMenuEnabled = False
WebBrowser1.WebBrowserShortcutsEnabled = False
webBrowser1.ObjectForScripting = Me
WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
End Sub
Public Sub Test(ByVal message As String)
MessageBox.Show(message, "client code")
Me.BeginInvoke(DirectCast(Sub() Me.Close(), MethodInvoker))
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
WebBrowser1.Print()
End Sub
End Class
Found my solution
No need to handle onafterprint in javascript.
Here is what I did,
Step 1
Added a reference to SHDocVw.dll in my project. This can be found in c:\windows\system32 folder.
Step2
My new updated code
Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
WebBrowser1.AllowWebBrowserDrop = False
WebBrowser1.IsWebBrowserContextMenuEnabled = False
WebBrowser1.WebBrowserShortcutsEnabled = False
WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
End Sub
Private Sub WebBrowser1_DocumentCompleted_1(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
Dim wb As WebBrowser = TryCast(sender, WebBrowser)
Dim ie As SHDocVw.InternetExplorer = DirectCast(wb.ActiveXInstance, SHDocVw.InternetExplorer)
AddHandler ie.PrintTemplateInstantiation, AddressOf IE_OnPrintTemplateInstantiation
AddHandler ie.PrintTemplateTeardown, AddressOf IE_OnPrintTemplateTeardown
'Just to get reference of the webBrowser1 control in ie events, uncomment the below line
'ie.PutProperty("WebBrowserControl", DirectCast(wb, Object))
wb.Print()
End Sub
Private Sub IE_OnPrintTemplateInstantiation(pDisp As Object)
' The PrintTemplateInstantiation event is fired when the print job is starting.
End Sub
Private Sub IE_OnPrintTemplateTeardown(pDisp As Object)
' The PrintTemplateTeardown event is fired when the print job is done.
'Just to get reference of the webBrowser1 control, uncomment the below line
'Dim iwb2 As SHDocVw.IWebBrowser2 = TryCast(pDisp, SHDocVw.IWebBrowser2)
'Dim wb As WebBrowser = DirectCast(iwb2.GetProperty("WebBrowserControl"), WebBrowser)
Me.Close()
End Sub
End Class