Stop further link navigating inside web browser control [WPF] - wpf

Hello is there something similar to AllowNavigation like in WinForms?
My search didn't yield any satisfying result.
Basically I'm trying to open a webpage inside new wpf window and stop user from clicking random links on that webpage and navigating further.
Saw something with
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}
Rest of the code is :
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
browser1.Navigating += browser1_Navigating;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
But it just makes my webpage not display ?
Thanks

Try this:
Once you have loaded your page, then you assign the browser_Navigating event handler.
public Popup_webpage(string ime)
{
InitializeComponent();
browser1.LoadCompleted += browser1_LoadCompleted;
string uri = "www.google.com"
browser1.Navigate(new Uri(uri, UriKind.Absolute));
browser1.Navigating += browser1_Navigating;
}
void browser1_LoadCompleted(object sender, NavigationEventArgs e)
{
browser1.Visibility = Visibility.Visible;
}
void browser1_Navigating(object sender, NavigatingCancelEventArgs e)
{
e.Cancel = true;
}

Related

Disable drag and drop from webbrowser control c#

I am trying to prevent users from dragging images that display in my webbrowser control.
I tried setting webbrowser.Document.MouseOver e.Cursor = Cursors.No Still not working.
I tried a few other ways.
I am unable to prevent images being dragged on the desktop.
Is it possible to prevent dragging of images from webbrowser control to desktop?
There is a way to prevent the body element of the HTML document to handle drag operation.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
this.webBrowser1.Url = new Uri("https://www.google.bg/search?q=stackoverflow&biw=1920&bih=950&source=lnms&tbm=isch&sa=X&sqi=2&ved=0ahUKEwjw2cWH4oTQAhVG8RQKHWCWB4AQ_AUIBigB");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.webBrowser1.Document.Body.Drag += new HtmlElementEventHandler(Body_Drag);
}
private void Body_Drag(object sender, HtmlElementEventArgs e)
{
e.ReturnValue = false;
}
}

dynamically add webbrowser control in windows chat application:

I am developing windows form chat application and i am writing message in richtextbox but when i click on send button and every message show in webbrowser control. .i want to add webbrowser control dynamically for individual message is send.i am using following code:
namespace WindowsFormsApplication1
{
[ComVisible(true)]
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// this.w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(w1_DocumentCompleted);
}
WebBrowser w1 = new WebBrowser();
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Add(w1);
HtmlElement div = w1.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}
private void Form1_Load(object sender, EventArgs e)
{
w1.Navigate(Path.Combine(Environment.CurrentDirectory, "HTMLPageForScripting.htm"));
}
}
}
Here is the case you asked for:
Add a Panel to form to put browser in and set its autoscroll property to true;
In button1 Click event, create browser, set its width and height, and navigate to the document you want.
In button1 Click event subscribe for DocumentCompleted event and add content there. We do add here because we should be sure all contents containing <div id="abc"></div>has been loaded.
Here is code:
private void button1_Click(object sender, EventArgs e)
{
var browser = new WebBrowser();
browser.Height = 100;
browser.Dock = DockStyle.Top;
browser.Navigate(#"D:\test.html");
browser.DocumentCompleted += browser_DocumentCompleted;
this.panel1.Controls.Add(browser);
//Just do it to put it at the end of list and scroll to it
browser.BringToFront();
this.panel1.ScrollControlIntoView(browser);
}
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var browser = (WebBrowser)sender;
HtmlElement div = browser.Document.GetElementById("abc");
div.InnerHtml = richTextBox1.Text;
}

New tab in WPF - items.add from buttons vs methods

OK, so I have a window called PictureWindow which displays pictures (I've cut out the code not related to making tabs). The TabControl is named "itemsTab". Using a button press, I can make a new tab no problem. But using the same operations inside a called method doesn't work. Using the buttonTab_Click method makes a new tab, the newTab method does not.
The only real difference I can see is due to the sender and RoutedEventArgs objects - how do these effect the operation here? Or is there something else I'm missing?
Thanks in advance.
Edit To make things even stranger, the newTab method does make a new tab, but only if it is called in the PictureWindow constructor method. If I have the following a new tab is made.
public PictureWindow(string current)
{
InitializeComponent();
newTab(current);
}
But if I call the method anywhere else it doesn't work.
public partial class PictureWindow : Window
{
public PictureWindow(string current)
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
private void buttonClose_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void buttonTab_Click(object sender, RoutedEventArgs e)
{
TabItem newTab = new TabItem();
newTab.Header = "New Tab!";
itemsTab.Items.Add(newTab);
}
public void newTab(string current)
{
TabItem newTab = new TabItem();
itemsTab.Items.Add(newTab);
}
}

WPF mediaelement

I have a MediaElement, but how can I call a function when the property "position" of MediaElement changes?
Position is not a DependencyProperty.
You can use a DispatchTimer. This article provides some good insight on how to get this working. MediaElement and More with WPF.
Here is some sample code that I took from a project I'm working on. It shows the position of the video using a slider control and allows the user to change the position.
I'm a bit of a newbie too, so it is possible that some of it is wrong (feel free to comment on problems in the comments section :).
private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
medPlayer.Play();
medPlayer.Stop();
mTimer = new DispatcherTimer();
mTimer.Interval = TimeSpan.FromMilliseconds(100);
mTimer.Tick += new EventHandler(mTimer_Tick);
mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
if (!mIsDragging)
{
try
{
mTick = true;
sldPosition.Value = medPlayer.Position.TotalMilliseconds;
}
finally
{
mTick = false;
}
}
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
mIsDragging = true;
medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
mIsDragging = false;
if (chkPlay.IsChecked.Value)
medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var pos = TimeSpan.FromMilliseconds(e.NewValue);
lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
if (!mTick)
{
medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
{
chkPlay.IsChecked = false;
medPlayer.Stop();
}
}
}

wpf prevent second click button

I have a problem again. when I click button, window appears. when I click button again same window appears again. I want when I click button first time, page appears. but I want to prevent second click. can anyone help me with this problem? thanks in advance.
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
}
set a simple boolean value to check if the window is already open?
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
isWindowAlreadyOpen = true;
}
}
Should do the trick.
EDIT
You'll have to register the closed event of the window to unset the boolean:
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
dic.Closed += Dictionary_Closed;
isWindowAlreadyOpen = true;
}
}
private void Dictionary_Closed(object sender, EventArgs e)
{
isWindowAlreadyOpen = false;
}
EDIT2
Alternatively, you can use dic.ShowDialog() if you want this window to be topmost and only one instance.

Resources