i am trying to reload a web page 3 times using "axMozillaBrowser1(Mozilla ActiveX Control)" but axMozillaBrowser1 control is not loading web page and it's waiting courser is blinking regularly but browser control is not loading web page.
How to make axMozillaBrowser1 wait till it loads webpage .
this is my program code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
progressBar1.Maximum = 3;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
do
{
axMozillaBrowser1.Navigate(textBox1.Text);
while(axMozillaBrowser1.ReadyState != MOZILLACONTROLLib.tagREADYSTATE.READYSTATE_COMPLETE)
{
Application.DoEvents();
if (axMozillaBrowser1.ReadyState == MOZILLACONTROLLib.tagREADYSTATE.READYSTATE_COMPLETE)
{
progressBar1.Value = progressBar1.Value + 1;
}
}
}while(progressBar1.Value != 3);
}
}
Related
I need to cancel the BakgroundWorker from the main form that was launched in another form. I am trying to solve this problem with the help of delegates. However, I cannot undo the BakgroundWorker. Help solve this problem. If there are other solutions, please write.
I give for example the code of the main form
public partial class MainForm : Form
{
public MainForm(string FIO)
{
//some code
}
public event EventHandler<EventArgs> Canceled;
private void Button5_Click(object sender, EventArgs e)
{
if (Canceled != null)
Canceled(sender, e);
}
}
Code of the form where it was launched backgroundWorker
public partial class CarriageForm : Form
{
public CarriageForm(ToolStripProgressBar toolStripProgressBar1, ToolStripLabel toolStripLabel1)
{
//some code
}
private void CarriageForm_Load(object sender, EventArgs e)
{
progBar.Visible = false;
if (!backgroundWorker1.IsBusy)
{
progBar.Visible = true;
progBar.Maximum = GetTotalRecords();
string GetCarriage = "Select dc.ID, dc.CarNumber [Номер вагона],dc.AXIS [Осность],do.ID [OwnerID], do.Name [Собственник],do.FullName [Собственник полное наименование] From d__Carriage dc Left Join d__Owner do on do.ID = dc.Owner_ID";
MainForm mainForm = new MainForm(null);
mainForm.Canceled += new EventHandler<EventArgs>(Button2_Click);
backgroundWorker1.RunWorkerAsync(GetCarriage);
}
//BackgroundWorker1_DoWork...
//BackgroundWorker1_ProgressChanged...
//BackgroundWorker1_RunWorkerCompleted..
public void Button2_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
{
// Stop the Background Thread execution
Application.UseWaitCursor = false;
System.Windows.Forms.Cursor.Current = Cursors.Default;
backgroundWorker1.CancelAsync();
progBar.Value = 0;
progBar.Visible = false;
TlStpLabel.Text = "Пользователь умышленно отменил";
}
}
}
For clarity
I am using Virtual Studio Community in C# (.Net 4.5).
I have a simple form, which contains one button and one webBrowser control.
When I click the button, I make the webBrowser navigate to google.com.
Then, when the page is loaded, I try to override the linkClick events as I saw in a solution I read on this site (stackoverflow) earlier.
But then, when I click on a link on the loaded page, it says the navigation was cancelled, but it navigates anyways.
What am I doing wrong?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com/");
}
private bool bCancel = false;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
for (int i = 0; i < webBrowser1.Document.Links.Count; i++)
{
webBrowser1.Document.Links[i].Click += new HtmlElementEventHandler(this.LinkClick);
}
}
private void LinkClick(object sender, System.EventArgs e)
{
bCancel = true;
MessageBox.Show("Link Was Clicked Navigation was Cancelled");
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (bCancel == true)
{
e.Cancel = true;
bCancel = false;
}
}
}
You need to bind the WebBrowserControl.Navigating event to the handler you've written; having the handler's name matching the control underscore event name isn't enough.
So you can do this in the Form1's constructor:
public Form1()
{
InitializeComponent();
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}
Better yet, add a Load event and do the same there. Check out the official documentation on the subject.
I have a countdown and it outputs to a label on the main form, I want to popout the countdown from the main form to a child form because the child form will be able to stay on top of all other windows (for game) I can't get it to updated the countdown on the child's label. I only get the second when i hit the popout button.
This is on the main form
private void tmrMain_Tick(object sender, EventArgs e)
{
TimerSeconds = TimerSeconds - 1;
if (TimerSeconds == 0)
{
tmrMain.Stop();
if (chbxPlaySound.Checked)
{
System.Media.SystemSounds.Exclamation.Play();
}
if (chbxRepeat.Checked)
{
btnStartTimer.PerformClick();
}
}
string dis_seconds = Convert.ToString(TimerSeconds);
//
// lblTimer is the label that shows the countdown seconds
//
lblTimer.Text = dis_seconds;
}
This is my pop out button on the main form
private void btnPopOut_Click(object sender, EventArgs e)
{
PopTimer ShowTimer = new PopTimer(TimerSeconds);
ShowTimer.Show(this);
}
This is my child form
public partial class PopTimer : Form
{
public PopTimer(int countTimer)
{
InitializeComponent();
//string dis_seconds = Convert.ToString(TimerSeconds);
lblPopTimer.Text = Convert.ToString(countTimer); ;
}
}
Your PopTimer form needs an accessible method, something like this:
public void SetCountdown(int value) {
lblPopTimer.Text = value.ToString();
}
Then your PopTimer needs to be accessible, so move the declaration:
private PopTimer showTimer = null;
private void btnPopOut_Click(object sender, EventArgs e)
{
showTimer = new PopTimer(timerSeconds);
showTimer.Show(this);
}
Then in your tick event:
if (showTimer != null) {
showTimer.SetCountdown(timerSeconds);
}
I have a function that displays numbers using a while loop but I want to stop execution of a while loop at random variable value using c# by clicking a button.
For Example:
private void FrqSent()
{
int i = 1;
while (i <= 5)
{
i = i + 1;
}
}
Here is a quick example on how you can use a Backgroundworker to accomplish your task:
public partial class Form1 : Form
{
private int i = 1;
public Form1()
{
InitializeComponent();
}
private void FrqSent()
{
while (i <= 500000000)
{
if (backgroundWorker1.CancellationPending)
{
break;
}
else
{
i = i + 1;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FrqSent();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(i.ToString());
}
}
Just create a new Windows-Forms Project and add a backgroundworker object aswell as 2 buttons. You have to set the DoWork, RunWorkerCompleted and Click events manually.
Edit: Do not forget to set the BackgroundWorker`s WorkerSupportsCancellation property to true.
not very elegant but simple
public partial class Form1 : Form
{
private bool _stop;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
_stop = false;
FrqSent();
}
private void buttonStop_Click(object sender, EventArgs e)
{
_stop = true;
}
private void FrqSent()
{
int i = 1;
while (i <= 5000000 && !_stop)
{
i = i + 1;
Application.DoEvents();
}
}
}
i am trying to reload a web page 3 times using "axMozillaBrowser1(Mozilla ActiveX Control)"
but axMozillaBrowser1 control is not loading web page and it's waiting courser is blinking regular but browser control is not loading web page.
Please tell me how to use axMozillaBrowser1 control properly. Thanx in advance.
this is my program code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
progressBar1.Maximum = 3;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
do
{
axMozillaBrowser1.Navigate(textBox1.Text);
while(axMozillaBrowser1.ReadyState != MOZILLACONTROLLib.tagREADYSTATE.READYSTATE_COMPLETE)
{
Application.DoEvents();
if (axMozillaBrowser1.ReadyState == MOZILLACONTROLLib.tagREADYSTATE.READYSTATE_COMPLETE)
{
progressBar1.Value = progressBar1.Value + 1;
}
}
}while(progressBar1.Value != 3);
}
}
Why you want to Navigate 3 times ?
But my suggest is :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
progressBar1.Maximum = 3;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
axMozillaBrowser1.Navigate(textBox1.Text);
}
private void webBrowser2_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
if ( progressBar1.Value < 3 )
{
axMozillaBrowser1.Navigate(textBox1.Text);
progressBar1.Value = progressBar1.Value + 1;
}
}
}