How can I embed a CefSharp webbrowser control in vb.net? - vb.net

I need the dll files to be included in the form controls so as to insert the web browser control in the form itself ... how can I do? This time I really need help because I'm going crazy; if you help me, do me a big favor!

When you ad reference, add the browser control via code (as you can't do it via designer).
For example:
Public Sub New()
InitializeComponent()
Dim CefSharp_Settings As New CefSettings With {
.ResourcesDirPath = Application.StartupPath,
.LocalesDirPath = Application.StartupPath & "\locales",
.CachePath = Application.StartupPath & "\CefSharp Cache"
}
CefSharp_Settings.CefCommandLineArgs.Add("persist_session_cookies", "1")
CefSharp.Cef.Initialize(CefSharp_Settings)
chromeBrowser = New ChromiumWebBrowser("www.google.com") With {
.Dock = DockStyle.Fill
}
Me.Controls.Add(chromeBrowser)
End Sub

Related

VB.Net Visual basic Adding a custom event in the Webbrowser control that calls a routine in the main application

I'm trying to create and call a custom event in the webbrowser control and everything that I've tried to do causes one error or another when the webpage executes the code. What I'm doing is adding a button on each row of a table to facilitate removing that row. However, the master list of data is in the application. When the script in the web page executes, I need to update the master list in the application. My thoughts were to call a custom event that will be fired in my application where I can do everything that I need to do. I just can't make this work. here are more details of what I have right now. Here is the html code for a given row:
Dim M As String = "</TD><TD>"
RetStr.Append("<TR ID='" & Me.Manifest & "' name='" & Me.Manifest & "'>")
RetStr.Append("<TD>").Append(CompanyID).Append(M).Append(CompanyName).Append(M)
RetStr.Append(ContactName).Append(M).Append(Address1).Append(M).Append(Address2)
RetStr.Append(M).Append(City).Append(M).Append(State).Append(M)
RetStr.Append(Zip).Append(M).Append(Phone).Append("</TD>")
RetStr.Append("<TD><button onclick='deleteRow(""" & Me.Manifest & """)'>Remove</button></TD>")
Return Replace(RetStr.ToString(), "<TD></TD>", "<TD> </TD>")
Here is the code that is in the function:
Dim HTMLOut As New List(Of String)
HTMLOut.Add("<HEAD>")
HTMLOut.Add(" <SCRIPT language=""VBScript"">")
HTMLOut.Add(" Function deleteRow(rowid)")
HTMLOut.Add(" set row = document.getElementById(rowid)")
HTMLOut.Add(" row.parentNode.removeChild(row)")
HTMLOut.Add(" dispatchEvent(Row)")
HTMLOut.Add(" End Function")
HTMLOut.Add(" </SCRIPT>")
HTMLOut.Add("</HEAD>")
HTMLOut.Add("<BODY>")
HTMLOut.Add(" <TABLE border='1' style='font-size:12;' NAME='Table' ID='TABLE'>")
Here is the code that I have in the application:
Private Sub WB_DocumentCompleted(sender As Object, e As
WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
WB.Document.AttachEventHandler("UpdateList", New EventHandler(
Function(ByVal s As Object, ByVal k As EventArgs)
MsgBox("BOO")
Return True
End Function))
End Sub
Any help in any direction, even if it means I need to change how I'm doing all of this, is very welcomed! There is more code then this, it's stripped down to what is needed to convey what I'm doing. I know I'm missing something, I just can't figure out what it is. The end goal is to update the master list in the application hosting the web browser; ideas suggestions and comments are always welcome. As a side note, I'm using the web browser control because the final part of the process is to create a file and sftp it to the vender (the application will do this), and print the report. Thanks!
I figured this out. I needed to create a class object, with the comvisible attribute set and add this to the objectForScripting property of the web browser control.
Imports System.Runtime.InteropServices
<ComVisible(True)> Public Class WBClassCode
Public Sub UpdateStuff(ByVal Data)
'My code goes here... called from the web page.
MsgBox("boo")
End Sub
End Class

How to check for strings in a panel?

I'm making a string checker which should check for a string on a website.
I'm using a panel instead of a web browser because I merged chromium with CefSharp into it.
Now I can't figure out how to check for a string in the panel.
I tried creating a different type of variable, but the variable comes from a file.
I also tried for normal text without variables.
Both didn't work.
Public Sub New()
Dim site As String = File.ReadAllText("webstore.shd")
Dim id As String = File.ReadAllText("ID.shd")
InitializeComponent()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
browser = New ChromiumWebBrowser(site) With {
.Dock = DockStyle.Fill
}
Panel1.Controls.Add(browser)
If Panel1.Contains(id) Then
' I am stuck right here.
msgbox("Succes")
Else
msgbox("Nope")
End If
End Sub
I expected that the program would search for the variable in the panel, but it wouldn't do that.

Embed a Google Business View Virtual Tour

I would like to embed this to my windows form.
Private Sub InitializeComponent()
Try
Me.VirtualView = New WebBrowser
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(347, 261)
Me.Name = "frmVirtual"
Me.Text = "frmVirtual"
Me.VirtualView.ScriptErrorsSuppressed = True
Me.VirtualView.Name = "frmVirtual"
Me.VirtualView.DocumentText = "<html><body><iframe width='600' height='450' style='border: 0' src='https://www.google.com/maps/embed?pb=!1m0!3m2!1sen!2sau!4v1481252003737!6m8!1m7!1sF%3A-pwYGx-oWTIk%2FWC6LeJuIxdI%2FAAAAAAAABIg%2FphppDvMZr54JiWnLbsbUgDcTGUfGXLMRACLIB!2m2!1d-33.76525136331761!2d150.9088391438127!3f310!4f0!5f0.7820865974627469' frameborder='0' allowfullscreen></iframe></body></html>"
Catch ex As Exception
MessageBox.Show(ex.Message.ToString(), "Unable to Retrieve")
End Try
End Sub
But nothing displays on this code. Please help. Thank you.
You should not be navigating the address in the InitializeComponent method, use the Load() method of the class to navigate. for this particular issue you need to set the DocumentText property of the WebBrowser control.
On another note you are creating a new instance of the WebBrowser control, but are not adding it anywhere I could see. If you don't add it you will never see this control. IMO just drag and drop a new one where you need it.
Me.VirtualView.ScriptErrorsSuppressed = True
Me.VirtualView.DocumentText = "<html><body><iframe width='600' height='450' style='border: 0' src='https://www.google.com/maps/embed?pb=!1m0!3m2!1sen!2sau!4v1481252003737!6m8!1m7!1sF%3A-pwYGx-oWTIk%2FWC6LeJuIxdI%2FAAAAAAAABIg%2FphppDvMZr54JiWnLbsbUgDcTGUfGXLMRACLIB!2m2!1d-33.76525136331761!2d150.9088391438127!3f310!4f0!5f0.7820865974627469' frameborder='0' allowfullscreen></iframe></body></html>"
What I did here was you need to wrap the iframe inside a html and body tags.
Note: I added the ScriptErrorsSuppressed to true because there are script error's when loading that. Also look into the new way to embed these maps, they require an API key to use in your calls here

Opening pdf from Crystal Reports Viewer

Thank you for viewing my question. I am building a project in Visual Studio 2010 using vb and .net. I have a Crystal Reports report that I'm trying to have auto export and open in PDF with a button click. Right now I am using Crystal Reports Viewer in my project which opens the report fine; However, I would like to have it only open in a pdf format. Is there a way to do this?
Note: I'm not here hunting for code. I'm wanting to learn, so if you could just guide me in the right direction, that will be great (if you don't want to provide code)!
Thank you for the help.
Josh
I'm using code from http://www.codeproject.com/Articles/14549/Crystal-Reports-To-PDF-converter-Without-Crystal-R
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class clsCrystalToPDFConverter
Dim ConInfo As New CrystalDecisions.Shared.TableLogOnInfo
Dim oRDoc As New ReportDocument
Dim expo As New ExportOptions
Dim sRecSelFormula As String
Dim oDfDopt As New DiskFileDestinationOptions
Dim strCrystalReportFilePath As String
Dim strPdfFileDestinationPath As String
Public Function SetCrystalReportFilePath(ByVal CrystalReportFileNameFullPath As String)
strCrystalReportFilePath = CrystalReportFileNameFullPath
End Function
Public Function SetPdfDestinationFilePath(ByVal pdfFileNameFullPath As String)
strPdfFileDestinationPath = pdfFileNameFullPath
End Function
Public Function SetRecordSelectionFormula(ByVal recSelFormula As String)
sRecSelFormula = recSelFormula
End Function
Public Function Transfer()
oRDoc.Load(strCrystalReportFilePath) 'loads the crystalreports in to the memory
oRDoc.RecordSelectionFormula = sRecSelFormula 'used if u want pass the query to u r crystal form
oDfDopt.DiskFileName = strPdfFileDestinationPath 'path of file where u want to locate ur PDF
expo = oRDoc.ExportOptions
expo.ExportDestinationType = ExportDestinationType.DiskFile
expo.ExportFormatType = ExportFormatType.PortableDocFormat
expo.DestinationOptions = oDfDopt
oRDoc.SetDatabaseLogon("PaySquare", "paysquare") 'login for your DataBase
oRDoc.Export()
End Function
End Class
You'll need to set the variables to the specifics of your project obviously. These are more than likely the classes and methods you'll be wanting to use however. This should allow you to take your crystal reports viewer file and turn it into something opened by PDF
This works for me.
Dim orpt As CrystalDecisions.CrystalReports.Engine.ReportDocument
orpt = DirectCast(crvInvoice.ReportSource, CrystalDecisions.CrystalReports.Engine.ReportDocument)
orpt.ExportToDisk(ExportFormatType.PortableDocFormat, "PdfFileName.pdf")

How to close parent form and open child?

Hey guys before I was just hiding the parent form, but now when I try to read from the parent file it says it can't because it's already running in a process. I followed some tutorial and it said to go to the project properties and have the application stop running when all the forms are closed.
But now since I did that it says the directory can't be found probably because I am reading the input from the parent form. Anyways here is my code
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))
How should I go about doing this?
Edit:
Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs) Handles btnHunter.Click
selection = "Hunter"
writeData.classSelection()
End Sub
This is what I have when the button is clicked.
Here is the classSelection sub:
Public Sub classSelection()
If frmClass.selection = "Hunter" Then
writeFile1.WriteLine(frmClass.selection)
End If
If frmClass.selection = "Gatherer" Then
writeFile1.WriteLine(frmClass.selection)
End If
If frmClass.selection = "Farmer" Then
writeFile1.WriteLine(frmClass.selection)
End If
writeFile1.Close()
End Sub
The error points to this line:
If frmClass.selection = "Hunter" Then
Saying part of the file path cannot be found.
If you want to read input textbox in closed parent form, you have to declare public var
Make a new module in your project .. and add this
public sLogin as String
And before you hide or close frmLogin .. add this
sLogin = txtUser.Text
So, you could change your code with
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & sLogin))
matzone has given you a good hint. And to check exactly what your path is, just add a MessageBox using variables :
Dim writePath1 As String
Dim writeFile1 As StreamWriter
writePath1 = "C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & sLogin
If MessageBox.Show(writePath1, "Continue ?", MessageBoxButtons.YesNo) = DialogResult.Yes Then
writeFile1 = New StreamWriter(File.OpenWrite(writePath1))
' ...
writeFile1.Close() ' Very important ! Adrian pointed it out.
End If
^^ and if it works, you can discard the Dialog test or replace it by some test code like If File.Exists(...)
However, I don't understand wether you want to close the parent Form or hide it. It's different !
Closing the parent Form will discard any access to parent Form members, including txtUser.Text.
If you want to close the parent Form, the ChildForm should not be a child of that parent you are trying to close, or you must just hide the parent Form :
frmLogin.Hide() ' Not frmLogin.Close()
If you close frmLogin, frmLogin.txtUser won't be accessible, or use sLogin provided by matzone instead. Alternatively, you should pass frmLogin.txtUser.Text value to a custom property of ChildForm.
Imports System.IO
Public Partial Class ChildForm1
' Inherits System.Windows.Form
' ...
Private _txtUserFile As String
Public WriteOnly Property TxtUserFile() As String
Set(ByVal NewFileName As String)
_txtUserFile = NewFileName
End Set
End Property
Public Sub LoadFile()
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" & txtUserFile))
' ...
writeFile1.Close()
End sub
' ...
End Class
Then use this in parent Form :
MyChildForm.TxtUserFile = Me.txtUser.Text
' Me.Close() ' This will definately KILL Form1 (the parent one)
Me.Hide() ' Always use Hide() until you'll terminate your Application
MyChildForm.Show()
MyChildForm.LoadFile()
^^ but this is not a good code either ! Your problem remains unclear (at least for me)
"Still saying it can't find part of the path", then check the path..
Does the file actually exists ?
Does the path contains glitch ? (use the provided MessageBox test)
Does your account can access that directory ? (Windows configuration and account levels)
Well !
In fact, the problem could be somewhere else.
For example, I was able to reproduce your exception by providing an empty string [""] as the value of, either :
frmLogin.txtUser.Text ' = ""
' or
sLogin ' = ""
' or
txtUserFile ' = ""
In fact, I get the "Could not find a part of the path..." exception because the StreamWriter couldn'd read/write to a File, as I didn't provided a valid FileName for that file. As the filename parameter was an empty string "", the provided path for StreamWriter was just representing a directory instead of a file and an exception was raised.
Now, you should check wether you have a valid path before building a new instance of StreamWriter to get sure you are actually pointing to a File ?
Dim writeFile1 As StreamWriter
Dim MyEntirePath As String = "C:\Users\...\Accounts\" + frmLogin.txtUser.Text
MessageBox.Show(MyEntirePath) ' would be enough to be sure your path is correct
' Some test code here...
If everythingOK then ' create the StreamWriter...
writeFile1 = New StreamWriter(MyEntirePath)
' ...
' ...
Also, it's not a good idea to create your streamwriter, and use it in another part/method of your code. You never known if one day, you'll change your code, and forget to make the link between
Dim writeFile1 As StreamWriter = New StreamWriter(File.OpenWrite("C:\Users\Nick\Documents\Visual Studio 2010\Projects\LoginFixed\Accounts\" + frmLogin.txtUser.Text))
' plus
Private Sub btnHunter_Click(sender As System.Object, e As System.EventArgs)
...
End Sub
' plus
Public Sub classSelection()
...
writeFile1.Close()
End Sub
^^ too much "here and there"...
You'll obviously also get an exception if you try to click btnHunter twice.. I don't know what is the purpose of your code nor how it works, it looks like a game.. But I would use File.Exist(..) checks, create the file before, if none, and put that in a Try/Catch to check if I eventually don't have administrator rights to write to that directory. Otherwise, make a code that allow user to read/write files to a custom folder. Andalso, you have :
Application.StartupPath
^^ Very usefull, like :
Dim MyFilePath As String = Application.StartupPath + "\Datas\MyText.txt"
After two weeks of coding, I usually forget where I put those "C:\blabla.." or "D:\gnagna\" or what classes actually uses those absolute reference paths. I've dropped this way of getting directories long ago since the day I moved to Win7 on another computer and all such applications I developped using that approach was doomed...