GeckoFX hyperlink with target='_blank' in browser - c#

I need a hyperlink with target='_blank' from geckowebbrowser1 to open in geckowebbrowser2 and not in a new window.
private void geckowebbrowser1_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
{
geckowebbrowser2.Navigate(e.Uri);
}
but a new window is created anyway :(

Looking at the source code, you could avoid creation of the new window by setting Cancel to false, like this:
private void geckowebbrowser1_CreateWindow(object sender, GeckoCreateWindowEventArgs e)
{
e.Cancel = true;
geckowebbrowser2.Navigate(e.Uri);
}
If you're interested, here's the part of the source code that handles the creation of a new window and calling the CreateWindow event handler:
GeckoWebBrowser browser = parent as GeckoWebBrowser;
if (browser != null)
{
var e = new GeckoCreateWindowEventArgs(flags);
browser.OnCreateWindow(e);
if (e.Cancel)
{
cancel = true;
return null; // When you set cancel to true on your end, the below code for creating a new window won't run
}
if (e.WebBrowser != null)
{
// set flags
((nsIWebBrowserChrome) e.WebBrowser).SetChromeFlagsAttribute(chromeFlags);
return e.WebBrowser;
}
nsIXULWindow xulChild = AppShellService.CreateTopLevelWindow(null, null, chromeFlags, e.InitialWidth, e.InitialHeight);
return Xpcom.QueryInterface<nsIWebBrowserChrome>(xulChild);
}
return null;

Related

Old form still display winforms c#

I implement a form for handle excel file when click button "Start".
Event click Start button:
private void btnImport_Click(object sender, EventArgs e)
{
showFormSelectLanguage();
if (CheckSheetFile() == true) {
using (WaitingForm frm = new WaitingForm(handleExcel))
{
frm.ShowDialog(this);
}
var dialogMessage = new DialogMessage();
dialogMessage.ShowDialog(this);
} else
{
ShowDialogNotFoundSheet();
}
}
showFormSelectLanguage method display dialog for select language:
private void showFormSelectLanguage()
{
var formSelectLanguage = new FormSelectLanguage();
formSelectLanguage.ShowDialog(this);
}
ShowDialogNotFoundSheet function for check sheet excel exist:
private void ShowDialogNotFoundSheet()
{
var dialogNotFoundSheet = new DialogNotFoundSheet();
dialogNotFoundSheet.setTextContent("Not found sheet");
dialogNotFoundSheet.ShowDialog(this);
}
Event click confirm select language button at Select language form:
private void btnConfirmLanguage_Click(object sender, EventArgs e)
{
//close dialog
this.Close();
}
Event click Close button for close DialogNotFoundSheet form:
private void btnCloseDialogNotFoundSheet_Click(object sender, EventArgs e)
{
this.Close();
}
CheckSheetFile method:
private bool CheckSheetFile()
{
var isCorrectFile = false;
try
{
xlWorkBook = xlApp.Workbooks.Open(txtFilePath.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
var xlWorkBook1 = xlWorkBook.Sheets["SheetName"];
isCorrectFile = true;
}
catch (Exception e)
{
return false;
}
return isCorrectFile;
}
Issue:
When I click Close button at DialogNotFoundSheet form. Then FormSelectLanguage from still display. It repeats. How can resolve it?
Expected 2 forms can close
Thanks!
Update:
All References btnImport_Click:
UI:
I don't exactly know what you did with btnImport_Click, but if your purpose is to disable the function of a button at a time and to enable it at another time, actually you don't have to register or unregister the click event, you can simply set button's Enabled propety.
//btnImport.Click += btnImport_Click;
btnImport.Enabled = true;
//btnImport.Click -= btnImport_Click;
btnImport.Enabled = false;
My guess of the reason of this loop is that you have called += btnImport_Click many times, but -= btnImport_Click is never (or less) run.
For instance if you do:
btnImport.Click += btnImport_Click;
btnImport.Click += btnImport_Click;
Each time btnImport is clicked, btnImport_Click will get invoked twice.

Can I detect failure on mshtml.IHTMLElement.click() call?

Can I determine if the click call was successfully? I'm debuggin an application that click on button (see below code) sometimes it seems fail to do so, I'd liek to determine whatever the click was successfully.
HtmlElement button = ...;
IHTMLElement nativeElement = button.DomElement as IHTMLElement;
nativeElement.click();
Have you considered adding an event handler for the click event for that particular HTMLElement? You could do something along these lines :
bool shouldfire = false;
bool didFire = false;
private void yourMethod()
{
HtmlElement button = ...; //however you are getting this element
button.Click +=button_Click;
IHTMLElement nativeElement = button.DomElement as IHTMLElement;
nativeElement.click();
shouldfire = true;
}
private void button_Click(object sender, HtmlElementEventArgs e)
{
didFire = true;
}
private void yourOtherMethod()
{
if (shouldfire != didFire)
{
//do something
}
}

How to check if Window is already open? Duplicate Windows

I have a button that opens a Window.
If the button is pressed again, it opens a duplicate of the same window.
info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
How do you check if the Window is already open, and deny a duplicate from being opened again?
I can't use info.ShowDialog() because it disables the Main Window.
Solutions that have not worked:
Info info = new Info();
if (!info.IsActive)
{
info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
}
Info info = new Info();
if (info.Visibility != Visibility.Visible)
{
info.Owner = Window.GetWindow(this);
info.Show();
}
public static bool IsWindowOpen<T>(string name = "") where T : Window
{
return string.IsNullOrEmpty(name)
? Application.Current.Windows.OfType<T>().Any()
: Application.Current.Windows.OfType<T>().Any(w => w.Name.Equals(name));
}
private void buttonInfo_Click(object sender, RoutedEventArgs e)
{
if (!IsWindowOpen<Window>("Info"))
{
Info info = new Info();
info.Owner = Window.GetWindow(this);
info.Show();
}
}
Create a form only when value is not null.
If the form was closed put the value back to null with the FormClosed event.
public static Info info;
if(info == null){
info = new Info();
info.Show();
}
put an event form close on the info form
private void info_FormClosed(object sender, FormClosedEventArgs e)
{
MainForm1.info = null;
}
It works for me
The sensible approach is to just keep track of the Window instance so you don't have to find it back later. Add a field:
private Info infoWindow;
If it is null then you know that the window doesn't exist yet, so you'll want to create it. Use the Closed event to set the variable back to null. If it is not null then you want to make sure that the window gets restored. So:
private void button_Click(object sender, RoutedEventArgs e) {
if (infoWindow == null) {
infoWindow = new Info();
infoWindow.Closed += (s, ea) => infoWindow = null;
infoWindow.Owner = this; // optional
infoWindow.Show();
}
else {
if (infoWindow.WindowState == WindowState.Minimized) {
infoWindow.WindowState = WindowState.Normal;
}
infoWindow.Activate();
}
}
And you probably also want to close the window automatically when the window that contains the button is closed:
private void Window_Closed(object sender, EventArgs e) {
if (infoWindow != null) infoWindow.Close();
}
You could use .IsLoaded field or bind the .ContentRendered event
Edit 1 -
Window1:
public class Window1 : Window
{
private Info info = null;
private Boolean IsInfoOpened = false;
protected void OpenInfo()
{
if (this.IsInfoOpened) return;
this.info = new Info();
this.info.ContentRendered += delegate { this.IsInfoOpened = true; };
this.info.Closed += delegate { this.IsInfoOpened = false; }
this.info.Show();
}
}

Keep Popup open and active as long as a TextBox has keyboard focus

I have a Popup that I want to always be open and its content active when a TextBox has keyboard focus. I have attempted this with this code
public partial class MyPopup : Popup
{
public MyPopup
{
InitializeComponent();
EventManager.RegisterClassHandler(
typeof(UIElement),
Keyboard.PreviewGotKeyboardFocusEvent,
(KeyboardFocusChangedEventHandler)OnPreviewGotKeyboardFocus);
}
private void OnPreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
if (sender is TextBox)
this.IsOpen = true;
else
this.IsOpen = false;
}
}
were I create the Popup in the constructor of App.
The problem with this code is that if the Popup is already open when ShowDialog is used the Popup is no longer active, even though it is still visually on top.
How do I get around this or get the required behavior in another way.
Found one solution where I check if the Window is opening by checking if it has loaded. If so I close the popup and reopen it again after the new Window has had its content rendered.
Not sure it's something I trust enough to use to use so a better solution would be welcome.
private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
e.Handled = true;
if (sender is TextBox)
{
var _parentWindow = Window.GetWindow((UIElement)sender);
if (!_parentWindow.IsLoaded)
{
this.IsOpen = false;
_parentWindow.ContentRendered += (o, i) => this.IsOpen = true;
}
else
{
this.IsOpen = true;
}
}
else
{
this.IsOpen = false;
}
}

Cancel opening link in browser

In my Windows phone application I use RichTextBox element
I have a hyperlink on it, and when user click on it there is a dialog: Do you want to open this link in exteranl browser. If user say no, external browser shouldn't be opened. I cancel navigation but in any case - external browser opens. How can I cancel opening link in browser?
//Constructor
static Helper()
{
var phoneApplicationFrame = Application.Current.RootVisual as PhoneApplicationFrame;
if (Application.Current.RootVisual as PhoneApplicationFrame != null)
{
phoneApplicationFrame.Navigating += new NavigatingCancelEventHandler(NavigationService_Navigating);
}
}
link.Foreground = new SolidColorBrush(Colors.Blue);
link.MouseOverForeground = new SolidColorBrush(Colors.Blue);
link.TargetName = "_blank";
var linkText = new Run() { Text = linkDesc };
link.Inlines.Add(linkText);
link.Click += new RoutedEventHandler(NavidateTo);
private static void NavidateTo(object sender, RoutedEventArgs routedEventArgs)
{
if (MessageBox.Show(
Constants.BrowserNavigating,
"",
MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
{
StateManager.Set("ExternalBrowser", "true");
}
else
{
StateManager.Set("Browser", "true");
}
}
public static void NavigationService_Navigating(object sender, NavigatingCancelEventArgs e)
{
var res = StateManager.Get("ExternalBrowser");
if (res != null)
{
StateManager.Remove("ExternalBrowser");
e.Cancel = true;
}
}
Rather than have the HyperlinkButton open the link itself, don't specify the NavigationUri but handle the Tap event yourself.
In the eventhandler ask the question and only open the browser if they say yes.
This will be much simpler than trying to cancel something that is already in progress.

Categories

Resources