I have 2 problems with combining GeckoFX and C#.
1.When i clicks on a button,my application is going to open a OpenFileDialog(generated by C# code) to change src attribute of a img tag.I use a contextmenu for this img to do that. My problem is if i click once on button which to open the OpenFileDialog after that i click on img (without contextmenu) the OpenFileDialog opens again.
2.When i choose new image for this img,i can't delete old file(using C# code) Here is my code
[HTML and Javascript code]
<script type="text/javascript">
$(document).ready(function(){
$('.div_image).bind('contextmenu',function(){
$('#contextmenu_image').css({top: e.pageY+'px',left: e.pageX+'px'}).show();
});
});
</script>
<div class="div_image" style="position: absolute; top: '20px;left:'20px;"><img id="img123" class="image" src="" style="width: 100%;height: 100%;"/></div>
<ul class="contextmenu" id="contextmenu_image" style="width: 100px; display: none;">
<li class="properties">Properties</li>
<li class="del">Delete</li>
<button id="choose_image">Choose image</button>
</ul>
[C# code]
private void ChooseImage()
{
if (geckoWebBrowser1.Document.ActiveElement.GetAttribute("id") == "choose_image")
{
OpenFileDialog open = new OpenFileDialog();
open.Filter =
"Image (*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG)|*.BMP;*.JPG;*.GIF;*.PNG;*.JPEG|" +
"All files (*.*)|*.*";
open.Title = "Choose an image";
DialogResult result = open.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK)
{
string srcFile = open.FileName;
string fileName = System.IO.Path.GetFileName(srcFile);
string fileExtent = System.IO.Path.GetExtension(srcFile);
string desDir = Application.StartupPath + "\\test\\images\\";
Random r = new Random();
string newFileName = "i_";
for (int i = 1; i <= 10; i++)
{
newFileName += Convert.ToString(r.Next(0, 9));
}
newFileName += fileExtent;
System.IO.File.Copy(srcFile, desDir + newFileName);
//Find old image
string old_image = geckoWebBrowser1.Document.GetElementById("img123").GetAttribute("src");
geckoWebBrowser1.Document.GetElementById("img123").SetAttribute("src", "images/" + newFileName);
if (old_image != "")
System.IO.File.Delete(desDir + old_image);//Delete old file,but unable
}
}
}
private void geckoWebBrowser1_DomClick(object sender, GeckoDomEventArgs e)
{
ChooseImage();
}
Sorry because of my bad English
For your first problem, I recommend changing how you wire up the click event:
browser.OnBrowserClick += new System.EventHandler(OnBrowserClick);
Then, you'll get an argument telling you what was clicked:
private void OnBrowserClick(object sender, EventArgs e)
{
var ge = e as GeckoDomEventArgs;
if (ge.Target.ClassName =="choose_image")
{
//Handle the click...
For your second problem, I thought perhaps the browser was holding on to the file, but in my experiments, it does not. I recommend you make sure the file is really there:
var oldPath = Path.Combine(desDir);
if(File.Exists(oldPath))
{
try
{
File.Delete(oldPath);
}
catch(Exception error)
{
//do something about not being able to delete the file yet
}
}
If you'd like to look at some open-source code that does a lot of these kinds of things with geckofx, see my Bloom project, particularly EditingView.cs and Browser.cs.