using GoogleEarth plugin from VB.NET through FC.GEPluginCtrls - vb.net

I'm really looking for a simple way to build VB.NET apps that use the GEplugin. So I have found this project that seems to do the dirty job I need: http://code.google.com/p/winforms-geplugin-control-library/
Well, all the code posted there around works on C#, but I need to have it on VB.NET. So I have tried this:
created a new 32-bit solution from VB.NET 2010 Express (I simply added
<PlatformTarget>x86</PlatformTarget >
inside the .vbproj file)
added a reference to FC.GEPluginCtrls.dll
inserted a GeWebBrowser control on the form
at the top of the code, added
Imports FC.GEPluginCtrls
then, in the form Load event, put this code:
InitializeComponent()
GeWebBrowser1.LoadEmbeddedPlugin()
Do
Loop Until GeWebBrowser1.PluginIsReady = False
GeWebBrowser1.CreateInstance(ImageryBase.Earth)
that, I think, would be equivalent to
http://code.google.com/p/winforms-geplugin-control-library/wiki/CreateInstance
So, the project compiles and doesn't get errors, but the GeWebBrowser control remains empty.

I actually wrote the library you are using. You are not listening for the PluginReady event. http://code.google.com/p/winforms-geplugin-control-library/wiki/PluginReady
To use it with VB simply convert the basic examples to VB -
http://code.google.com/p/winforms-geplugin-control-library/wiki/ExampleForm
Also, a loop polling PluginIsReady is totally unnecessary as the PluginReady event is asynchronous.
To show the earth all you would need is the following.
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin()
End Sub
To use the plugin when it has initialzesd use the PluginReady event. Something like.
Option Strict Off
Public Class Form1
Private Dim _ge as Object = Nothing
Private Sub GeWebBrowser1_PluginReady( ByVal sender As System.Object, ByVal e As FC.GEPluginCtrls.GEEventArgs) Handles GeWebBrowser1.PluginReady
_ge = e.ApiObject ' reference to the Google Earth Plugin object
MessageBox.Show(_ge.getApiVersion()) ' _ge is the plugin -use it just as in the javascript api...
End Sub
Private Sub Form1_Load( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GeWebBrowser1.LoadEmbeddedPlugin() ' load the plugin
End Sub
End Class

Related

Drag and drop an image into a RichTextBox

I am updating code done in VB 4, where I have a RichTextBox. I need to be able to drag-and-drop an image from Windows Explorer into the RTB. Unfortunately, I am unable to get the drag-and-drop to work.
I've created a much more simple Windows Form program to try to resolve this, but have made no progress. I begin by setting AllowDrop to True.
Public Sub New()
InitializeComponent()
Me.DragAndDropTextBox.AllowDrop = True
End Sub
I then create handlers for the RTB. These are taken directly from MSDN.
Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub DragAndDropTextBox_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
Dim img As Image
img = Image.FromFile(e.Data.GetData(DataFormats.FileDrop, False))
Clipboard.SetImage(img)
Me.DragAndDropTextBox.SelectionStart = 0
Me.DragAndDropTextBox.Paste()
End Sub
When I grab an image in Explorer and drag it over my window, I get the circle with a slash. I have put breakpoints on the first line of each of the handlers, and they are never reached. I have looked at several pages, and they all seem to give the same process, so I must be missing something simple.
I am not worried right now about pasting the image into the text box; I know I need to work on that. I am only trying to capture the image, but the handler methods do not seem to be getting called.
UPDATE
After quite a bit of experimentation, I found that the actual issue is with my Visual Studio 2010, which I always run as administrator. When I run the program from an exe, the drag-and-drop works. When I try running from VS in debug, it does not. Has anyone experienced this before?
If anyone could shed some light on this, I would be very grateful.
Try getting rid of the InitializeComponent() call in your Sub New function. When I did that, I was able to detect the DragEnter event. Here is the code I tested (I created a simple WinForm and put a RichTextBox on it called DragAndDropTextBox):
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DragAndDropTextBox.AllowDrop = True
End Sub
Private Sub DragAndDropTextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragEnter
Debug.Print("Entering text box region")
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub DragAndDropTextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles DragAndDropTextBox.DragDrop
Dim img As Image
img = Image.FromFile(e.Data.GetData(DataFormats.FileDrop, False))
Clipboard.SetImage(img)
Me.DragAndDropTextBox.SelectionStart = 0
Me.DragAndDropTextBox.Paste()
End Sub
End Class
The InitializeComponent() call should appear in your code (I believe) when you add your own custom controls to a form. Otherwise, I don't think you need to call it.
It turned out that the Drag-And-Drop was working when running the code from an exe, but not from within Visual Studio. More searching turned up this answer, which states that Drag-And-Drop does not work in Visual Studio when it is run as an Administrator. I ran it with normal permissions, and the code worked.

How is it possible to parse the URL of the desired popup to the popup-form AND show hints/tooltips in the WebKit-Component?

I'm trying to use the WebKit-component (http://www.webkit.org/) in VB with the help of Visual Studio 2008.
This is running without problems, except for two following two issues:
1. Hints/Tooltips are not shown (e.g. as there usually will appear one if you stay with the mouse over the Google-logo)
2. If there's a popup-window, I don't know how to get the new desired URL.
I'm already working a few days on this matter and couldn't find any solution yet :(
Maybe you know a solution to this problem.
Cheers
Markus G.
P.S.: If you need more than the following Source Code to analyze the problem, then let me know ...
Source Code Form1
Imports System.IO
Imports WebKit
Public Class frmMain
Private _url As String
Private _mode As String
Private _popupUrl As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
On Error Resume Next
Dim bLogging As Boolean
setWindowAndBrowserSettings()
_url = "http://www.google.com"
browserComp.Navigate(_url)
End Sub
Private Sub setWindowAndBrowserSettings()
Me.Text = "Test - Browser"
Me.WindowState = FormWindowState.Maximized
browserComp.Dock = DockStyle.Fill
browserComp.Visible = True
End Sub
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
'frmPopup.WindowState = FormWindowState.Maximized
frmPopup.Text = "ixserv - POPUP"
frmPopup.popup.Navigate(_popupUrl)
frmPopup.Show()
End Sub
Private Sub browserComp_NewWindowRequest(ByVal sender As Object, ByVal e As WebKit.NewWindowRequestEventArgs) Handles browserComp.NewWindowRequest
e.Cancel = False
_popupUrl = browserComp.Url.ToString ' WHERE can I get the clicked URL? This is the old one of the remaining window
End Sub
End Class
Code Form2
Public Class frmPopup
End Class
Following popup/new-Window-Create-function works for me:
Private Sub browserComp_NewWindowCreated(ByVal sender As Object, ByVal e As WebKit.NewWindowCreatedEventArgs) Handles browserComp.NewWindowCreated
frmPopup.Text = "POPUP"
Dim popupBrowser As WebKit.WebKitBrowser
popupBrowser = e.WebKitBrowser
frmPopup.Controls.Add(popupBrowser)
frmPopup.Show()
End Sub
whereas frmPopup is a new form.
Before I tried this I already added the Webkit-component to the new form, which might had been the problem. I assume, the trick is, to create a new WebKitBrower-element that is directly connected to the argument e.WebkitBrowser instead of overloading an existing webkitbrowser-component in the form. Don't ask me for reasons for this now (I really don't know) :P
Oh, I should add that I used the Webkit.NET component. The same trick works also for the OpenWebkitSharp-wrapper
The hint-problem still remains ...

BackgroundWorker causing error when not Startup project

I have a project in VSTO/VB using a BackgroundWorker that works fine. It is a form that calls for a web page of information. The web page can take a while, so I have the form calling with the BackgroundWorker.
I then have an Excel Addin project that has added the BackgroundWorker project. When I call up the form from the Excel Addin project and use the BackgroundWorker to request the web page, it grabs the web page ok. But the work done upon completion, during the BackgroundWorker1_RunWorkerCompleted method, is resulting in an error message:
"Cross-thread operation not valid: Control 'TabPage2' accessed from a thread other than the thread it was created on."
Why does the BackgroundWorker project not work when called from the Excel Addin project?
I note that when I set the BackgroundWorker project as the "Startup Project" there is no error generated. Its something to do with calling this BackgroundWorker project from the Excel Addin project.
Edit: Could be that I am calling RunWorkerAsync() from a non UI thread?
I have an Excel Addin project with a Ribbon class. The Ribbon1.vb has a button click method that creates an instance of the second project, from which the backgroundworker will be called:
Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles Btn.Click
Dim MySecondProject As SecondProject.Form1 = New SecondProject.Form1()
MySecondProject.Show()
End Sub
MySecondProject is then calling the BackgroundWorker from within its own button click method as:
BackgroundWorker1.RunWorkerAsync()
Then, after it completes, the backgroundworker is trying to update a label in MySecondProject:
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
SuccessLabel.Text = "Success!"
End Sub
When MySecondProject was the Startup Project, the backgroundworker kept track of the correct thread and updated the label successfully upon completion. With the Excel Addin as the Startup Project, and MySecondProject instantiated during runtime, the backgroundworker seems to lose track of the correct thread. Should I manually be inserting Invoke or BeginInvoke somewhere to help the backgroundworker keep track of the correct thread to update?
So, it turns out that a Backgroundworker launched from a Form launched from a Ribbon cannot update a control at the end of processing. I am not sure why it works from a Form launched as a Startup project while it does not work from a form launched from the Ribbon, but there it is -- turns out that you need to deal with the Backgroundworker losing track of the UI thread.
Using a MethodInvoker works, as in the following snippet:
Imports System.Windows.Forms
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
[do nothing]
End Sub
Private myString As String = "This is my string"
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If Label1.InvokeRequired Then
Dim mi As MethodInvoker = AddressOf UpdateFormText
Label1.BeginInvoke(mi)
Else
Label1.Text = myString
End If
End Sub
Private Sub UpdateFormText()
Label1.Text = myString + " (BeginInvoked)"
End Sub
End Class
A better answer in VS2010 would use an inline MethodInvoker instead of the second function:
Me.Invoke(CType(Sub() Me.Label1.Text = "This is my string", MethodInvoker))

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

VB.net Webclient.DownloadFileAsync Not Downloading

I am trying to make a download manager for my program. But when I run this code it gives me the message box say "Download Started" but that is it. I do not get any file downloaded or progress bar change? Does anyone know why?
Public Class frmDownloader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebClient1.DownloadFileAsync(New Uri("https://s3.amazonaws.com/MinecraftDownload/launcher/Minecraft_Server.exe"), "C:\hi.exe")
MsgBox("download started")
End Sub
Private Sub WebClient1_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles WebClient1.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
MsgBox("Download Progress Changed")
End Sub
End Class
The DownloadProgressChanged method is invoked on a different thread than the one that started the download. Inside this callback you seem to be manipulating some GUI element: ProgressBar1.Value. You should never manipulate GUI elements on different threads than the one on which they were created or you might get an exception. Depending on the type of application you are working on there are different ways to marshal calls on the GUI thread. For example in WinForms you should use the Control.BeginInvoke method. In WPF and Silverlight the equivalent is the Dispatcher.BeginInvoke.