call a dim from another form - vb.net

Hello iam trying to call browser from my main form but it is not visible when i type FrmWebBrowser.browser.
this is the code from FrmWebBrowser.
Imports CefSharp
Imports CefSharp.WinForms
Public Class FrmWebBrowser
Public Sub FrmWebBrowser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim settings As New CefSettings()
Dim browser = New ChromiumWebBrowser("http://google.com/") With {
.Dock = DockStyle.Fill
}
Me.Controls.Add(browser)
End Sub
End Class

Just declare browser at the beginning of your form.
dim browser as ChromiumWebBrowser
Public Sub FrmWebBrowser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim settings As New CefSettings()
browser = New ChromiumWebBrowser("http://google.com/") With {
.Dock = DockStyle.Fill
}
Me.Controls.Add(browser)
End Sub

Related

Thread inside module and add value into me.texbox1

Have anyone idea why my code return "" inside my Textbox ? :-)
This i have in main Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim My_Thread as Threading.Thread
My_Thread = New Threading.Thread(AddressOf Module1.MyTest)
My_Thread.Start()
End Sub
And this in module1
Sub MyTest()
Dim TestingValue as string = "Test"
MainForm.Textbox1.Text = TestingValue
End sub
Invoke all time crash code and another try return "" inside texbox1 :-/
Create a Sub Class with public declarations to any object you want on MainForm, then pass the class as a parameter to the module. This would be a cleaner approach than passing the entire Form class instance. Then using the method that Jimi has suggested you can setyou textboxs without cross thread violation.
Public Class MainForm
Public Class PassToModule
Public TxBx1 As TextBox = MainForm.TextBox1
Public TxBx2 As TextBox = MainForm.TextBox2
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim PassToModule As New PassToModule
Dim My_Thread As Threading.Thread
My_Thread = New Threading.Thread(AddressOf MyTest)
My_Thread.Start(PassToModule)
End Sub
End Class
Module Module1
Dim FromMainForm As MainForm.PassToModule
Sub MyTest(PasToModule As MainForm.PassToModule)
FromMainForm = PasToModule
FromMainForm.TxBx1.BeginInvoke(New MethodInvoker(Sub()
FromMainForm.TxBx1.Text = "Test"
FromMainForm.TxBx2.Text = "Test"
End Sub))
End Sub
End Module

GetElementById with CefSharp

I'm trying to access to a website variable by it's ID in VB.net. The ID is "value", the data I'm trying to access is the stock price, and the website is linked in the code. I was using the built-in web browser with the next code:
Imports System.Xml
Imports System.Net
Public Class Bolsa
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate("https://www.ahorro.com/acnet/fichas/ficha_valor.acnet?isin=ES0113211835&marketCode=09&submarketId=09")
While Not WebBrowser1.ReadyState = WebBrowserReadyState.Complete
Application.DoEvents()
End While
Dim request As String = WebBrowser1.Document.GetElementById("value").InnerText
Dim s As String = request.Replace("<span>", Nothing)
Dim t As String = s.Replace("</span>", Nothing)
TextBox1.Text = t
End Sub
End Class
Now I'm using CefSharp plugin, because I need HTML5 support, but I cannot access the data, and I think the method is correct, I found it in the official site. The actual code:
Imports CefSharp.WinForms
Imports CefSharp
Imports System.Xml
Imports System.Net
Imports System.Treading
Imports System.Treading.Tasks
Public Class Bolsa
Private WithEvents WebClave As ChromiumWebBrowser
Dim cadena
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs)
InitializeComponent()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
WebClave = New ChromiumWebBrowser("https://www.ahorro.com/acnet/fichas/ficha_valor.acnet?isin=ES0113211835&marketCode=09&submarketId=09")
pANwEB.Controls.Add(WebClave)
End Sub
Private Sub WebClave_IsBrowserInitializedChanged(sender As Object, e As IsBrowserInitializedChangedEventArgs) Handles WebClave.IsBrowserInitializedChanged
If e.IsBrowserInitialized Then
cadena = WebClave.EvaluateScriptAsync("document.getElementById('value').innerHTML")
TextBox1.Text = cadena
End If
End Sub
End Class
Any advice?
Thanks in advance.
[EDIT]: Added full original code
Imports CefSharp
Imports CefSharp.WinForms
Public Class Form1
Public WithEvents browser As ChromiumWebBrowser
Public Sub New()
InitializeComponent()
Dim settings As New CefSettings()
CefSharp.Cef.Initialize(settings)
browser = New ChromiumWebBrowser("https://google.com") With {
.Dock = DockStyle.Fill
}
Panel1.Controls.Add(browser)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cvbar = browser.EvaluateScriptAsync("document.getElementById(""lst-ib"").value;")
Dim response = cvbar.Result
If response.Success = True And response.Result <> "" Then
MsgBox(response.Result)
End If
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.

VB Clicked Links Open In New Tab

I've been searching around for a while for some code to do this, and I found a couple. most of them didn't work but I'm trying to get this one to work.
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'This creates a new tab
Dim tp As New TabPage
TabControl1.Controls.Add(tp)
'This creates a new webbrowser with the NewWindow Event
'And navigates it to the link wanting to be opened
Dim wb As New WebBrowser
Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myElement.GetAttribute("href")
With wb
.Navigate(target)
.Dock = DockStyle.Fill
End With
AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow
tp.Controls.Add(wb)
'This prevents ie from popping up
e.Cancel = True
End Sub
But then I get a error on here WebBrowser_NewWindow, and when I check and see what it says and I am told WebBrowser_NewWindow Is Not Declared. It may be inaccessible due to protection level How am I supposed to fix this?
Full Code
Public Class Form2
Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.BringToFront()
WebBrowser1.Navigate("www.google.com")
End Sub
Private Sub IClarityButton2_Click_1(sender As Object, e As EventArgs) Handles IClarityButton2.Click
If TextBox2.Text = "Close" Then
End
Else
TextBox2.Text = "Invalid"
End If
End Sub
Private Sub WebBrowser1_NewWindow(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
'This creates a new tab
Dim tp As New TabPage
TabControl1.Controls.Add(tp)
'This creates a new webbrowser with the NewWindow Event
'And navigates it to the link wanting to be opened
Dim wb As New WebBrowser
Dim myElement As HtmlElement = WebBrowser1.Document.ActiveElement
Dim target As String = myElement.GetAttribute("href")
With wb
.Navigate(target)
.Dock = DockStyle.Fill
End With
'AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow
tp.Controls.Add(wb)
'This prevents ie from popping up
e.Cancel = True
End Sub
End Class
Try removing AddHandler wb.NewWindow, AddressOf WebBrowser_NewWindow

How Do I Open a New Webbrowser Tab in Vb.net application

I have a Form1 with button1 and a webbrowser1. When I click on button1, I want to open a new web browser tab in the same form, not in Firefox or Internet Explorer or Chrome.
I tried using TabControl but am not sure how that works since it does not resize and its kind of annoying. I just want to open a new tab with web browser in the form.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim wb As New WebBrowser
wb.Navigate("www.google.com")
Dim tab As New TabPage("Title")
tab.Controls.Add(wb)
TabControl1.TabPages.Add(tab)
TabControl1.SelectedTab = tab
tab.Size = New System.Drawing.Size(280, 174)
End Sub
End Class
To add a new Tabbed browser, first u need to add a new Tab to your existing TabControl,
once the new Tab is added, then you need to add a new browser control into the created Tab
Private Sub btnAddTab_Click(sender As Object, e As EventArgs)
Dim page As New TabPage(String.Format("Tab # {0}", tabControl1.TabPages.Count + 1))
tabControl1.TabPages.Add(page)
Dim browser As New WebBrowser()
page.Controls.Add(browser)
browser.Dock = DockStyle.Fill
browser.Navigate(New Uri("http://www.google.co.in"))
End Sub
Create your own TabPage so that you can handle events and controls easly:
Public Class WBTab
Inherits TabPage 'it actually is a tabpage
Public WithEvents WB As New WebBrowser 'that has a single webbrowser in it
Sub New(ByVal URL As String) 'when the page is created, show it and load the URL
WB.Dock = DockStyle.Fill
Me.Controls.Add(WB)
WB.Navigate(URL)
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
Me.Text = WB.DocumentTitle 'when the page is loaded you may now show its title in your tab.
End Sub
Private Sub WB_Navigating(sender As Object, e As System.Windows.Forms.WebBrowserNavigatingEventArgs) Handles WB.Navigating
Me.Text = e.Url.ToString
End Sub
End Class
Now it is ready to be used:
Dim google As New WBTab("google.com") 'create a new tab with URL
TabControl1.TabPages.Add(google) 'show it
This should work:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim tabpage As New TabPage
tabpage.Text = "New Tab"
TabControl1.TabPages.Add(tabpage)
Dim webBrowser As New WebBrowser
TabControl1.SelectedTab = tabpage
tabpage.Controls.Add(webBrowser)
webBrowser.Dock = DockStyle.Fill
webBrowser.Navigate("http://www.stackoverflow.com")
End Sub