Using the WebBrowser in WPF, how can I check for a change in the URL? is there an even that can be fired when it hits a condition? Below I have a button event that sets the App.browserLinkCheck as the target URL, and opens the WebBrowser instance.
private void btNextWelcomeNewHire_Click(object sender, System.Windows.RoutedEventArgs e)
{
App.borwserLinkCheck = App._PasswordSyncWebLink;
webBrowser.Navigate(new Uri(App.borwserLinkCheck));
//webBrowser.Navigating += webBrowser_Navigating;
}
You can use Navigating event to detect or even cancel navigation in the webBrowser.
You can save the current WebBrowser url and compare it with the new one received in the Navigating event and compare them to see if it has changed.
private Uri currentUri;
void myBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (currentUri.AbsolutePath != e.Uri.AbsolutePath)
{
// Url has changed ...
// Update current uri
currentUri = e.Uri;
}
}
Thanks to #Omribitan example I have managed to over come the NullException issue by adjusting the code:
private String targetStringToCompare = "www.example.com";
void myBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
if (e.Uri.AbsoluteUri.ToString() == targetStringToCompare)
{
// Do something when the change will be detected
}
}
Related
When changing AxWindowsMediaPlayer URL in PlayStateChange Event, it doesn't start playing automatically, just changes to "Ready" state.
I have an "AxWindowsMediaPlayer" Control in my C# WinForms program. when I normally change the URL property of WindowsMediaPlayer1, it works fine and plays new mp3 file automatically.
When the song ended WindowsMediaPlayer1 State changes to Stopped and I Want next URL automatically start Playing.
I used PlayStatChange event, so when player state is Stopped, URL Will change, but Not playing automatically!
The player goes to Ready State until I press the play button on the WindowsMediaPlayer1.
Here is the Code:
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "6.mp3"; //Works fine
}
private void button1_Click(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "4.mp3"; //Works fine. It changes the music.
}
private void WindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) //1 is for "Stopped" State
WindowsMediaPlayer1.URL = "5.mp3";
// Here is the problem.
// URL Will change but player goes to "Ready" State
// But not in "playing" until I press the play button in control.
}
Any help would be appreciated.
As mentioned in media player documentations, you should not set the Url from event handler code. Instead you can play next file this way:
private void axWindowsMediaPlayer1_PlayStateChange(object sender,
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1)
{
this.BeginInvoke(new Action(() => {
this.axWindowsMediaPlayer1.URL = #"address of nextfile";
}));
}
}
Also as another option you can consider using a playlist.
I found this note on msdn about player.URL:
"Do not call this method from event handler code. Calling URL from an event handler may yield unexpected results."
so I tried another way to solve it and its worked.
added a timer and a bool varible to check if WindowsMediaPlayer1 is "Stopped"
Here is the solution:
public partial class Form1 : Form
{
bool nextURL = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
WindowsMediaPlayer1.URL = "5.mp3";
}
private void WindowsMediaPlayer1_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if (e.newState == 1) // 1 is consider for "Stopped" State
{
nextURL = true; // if the song ended "nextURL" flag sets to true
}
}
private void timer1_Tick(object sender, EventArgs e)
{
if (nextURL)
{
WindowsMediaPlayer1.URL = "6.mp3";
nextURL = false;
}
}
I am looking to simulate a custom tooltip the like of you see in websites using c# .NET 4.5 windows forms.This tooltip will basically show status of some Tasks like how many tasks are pending,tasks in process, completed etc.To do this i am using a borderless win form.This winform will have some texts, images etc.I want it to reveal itself on button's mouseHover event and disappear on MouseLeave event.My problem is that on Mousehover event numerous instances of that tooltip form is getting generated and on MouseLeave they are not getting closed.My code is
private void B_MouseHover(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
frmSecQStatToolTipDlg tooltip = new frmSecQStatToolTipDlg();
tooltip.Close();
}
My code is not working, hence please tell me how to do this the correct way.Thanks
You're generating a new instance of the form class every time you get a hover event, and every time you get a leave event. If you want to continue to use this approach I would recommend you use a variable on your main form object to store the reference to your tooltip form. Secondly, you need to not generate a new instance whenever the event handler is called, but only when necessary. I would create your instance the first time your Hover event is called for a particular control, and then dispose of it when your Leave handler is called -- this is under the assumption that the tooltip dialog's constructor loads up different information for each control being hovered over. Like so:
frmSecQStatToolTipDlg f_tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
f_tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(f_tooltip != null)
{
f_tooltip.Close();
f_tooltip = null;
}
}
You should keep a global field for this form, and should not dispose or close it. Just hide it on some events and show again.
Sample Code:
frmSecQStatToolTipDlg tooltip;
private void B_MouseHover(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg == null)
{
tooltip = new frmSecQStatToolTipDlg();
}
tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
if(frmSecQStatToolTipDlg != null)
{
tooltip.Hide();
}
}
With this logic you'll not have to create tooltip instance again and again and it will not take time to popup if you frequently do this activity.
Declare your tooltip once as readonly and use it without asking anytime if it is null or not.
If you need to Dispose it, implement the IDisposable pattern:
https://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.110).aspx
private readonly frmSecQStatToolTipDlg _tooltip = new frmSecQStatToolTipDlg() ;
private void B_MouseHover(object sender, EventArgs e)
{
_tooltip.Location = this.PointToScreen(new Point(this.Left, this.Bottom));
_tooltip.Show();
}
private void B_MouseLeave(object sender, EventArgs e)
{
_tooltip.Hide();
}
Has any one successfully trapped the event of mouse scroll in a web browerser component?
I have two web browser controls i would like to scroll at the same time.
But there are no scroll events for web browsers.
I would like to create an event something like this below? has any one done or seen this before?
private void webCompareSQL_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Document.Window.AttachEventHandler("OnScroll");
}
Here i would call my event and proceed with the code.
private void windowEvents_OnScroll()
{
int nPos = GetScrollPos(webCompareSQL.Handle, (int)ScrollBarType.SbVert);
nPos <<= 16;
uint wParam = (uint)ScrollBarCommands.SB_THUMBPOSITION | (uint)nPos;
SendMessage(WebPrevSQL.Handle, (int)Message.WM_VSCROLL, new IntPtr(wParam), new IntPtr(0));
}
I have found this code but don't know how to use it. its an event.
webCompareSQL.Document.Window.Scroll
I was able to get this working as follows. This example assumes that both web browser controls are navigating to the same Url. I am also syncing the horizontal scrollbar in addition to the vertical - this can be omitted if it is not required.
webBrowser1.DocumentCompleted
+= new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser2.DocumentCompleted
+= new WebBrowserDocumentCompletedEventHandler(webBrowser2_DocumentCompleted);
NavigateToPage("www.google.com");
....
private void NavigateToPage(string url)
{
webBrowser1.Navigate(url);
webBrowser2.Navigate(url);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler1);
}
private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser2.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler2);
}
public void OnScrollEventHandler1(object sender, EventArgs e)
{
webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollTop
= webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop;
webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollLeft
= webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
}
public void OnScrollEventHandler2(object sender, EventArgs e)
{
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop
= webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollTop;
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollLeft
= webBrowser2.Document.GetElementsByTagName("HTML")[0].ScrollLeft;
}
I note your comment in How to retrieve the scrollbar position of the webbrowser control in .NET, relating to this operation
webBrowser1.Document.GetElementsByTagName("HTML")[0].ScrollTop
not working. I can confirm that this definitely works on my machine, so if this code does not work on yours I can look into alternatives.
The real event name is "onscroll" not "OnScroll".
MSDN:http://msdn.microsoft.com/en-us/library/ie/ms536966(v=vs.85).aspx
Following code is firing the method when event occured.
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.Window.AttachEventHandler("onscroll", OnScrollEventHandler);
}
public void OnScrollEventHandler(object sender, EventArgs e)
{
}
I have a WebBrowser element in a page, to which I would like to add a back and forward buttons, and have those buttons disabled when there's nothing to go back to and nothing to go forward to.
In Cocoa, the UIWebView has methods to easily check that: canGoBack and canGoForward, and you have goBack and goForward methods available (along with reload etc..)
Android has the exact same method names for achieving the same.
I see those methods are available in .Net 4 and 3.5 SP1.
I've found some references about using javascript commands in Silverlight but I find this very cumbersome, plus there's no way to detect if there's anything in the history (unless of course I manage this myself)
Surely, there's something a tad more advanced in Windows Phone ..
Here is how I ended up doing it.
This assumes you have set a backButton and forwardButton; the status of these buttons will be updated accordingly depending on where you are in the navigation stack.
webView is the WebBrowser object
List<Uri> HistoryStack;
int HistoryStack_Index;
bool fromHistory;
// Constructor
public HelpView()
{
InitializeComponent();
HistoryStack = new List<Uri>();
HistoryStack_Index = 0;
fromHistory = false;
webView.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(WebView_Navigated);
UpdateNavButtons();
}
private void backButton_Click(object sender, RoutedEventArgs e)
{
if (HistoryStack_Index > 1)
{
HistoryStack_Index--;
fromHistory = true;
webView.Navigate(HistoryStack[HistoryStack_Index-1]);
updateNavButtons();
}
}
private void forwardButton_Click(object sender, RoutedEventArgs e)
{
if (HistoryStack_Index < HistoryStack.Count)
{
HistoryStack_Index++;
fromHistory = true;
webView.Navigate(HistoryStack[HistoryStack_Index-1]);
UpdateNavButtons();
}
}
private void UpdateNavButtons()
{
this.backButton.IsEnabled = HistoryStack_Index > 1;
this.forwardButton.IsEnabled = HistoryStack_Index < HistoryStack.Count;
}
private void WebView_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
if (!fromHistory)
{
if (HistoryStack_Index < HistoryStack.Count)
{
HistoryStack.RemoveRange(HistoryStack_Index, HistoryStack.Count - HistoryStack_Index);
}
HistoryStack.Add(e.Uri);
HistoryStack_Index++;
UpdateNavButtons();
}
fromHistory = false;
}
I have a back button added to the applicationbar of a page in one of my apps which contains a webbrowser. I wanted the back button in the app bar to take the web page navigation backward, and wanted the hardware back button to go to the previous xaml page. This way, the user doesn't have to use the hardware back button to navigate backward through all the visited web pages in the webbrowser in order to go back to the prior xaml page. Here is how I did it, and you could easily set up a forward stack and when the user clicks the back (appbar) button, the page pops from that stack and is pushed to the forward stack.
private void NavigateWeb()
{
if (!loaded)
{
NavigationStack.Clear();
try
{
Web.Source = new Uri("http://m.weightwatchers.com/");
loaded = true;
}
catch (Exception ex)
{
MessageBox.Show("Unable to navigate to page.\n" + ex.Message,
"Error", MessageBoxButton.OK);
}
}
}
void Web_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
NavigationStack.Push(e.Uri);
}
void btnBack_Click(object sender, EventArgs e)
{
if (NavigationStack.Count > 2)
{
// get rid of the topmost item...
NavigationStack.Pop();
// now navigate to the next topmost item
// note that this is another Pop - as when the navigate occurs a Push() will happen
Web.Navigate(NavigationStack.Pop());
}
}
The reason I check for NavigationStack.Count > 2 is that the particular webpage that I'm showing in the webbrowser always starts with a "click here to continue" link on the first page, and there is no reason to go back to there. That's the downfall of showing other people's sites in your webbrowser - you don't have control over what is shown.
In regards to the javascript solution it is doing something like this:
private void backButton_Click(object sender, RoutedEventArgs e)
{
try
{
webView.InvokeScript("eval", "history.go(-1);");
}
catch
{
// Eat error
}
}
private void forwardButton_Click(object sender, RoutedEventArgs e)
{
try
{
webView.InvokeScript("eval", "history.go(1);");
}
catch
{
// Eat error
}
}
with having the IsScriptingEnabled set to true for the WebBrowser element.
However, this always generates an exception with error 80020006. I read various posts about how the DOCTYPE could have been the culprit, the system caching or IsScriptEnabled being set after the content was loaded... It just never worked...
Is it possible to have a WPF window/element detect the drag'n'dropping of a file from windows explorer in C# .Net 3.5? I've found solutions for WinForms, but none for WPF.
Try the following :
private void MessageTextBox_Drop(object sender, DragEventArgs e)
{
if (e.Data is DataObject && ((DataObject)e.Data).ContainsFileDropList())
{
foreach (string filePath in ((DataObject)e.Data).GetFileDropList())
{
// Processing here
}
}
}
private void MessageTextBox_PreviewDragEnter(object sender, DragEventArgs e)
{
var dropPossible = e.Data != null && ((DataObject)e.Data).ContainsFileDropList();
if (dropPossible)
{
e.Effects = DragDropEffects.Copy;
}
}
private void MessageTextBox_PreviewDragOver(object sender, DragEventArgs e)
{
e.Handled = true;
}
Unfortunately, TextBox, RichTextBox, and FlowDocument viewers always mark drag-and-drop events as handled, which prevents them from bubbling up to your handlers. You can restore drag-and-drop events being intercepted by these controls by force-handling the drag-and-drop events (use UIElement.AddHandler and set handledEventsToo to true) and setting e.Handled to false in your handler.
Turns out I couldn't drop onto my TextBox for some reason, but dropping onto buttons works fine. Got it working by adding 'AllowDrop="True"' to my window and adding drop event handler to button consisting of:
private void btnFindType_Drop(object sender, DragEventArgs e)
{
if (e.Data is System.Windows.DataObject &&
((System.Windows.DataObject)e.Data).ContainsFileDropList())
{
foreach (string filePath in ((System.Windows.DataObject)e.Data).GetFileDropList())
{
// Processing here
}
}
}
I had similar Issue, The drop events and drag enter events were not fired. The issue was with the windows User Account Settings. Set it to least secure setting and try the same code it works.