I want to stop browser navigating,when i click button.I tried in the below method but it doesn't worked.
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
button2.Click += new RoutedEventHandler((object caller,System.Windows.RoutedEventArgs f) =>
{
e.Cancel = true;
});
}
Try this one-
private void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
stop.IsEnabled=true;
stop.Click += new RoutedEventHandler((object caller, System.Windows.RoutedEventArgs f) =>
{
stopPressed = true;
});
if (stopPressed == true)
e.Cancel = true;
}
If you want to stop the browser navigating you can try like this:
private void stop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
MiniBrowser.InvokeScript("eval", "document.execCommand('Stop');");
}
Related
No response from the command line is displayed when JLink.exe starts, nothing happens when the "ReadMemory" button is pressed.
But if after that you enter the command "exit" all the operations performed on the command line are displayed.
How can you fix this and why is it happening?
public partial class MainWindow : Window
{
Process jLinkProcess = new Process();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
jLinkProcess.StartInfo = new ProcessStartInfo(#"C:\Program Files\SEGGER\JLink\JLink.exe");
jLinkProcess.StartInfo.UseShellExecute = false;
jLinkProcess.StartInfo.RedirectStandardInput = true;
jLinkProcess.StartInfo.RedirectStandardOutput = true;
jLinkProcess.StartInfo.RedirectStandardError = true;
jLinkProcess.StartInfo.CreateNoWindow = true;
jLinkProcess.OutputDataReceived += CmdProcess_OutputDataReceived;
jLinkProcess.ErrorDataReceived += CmdProcess_ErrorDataReceived;
jLinkProcess.Start();
jLinkProcess.BeginOutputReadLine();
jLinkProcess.BeginErrorReadLine();
jLinkProcess.StandardInput.WriteLine("connect");
jLinkProcess.StandardInput.WriteLine("NRF52811_XXAA");
jLinkProcess.StandardInput.WriteLine("SWD");
jLinkProcess.StandardInput.WriteLine("4000");
}
private void CmdProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Dispatcher.Invoke(() =>
{
TextBox2.Text += e.Data + "\r\n";
TextBox2.ScrollToEnd();
});
}
private void CmdProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Dispatcher.Invoke(() =>
{
TextBox2.Text += e.Data + "\r\n";
TextBox2.ScrollToEnd();
});
}
private void TextBox1_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (sender is TextBox textBox && e.Key == Key.Enter)
{
jLinkProcess.StandardInput.WriteLine(textBox.Text);
textBox.Text = string.Empty;
e.Handled = true;
}
}
private void ReadMemory(object sender, RoutedEventArgs e)
{
string mac = "mem 10000060,8";
jLinkProcess.StandardInput.WriteLine(mac);
}
}
Here's what H have so far:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
}
private void button3_Click(object sender, EventArgs e)
{
audio.Stop();
if (button1.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
if (button2.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
}
}
this is only my test so far but what i want to do is change what button 3 does, i.e. if button 1 is clicked button three will open webpage 1, if button2 is clicked button 3 will open webpage 2, button 3's image will change depending, but what im finding with what i have done so far is that it opens BOTH pages AT THE SAME TIME ... how to i prevent this? i have tried if, else and else if, same result every time.
Both of your buttons are enabled, you are checking to see if the buttons are enabled or disabled (clickable or not), not which one has been clicked.
also:if (button2.Enabled == true)
is nested in the first conditional, I'm not sure if that's what you want.
You can: disable buttons 1 and 2 after their clicked so that, for instance button2.Enabled will now = false; (but then you will not be able to reclick that button)
More sophisticated, but better, is to use a delegate for the button3, and assign them in your button1_Click and button2_Click events. Something like this:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
button3.Click += new EventHandler(this.Button3_Click_First);
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
button3.Click += new EventHandler(this.Button3_Click_Second);
}
void Button3_Click_First(Object sender,
EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
}
void Button3_Click_Second(Object sender,
EventArgs e)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
You may also have to check and make sure an event handler was not previously assigned, in calse someone clicks button1, then button2, then button1 ect. This is described here: Removing event handlers
You can handle your problem by storing the URL of the webpage in a private field, setting it when buttons 1 or 2 are clicked and reading from it after clicking button 3.
private string _address = null;
private void button1_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide";
}
private void button2_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide";
}
private void button3_Click(object sender, EventArgs e)
{
if (_address != null)
{
audio.Stop();
if (button1.Enabled || button2.Enabled)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start(_address);
}
}
}
I wasn't sure if all the code in button3_Click is necessary, so I cleared it up a little. I might be a bit off, though.
button.Enabled is always true for all buttons by default unless you set it to false. So you cannot use button1.Enabled property to check which button is pressed. try below approach.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
if ((bool)ViewState["Button1Clicked"])
{
//open webpage2 code comes here
}
else
{
//open webpage2 code comes here
}
}
I'm trying to get source code of a webpage and save it to richtextbox and that web browser control navigates to new URL. But I'm getting a blank rich text box. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
webBrowser2.Navigate("http://www.gmail.com");
}
private void timer1_Tick(object sender, EventArgs e)
{
if(webBrowser2.ReadyState == WebBrowserReadyState.Complete)
{
timer1.Enabled = false;
richTextBox1.Text = webBrowser2.DocumentText;
webBrowser2.Navigate("new URL");
}
}
I see two alternatives to what you're doing.
1) Using DocumentCompleted:
private void Form_Load(object sender, EventArgs e) {
webBrowser.Navigate("www.google.com");
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
richTextBox.Text = webBrowser.DocumentText;
}
2) Using the WebClient:
private void Form_Load(object sender, EventArgs e) {
using (System.Net.WebClient wc = new System.Net.WebClient()) {
richTextBox.Text = wc.DownloadString("http://www.google.com");
}
}
I cannot figure out how to catch the Navigating event on the WebBrowser control. Basically I'm trying to figure out how trigger the progress bar to show when a user clicks a link on a page.
Here is the code I use to show the progress bar and then hide it on page loaded. Can someone help me out with the event handler for Navigating?
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
progressBar.IsIndeterminate = true;
progressBar.Visibility = Visibility.Visible;
webBrowser.Navigate(new Uri(MY_URL, UriKind.Absolute));
webBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(PageLoadCompleted);
webBrowser.Navigating = ?
}
private void PageLoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
}
The documentation you seek is here. You can write
webBrowser.Navigating += webBrowser_Navigating;
// ...
void webBrowser_Navigating( object sender, NavigatingEventArgs e )
{
// ...
}
The answer of by VisualStuart helped me solve my problem.
My now working code is as below :
private void MyButton1_Click(object sender, RoutedEventArgs e)
{
MyprogressBar.IsIndeterminate = true;
MyprogressBar.Visibility = Visibility.Visible;
string site = MyTextBox1.Text;
webBrowser1.Navigate(new Uri(site, UriKind.Absolute));
webBrowser1.Navigating += webBrowser1_Navigating;
webBrowser1.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(webBrowser1_LoadCompleted);
}
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
MyTextBox1.Text = e.Uri.ToString();
MyprogressBar.IsIndeterminate = true;
MyprogressBar.Visibility = Visibility.Visible;
}
how can i set timeout for webBrowser navigate (url) event
c# netframework 4.0
By using a Timer of course. For example:
public void NavigateTo(Uri url) {
webBrowser1.Navigate(url);
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e) {
timer1.Enabled = false;
MessageBox.Show("Timeout on navigation");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (e.Url == webBrowser1.Url && timer1.Enabled) {
timer1.Enabled = false;
// etc..
}
}
I am using following approach based on Navigating and Navigated events. The time between these two events are observed for redirecting to home pgae.
//Navigation Timer
timer2.Enabled = true;
timer2.Interval = 30000;
br.DocumentCompleted += browser_DocumentCompleted;
br.DocumentCompleted += writeToTextBoxEvent;
br.Navigating += OnNavigating;
br.Navigated += OnNavigated;
br.ScriptErrorsSuppressed = true;
br.Navigate(ConfigValues.websiteUrl);
private void OnNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
//Reset Timer
timer2.Stop();
timer2.Start();
WriteLogFunction("OnNavigating||||||"+e.Url.ToString());
}
private void OnNavigated(object sender, WebBrowserNavigatedEventArgs e)
{
//Stop Timer
timer2.Stop();
WriteLogFunction("NAVIGATED <><><><><><><> " + e.Url.ToString());
}
private void timer2_Tick(object sender, EventArgs e)
{
WriteLogFunction(" Navigation Timeout TICK");
br.Stop();
br.Navigate(ConfigValues.websiteUrl);
}
Reference
Create a time-out for webbrowser loading method
webbrowser timeout if page wont load