Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Active X Control on Windows Form


Reply
 
Thread Tools Display Modes
  #1  
Old 09-07-2007, 02:39 AM
MondeoST24 MondeoST24 is offline
Newcomer
 
Join Date: Sep 2007
Posts: 2
Default Active X Control on Windows Form

Hi,

Does anyone know how to instantiate/initialize an active x control in a windows forms app in code. In particular the ax web browser control.

It works fine just dropping it onto the form, but I want to create it from scratch in a seperate thread to the UI, I can create it and it appears on the form however when I call its navigate method I get this

Exception of type 'System.Windows.Forms.AxHost+InvalidActiveXStateException' was unhandled

Thanks
Reply With Quote
  #2  
Old 09-07-2007, 07:21 AM
gazmac's Avatar
gazmac gazmac is offline
Junior Contributor
 
Join Date: Apr 2003
Posts: 200
Default

What code did you try to dynamically create the web-browser?
Reply With Quote
  #3  
Old 09-07-2007, 07:22 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

Quote:
Originally Posted by MondeoST24 View Post
Does anyone know how to instantiate/initialize an active x control in a windows forms app in code. In particular the ax web browser control.
.NET 2.0 is free and has a .NET WebBrowser control; consider using this instead of the ActiveX control unless your requirements state you cannot use .NET 2.0.

Quote:
It works fine just dropping it onto the form, but I want to create it from scratch in a seperate thread to the UI
This is probably going to give you fits. The threading model really only supports interaction with controls on the thread that created them; having controls created from another thread is going to introduce a nightmare of thread marshalling that will require several bad ideas to implement.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #4  
Old 09-07-2007, 08:30 AM
MondeoST24 MondeoST24 is offline
Newcomer
 
Join Date: Sep 2007
Posts: 2
Default

Thanks,

Let me give you a bit more info. What I'm trying to achieve is an application which has seven web browsers each connecting to a pricing site. My application automatically goes through each site, logging in, choosing a vehicle, choosing parameters then finally scraping a price of the final page. In the end I want to return all the prices to the user cheapest first.

Is there a better way of going about it that someone can suggest?

Thank you
Reply With Quote
  #5  
Old 09-07-2007, 09:09 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is online now
Ultimate Contributor

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 7,598
Default

Quote:
Originally Posted by MondeoST24 View Post
Thanks,

Let me give you a bit more info. What I'm trying to achieve is an application which has seven web browsers each connecting to a pricing site. My application automatically goes through each site, logging in, choosing a vehicle, choosing parameters then finally scraping a price of the final page. In the end I want to return all the prices to the user cheapest first.

Is there a better way of going about it that someone can suggest?

Thank you
The WebBrowser control is asynchronous by nature so there'd be no need to muck about with threads, but this is a ham-fisted brute force approach to the method and I think there is a better way.

Why do you need the web browser if all you are interested in is the HTML? There are several open discussions about using HttpWebRequest to get the HTML of a page, then parsing that HTML for specific values on this site. I think this would be the best method, though the only catch is that I can't figure out how to get that HTML text into a class like HtmlDocument that provides nice DOM parsing methods.

*edit* Actually I've made a breakthrough though it's kind of messy. The following code demonstrates retrieving a page's HTML, creating an mshtml object from it, then using the mshtml object's properties to get values from the HTML. It requires a reference to the Microsoft.mshtml assembly:
Code:
Imports System.Net
Imports mshtml

Module Module1

    Sub Main()
        Dim requestUri As New Uri("http://www.xtremevbtalk.com")
        Dim request As WebRequest
        Dim response As WebResponse

        ' Make our request then get the response
        request = WebRequest.Create(requestUri)
        request.Method = "GET"
        response = request.GetResponse()

        ' Get the page's HTML from the response
        Dim responseStream As System.IO.Stream
        Dim reader As System.IO.StreamReader
        Dim theHtml As String

        responseStream = response.GetResponseStream()
        reader = New System.IO.StreamReader(responseStream)
        theHtml = reader.ReadToEnd()

        ' Now create the mshtml object and get its title
        Dim doc As IHTMLDocument2 = New HTMLDocument()
        doc.write(theHtml)
        Dim title As String = doc.title

        Console.WriteLine(title)

        Console.ReadLine()
    End Sub

End Module
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.

Last edited by AtmaWeapon; 09-07-2007 at 09:41 AM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:

Powered by liquidweb