473,405 Members | 2,421 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,405 software developers and data experts.

Adding a Click event to a Web Browser control

Can anyone point me to a good guide for how to do this or give me some
pointers? What I'm basically trying to do is use the Web Browser as a
picture box that has a web source for the image, but it doesn't have a click
event for the control (so I can pop up a new window with an enlarged image).
I can put a panel above it (on a user control) with a click event or even a
picture box, but Transparent background apparently isn't actually
transparent so I'm staring at a gray square until I click on it then it pops
up the enlarged image perfectly. If anyone knows a better way to do this
I'm game for that also.

TIA,
Jayyde
Oct 27 '06 #1
7 19808
Anyone?

Pretty please?

"Jayyde" <an**@hotmail.comwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
Can anyone point me to a good guide for how to do this or give me some
pointers? What I'm basically trying to do is use the Web Browser as a
picture box that has a web source for the image, but it doesn't have a
click event for the control (so I can pop up a new window with an enlarged
image). I can put a panel above it (on a user control) with a click event
or even a picture box, but Transparent background apparently isn't
actually transparent so I'm staring at a gray square until I click on it
then it pops up the enlarged image perfectly. If anyone knows a better
way to do this I'm game for that also.

TIA,
Jayyde

Oct 30 '06 #2
Well, you could perhaps set the .ObjectForScripting to a C# object with a
method, and make the browser load some simple HTML (which you would write)
which places an IMG with an "onclick" that invokes a method on the scripting
object via javascript; something like below (all notepad jobs; not tested).

Marc
*** class def
[ComVisible(true)]
public class ScriptingObject {
internal event EventHandler Clicked;
public void SomeMethod() {
EventHandler handler = Clicked;
if(handler!=null) handler(this,EventArgs.Empty);
}
}
*** HTML extract
<IMG SRC="..." ONCLICK="window.external.SomeMethod()"/>
*** UI code
ScriptingObject() obj = new ScriptingObject();
obj.Clicked += // TODO: handle
myBrowser.ObjectForScripting = obj;
Oct 30 '06 #3
In fact, for a very similar example:

http://msdn2.microsoft.com/en-US/lib...scripting.aspx

Note also my "internal" was just me being lazy; the event should probably
just be [ComVisible(false)]. I would also recommend (unlike the MSDN
example) keeping a *very* tightly scoped object for this purpose to minimise
attack surfaces. Not that this is an issue for a simple job like this one.

Marc
Oct 30 '06 #4
Thanks for all the help Marc :). I just have 1 more question (one little
thing that's not cooperating). This thing is basically pulling image file
names from a db and creating individual thumbnails for them then needs to
update a selected image web browser (bigger) when they're clicked. It's
working perfectly for products that only have 1 image, but for those that
have more than 1 thumbnail it fires off the first click and resets the
document text for the selected image fine, but when the second one is
clicked it goes through the motions but never actually updates the document
text. I've tried it with and without a following .Refresh on the web
browser and with and without a .DocumentText = String.Empty before it tries
to update it. Is there something else that I need to do to make the
document text update?

TIA,
Jayyde

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eh**************@TK2MSFTNGP02.phx.gbl...
In fact, for a very similar example:

http://msdn2.microsoft.com/en-US/lib...scripting.aspx

Note also my "internal" was just me being lazy; the event should probably
just be [ComVisible(false)]. I would also recommend (unlike the MSDN
example) keeping a *very* tightly scoped object for this purpose to
minimise attack surfaces. Not that this is an issue for a simple job like
this one.

Marc

Oct 31 '06 #5
Sorry, no idea from that description...

Marc
Oct 31 '06 #6
Maybe this will help and maybe it won't then lol. Basic form set up is a
panel of small thumbnails of products (100x100) with a slightly larger
thumbnail (180x180) spot over to the side that displays that slightly larger
image for whatever smaller thumbnail the user selects. The user can then
click to zoom the selected which just opens a new form where they can make
it as big as they want and save the picture locally if they want. (Note:
the context menus are turned on just so I can get to the view source to try
to figure out what the heck's going on--they won't be on release).

************** CODE ********************************

private void LoadProductImages(DataSet dsImages)
{
WebBrowser wb;
int i = 0;
int x = 0;

foreach (DataRow dr in dsImages.Tables[0].Rows)
{
i++;

wb = new WebBrowser();
pnlImages.Controls.Add(wb);

wb.Name = "webThumb" + i.ToString();
wb.AllowWebBrowserDrop = false;
wb.AllowNavigation = false;
wb.ScrollBarsEnabled = false;
wb.IsWebBrowserContextMenuEnabled = true;
wb.WebBrowserShortcutsEnabled = false;
wb.ObjectForScripting = this;
wb.ScriptErrorsSuppressed = true;
wb.DocumentText =
"<html><head><style>" +
"body,table,tr,td,img{margin:0px; padding:0px; display:inline;
vertical-align: bottom; }" +
"</style></head><body><center>" +
"<img src=\"" + cIMAGE_HOST +
dr[ProductImageDCSchema.FileName].ToString() + "\" " +
"height=\"100\" " +
"onclick=\"window.external.Thumbnail_Click('" +
dr[ProductImageDCSchema.FileName].ToString() + "')\">" +
"</center></body></html>";
wb.Size = new Size(100, 100);
wb.Location = new Point((x + 3), 3);
wb.Refresh();

x += 103;
}
}

public void Thumbnail_Click(string ImageFileName)
{
webSelectedImage.DocumentText = String.Empty;
mstrSelectedImageFileName = ImageFileName;
** webSelectedImage.DocumentText =
"<html><head><style>" +
"body,table,tr,td,img{margin:0px; padding:0px; display:inline;
vertical-align: bottom; }" +
"</style></head><body><center>" +
"<img src=\"" + cIMAGE_HOST + ImageFileName + "\" " +
"height=\"180\" " +
"onclick=\"window.external.SelectedImage_Click ('" + ImageFileName +
"')\">" +
"</center></body></html>";
webSelectedImage.Refresh();
}

public void SelectedImage_Click(string ImageFileName)
{
if (webSelectedImage.DocumentText != String.Empty)
EnlargeImage();
else
MessageBox.Show("Please select an image to enlarge.");
}

************************************************** **

It's on that **ed line where it gets messed up for some reason. The first
thumbnail that gets clicked sets it fine. When another one gets clicked it
sends ImageFileName ok, but the DocumentText doesn't actually save to the
new one. When I break point it and hover over it it's almost like it hasn't
refreshed. Freakishly, however, when I view source on the selected image
web browser all it has as its DocumentText is "<HTML></HTML".

I have no idea if any of that explains the situation better, but for my sake
I hope it does ;).

"Marc Gravell" <ma**********@gmail.comwrote in message
news:eU**************@TK2MSFTNGP03.phx.gbl...
Sorry, no idea from that description...

Marc

Oct 31 '06 #7
Eesh that's a lot of WebBrowsers... any reason you couldn't just lay out all
the thumbnails in one browser within a single HTML block? Or better, any
reason you can't do the bulk of this (presumably excluding EnlargeImage)
with a single DHTML page? Still, C# is a viable solution...

A quick (!) test shows that this concept generally works OK (i.e. with
multiple WebBrowsers all updating the DocumentText of another one).

My first suspicion would be that you html is malformed. Hard to tell without
looking at the final string; as a debugging aid, write the HTML to a
variable first, and either view it (break point) in a the HTML viewer, else
save it to the file system (e.g. File.WriteAllText() to a temp file), and
then inspect this file carefully. Or try loading it into a browser. See what
happens.

Personally, I don't really like inline HTML etc; you simply can't debug it
properly in this way. I tend to keep the HTML template in either a simple
text file (with token replacement), or build it via xslt (which makes it
easier to avoid "escaping" bugs). That said, it would be even less postable
if you had done it in a tidy way, so that isn't a dig! For all I know your
"real" code has the HTML off to one side...

Marc
Oct 31 '06 #8

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

Similar topics

8
by: George Hester | last post by:
In a page I have when the user left-clicks the page a Input box for a form gets the focus. But if the user right-clicks the page the Input box is not getting the focus. I'd like the Input box to...
4
by: yfng | last post by:
In a web form, I want the user clicks on one button and this button will trigger another button/link which will open a new browser window? How to do that? Is there any method like...
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
2
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Ankit Aneja | last post by:
I have a strange situation and I have no idea how to solve this. Its a Recruitment Search Page,in the Admin Page, for every button click event the Admin Person has to create a checkbox on the users...
5
by: Mark Rae | last post by:
Hi, Is it possible to add events, specifically a Click event, to a dynamically created TableCell? I have an <asp:Table...> control to which I'm dynamically adding TableRow controls made up of...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
1
by: Rotsey | last post by:
Hi, I have a form with a webbrowser control set to fill the form. I want to capture when the form is clicked. As the browser control is filling the form the user would click on the browser...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.