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.
Related
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.
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;
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();
}
}
I'm displaying ModalDialog using sample code Busy.xaml in Template10 :
public static void SetBusy(bool busy, string text = null)
{
WindowWrapper.Current().Dispatcher.Dispatch(() =>
{
var modal = Window.Current.Content as ModalDialog;
var view = modal.ModalContent as Busy;
if (view == null)
modal.ModalContent = view = new Busy();
modal.IsModal = view.IsBusy = busy;
view.BusyText = text;
modal.CanBackButtonDismiss = true;
});
}
I can close this dialog by using ALT+Left Arrow, but on most Desktop application pressing ESC key usually will also close popup or dialog.
I try to add code to handle KeyDown on Busy.xaml but this method never executed when I press ESC or any key.
private void UserControl_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Escape)
{
e.Handled = true;
SetBusy(false);
}
}
So, how to make this ModalDialog close when user press ESC key ?
You have to attach an event handler to the CharacterReceived event of the CoreWindow.
Modify the SetBusy method:
public static void SetBusy(bool busy, string text = null)
{
WindowWrapper.Current().Dispatcher.Dispatch(() =>
{
var modal = Window.Current.Content as ModalDialog;
var view = modal.ModalContent as Busy;
if (view == null)
modal.ModalContent = view = new Busy();
modal.IsModal = view.IsBusy = busy;
view.BusyText = text;
modal.CanBackButtonDismiss = true;
// Attach to key inputs event
var coreWindow = Window.Current.CoreWindow;
coreWindow.CharacterReceived += CoreWindow_CharacterReceived;
});
}
Where as the CoreWindow_CharacterReceived would look like this:
private static void CoreWindow_CharacterReceived(CoreWindow sender,
CharacterReceivedEventArgs args)
{
// KeyCode 27 = Escape key
if (args.KeyCode != 27) return;
// Detatch from key inputs event
var coreWindow = Window.Current.CoreWindow;
coreWindow.CharacterReceived -= CoreWindow_CharacterReceived;
// TODO: Go back, close window, confirm, etc.
}
While the modal is open just use something along this route:
private void Modal_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
Another way to address (e.KeyCode==Keys.Escape) is:
(e.KeyChar == (char)27)
or
e.KeyCode==(char)Keys.Escape
For this code to work, you need Form.KeyPreview = true;
For more information on what's above: https://msdn.microsoft.com/en-us/library/system.windows.forms.control.keydown.aspx
I believe that you need to append the CancelButton Property for this to work properly.
(Almost the same approach) I believe this should work nicely as well:
private void HandleEsc(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
Close();
}
This is for a console application:
if (Console.ReadKey().Key == ConsoleKey.Escape)
{
return;
}
I am using webBrowser in Windows Forms to edit a text file.
I can save the file via the dialog, but I don't know how to tell the webBrowser control to unload the document?
So that if I press button again it will open the saved file again.
private void EditDates_Click(object sender, EventArgs e)
{
if (EditDates.Text.StartsWith("Close"))
{
webBrowser1.ShowSaveAsDialog();
EditDates.Text = "Edit Dates";
webBrowser1.Document.CLOSE????
}
else
{
EditDates.Text = "Close Dates";
webBrowser1.Url = new Uri(#"H:\EOD\ProductionDates.txt");
webBrowser1.Document.ExecCommand("EditMode", false, null);
}
}
if (EditDates.Text.StartsWith("Close"))
{
webBrowser1.ShowSaveAsDialog();
EditDates.Text = "Edit Dates";
webBrowser1.Url = new Uri("about:blank");
}