我有2個問題與組合GeckoFX和C#。GeckoFX和C#!單擊按鈕以刪除文件時出錯
1.當我點擊一個按鈕時,我的應用程序將打開一個OpenFileDialog(由C#代碼生成)來更改img標記的src屬性。我使用這個img的context菜單來做到這一點。我的問題是,如果我點擊一次按鈕,打開OpenFileDialog後,我點擊img(沒有contextmenu)OpenFileDialog再次打開。
2,當我選擇新形象這個IMG,我不能刪除舊文件(使用C#代碼) 這裏是我的代碼
[HTML和Javascript代碼]
<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#代碼]
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();
}
對不起,因爲我的英語不好
非常感謝!我試着在函數的底部使用這段代碼 geckoWebBrowser1.Document.ActiveElement.Blur(); 而對於第二個錯誤,我錯誤path.Hum!再次感謝! Iam將嘗試你的代碼! – 2012-08-06 08:43:00