473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C# and WebBrowser component question

I have an app with a WebBrowser component and I'm setting the the
ObjectForScripting component so the object can be used from javascript.

The object has a Scripting.Dictionary property. My problem is that in
javascript I have to use "external.dictionary.Item('x')" to get the
items instead of "external.dictionary('x')". If I create the
Scripting.Dictionary in javascript it works ok. For example:

javascript:

var dict=new ActiveXObject( 'Scripting.Dictionary' );

dict.Add("item1","test");
external.dictionary.Add("item1","test");
alert(dict("item1"));
alert(external.dictionary("item1")); // this fails

alert(external.dictionary.Item("item")); / this works

What can I do so the dictionaries created by C# work the same as the
Javascript created ones?
Sep 11 '07 #1
4 4178
Fawzib,

Can you show the C# code which sets the ObjectForScripting? My guess is
that you are using a Dictionary<TKey, TValuewhich then uses the
IDictionary interface (the only one marked as ComVisible).

If you want to use a .NET Dictionary<TKey, TValueand still expose it
like a Scripting.Dictionary in the browser, then you either have to use a
Scripting.Dictionary in your app, and pass that, or, you can create a
wrapper class which implements the Scripting.Dictionary interface (it is a
COM object, so it has an interface) and then takes your .NET dictionary
instance and forwards the calls.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fawzib Rojas" <f_*****@nospam.spectron.msim.comwrote in message
news:ue*************@TK2MSFTNGP02.phx.gbl...
>I have an app with a WebBrowser component and I'm setting the the
ObjectForScripting component so the object can be used from javascript.

The object has a Scripting.Dictionary property. My problem is that in
javascript I have to use "external.dictionary.Item('x')" to get the items
instead of "external.dictionary('x')". If I create the
Scripting.Dictionary in javascript it works ok. For example:

javascript:

var dict=new ActiveXObject( 'Scripting.Dictionary' );

dict.Add("item1","test");
external.dictionary.Add("item1","test");
alert(dict("item1"));
alert(external.dictionary("item1")); // this fails

alert(external.dictionary.Item("item")); / this works

What can I do so the dictionaries created by C# work the same as the
Javascript created ones?

Sep 11 '07 #2
Nicholas Paldino [.NET/C# MVP] wrote:
Fawzib,

Can you show the C# code which sets the ObjectForScripting? My guess is
that you are using a Dictionary<TKey, TValuewhich then uses the
IDictionary interface (the only one marked as ComVisible).

If you want to use a .NET Dictionary<TKey, TValueand still expose it
like a Scripting.Dictionary in the browser, then you either have to use a
Scripting.Dictionary in your app, and pass that, or, you can create a
wrapper class which implements the Scripting.Dictionary interface (it is a
COM object, so it has an interface) and then takes your .NET dictionary
instance and forwards the calls.

I don't see why I cant access the dictionary created in C# the same way
as a Javascript created one. Here is the class (removed most of the
irrelevant info):

public class MyExternal {
//
object m_globals = null;
//
public MyExternal(object gl) {
m_globals = gl;
}
public object globals {
get{
return(m_globals);
}
}
}

public partial class MainForm : Form {
// originally it was:
// Scripting.Dictionary globals=new Scripting.Dictionary();
// changed it because it didn't work either
MyExternal m_external;
//
object globals = Activator.CreateInstance(
Type.GetTypeFromProgID("Scripting.Dictionary")
);
//
public MainForm() {
InitializeComponent();
m_external=new MyExternal(globals);
AddValue(globals, "test", "test information");
MainBrowser.ObjectForScripting = m_external;
}
public void AddValue(object d, object k, object v) {
((Scripting.Dictionary)d).Add(ref k,ref v);
}
}
Sep 11 '07 #3
Fawzib,

You need to use the DictionaryClass which is imported through TLBIMP.
The DictionaryClass implements the dispatch interface (which is necessary
for accessing the default property through script) whereas Dictionary and
IDictionary do not.

Change the type you expose to DictionaryClass and it should work.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Fawzib Rojas" <f_*****@nospam.spectron.msim.comwrote in message
news:u7**************@TK2MSFTNGP06.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
>Fawzib,

Can you show the C# code which sets the ObjectForScripting? My guess
is that you are using a Dictionary<TKey, TValuewhich then uses the
IDictionary interface (the only one marked as ComVisible).

If you want to use a .NET Dictionary<TKey, TValueand still expose
it like a Scripting.Dictionary in the browser, then you either have to
use a Scripting.Dictionary in your app, and pass that, or, you can create
a wrapper class which implements the Scripting.Dictionary interface (it
is a COM object, so it has an interface) and then takes your .NET
dictionary instance and forwards the calls.


I don't see why I cant access the dictionary created in C# the same way as
a Javascript created one. Here is the class (removed most of the
irrelevant info):

public class MyExternal {
//
object m_globals = null;
//
public MyExternal(object gl) {
m_globals = gl;
}
public object globals {
get{
return(m_globals);
}
}
}

public partial class MainForm : Form {
// originally it was:
// Scripting.Dictionary globals=new Scripting.Dictionary();
// changed it because it didn't work either
MyExternal m_external;
//
object globals = Activator.CreateInstance(
Type.GetTypeFromProgID("Scripting.Dictionary")
);
//
public MainForm() {
InitializeComponent();
m_external=new MyExternal(globals);
AddValue(globals, "test", "test information");
MainBrowser.ObjectForScripting = m_external;
}
public void AddValue(object d, object k, object v) {
((Scripting.Dictionary)d).Add(ref k,ref v);
}
}

Sep 11 '07 #4
Well, anyone has any other ideas?
Sep 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
Hi group, Here's a problem I've been trying to solve for the past several weeks. I have the standard WebBrowser object added to a form so it can access websites, and I have it load a default...
0
by: Sharon | last post by:
Hi all. I'm using the WebBrowser class and sometimes i get this exception: System.Windows.Forms.IWebBrowser2.Navigate2(Object& URL, Object& flags, Object& targetFrameName, Object& postData,...
2
by: frederik12 | last post by:
Hi all, Today I downloaded and installed VS 2005 C# Express Edition (.NET framework 2.0). I'm very pleased about this new product. In my current application I'm using the WebBrowser control in...
6
by: ESmith | last post by:
I'm looking to user a web browser component in VS2005. I like the functionality in the AXWebBrowser component (from ActiveX controls page) that allows me to do this: mshtml.HTMLDocument doc; ...
15
by: Laurent Lequenne | last post by:
Hello All, I'm currently developing a free windows Scrabble (in french :)) application that uses extensively the WebBrowser class of NET 2.0 for configuration, and data browsing. It works 100%...
1
by: claes R | last post by:
Hi ! I am trying to add a cookie to a webbrowser component hosted inside a windows forms window and perform a call. ala, browser.Navigate( xxx,yyy, zzz, strHeader) where strHeader contains...
5
by: ThisBytes5 | last post by:
I have an application that uses the web browser component in VS 2005. I know this is just a wrapper for the com component. What I need to do is control wether or not the images on the navigated...
0
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
Hi, Is it possible to access an htc file from the windows webbrowser control? I am receiving the following error: "An error has occurred in the script on this page." "Error: Access is denied...
3
by: TedTrippin | last post by:
Hi, Background - I've create a windows application in VS and added a WebBrowser component and all works well. I'm now trying to do this programmatically. My application now listens on a socket...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.