blah blah blah is here! blah blah » Close

up0down
link

There's a "Auto Refresh" checkBox in my WinForm which invokes the Timer control (60000) 1 min to call the form1_Load(); How shall I link the progressBar with timer?

I tried doing something like this:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
timer.Interval = 1000;
timer.Start();
for (progressBar1.Value = 0; (progressBar1.Value) == 100; progressBar1.Value++)
{
progressBar1.Value = timer.Interval;
progressBar1.Update();
}
if (checkBox1.Checked && progressBar1.Value == 100)
progressBar1.Value = 0;
}

private void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
Form1_Load(sender, e);
// to loop until the checkbox remained checked.
if (checkBox1.Checked)
checkBox1_CheckedChanged(sender, e);
}


Wherre am I making the mistake???

last answered one year ago

3 answers

up0down
link

The Form_Load eventhandler is invoked automatically by the system just before a form is displayed for the first time.

The only circumstances in which you should invoke it yourself from code are where the form is already displayed but, for some reason, you want to execute the code in the eventhandler again. Even then, it's arguably bad practice to do so.

So what exactly are you trying to achieve here?

gsvirdi
412

I'm displaying the xml file in the GridView, and I've tried to refresh(Form1_Load) the GridView after a particular time, let's say 1minute. . . . . . I'm just trying to show the progress of the [b]Timer[/b] control in the [b]ProgressBar[/b]. And on completion of Time... I'm loading the form again so the any changes made in the xml file are reflected automatically in the GridView also.

up0down
link

I'm simply creating a mess which only a newbie can do, Let me explain what I'm doing:

private void Form1_Load(object sender, EventArgs e)
{
// Creating link to xml and displaying it on Form Load.
DataSet ds = new DataSet();
ds.ReadXml(filename);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "bookdetail";
}

// A checkBox of "Auto Refresh" the form data.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
timer.Interval = 10000;
timer.Start();
// A wrong way to check if the progressBar is 100%.
bool Max = (progressBar1.Value) == (progressBar1.Maximum);
// Trying to link the progressBar with the Timer control.
// so the I can show the progress of the Timer (something like: Refreshing in... ).
for (progressBar1.Value = 0; Max; progressBar1.Value++)
{
progressBar1.Value = Convert.ToInt32(timer);
progressBar1.Refresh();
}
}

private void timer_Tick(object sender, EventArgs e)
{
timer.Stop();
// At the end of timer... the Formdata will be Re-Loaded.
Form1_Load(sender, e);
// Simply trying to create a looping statement
// If "Auto Refresh" checkBox is still checked, then repeat FormLoad.
if (checkBox1.Checked)
checkBox1_CheckedChanged(sender, e);
}


Does this makes some sense???

up0down
link

OK, first change (in the designer) the ProgressBar's Maximum property to 60 and Step property to 1. Now change the last two eventhandlers as follows:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
timer.Interval = 1000;
timer.Start();
}
}

private void timer_Tick(object sender, EventArgs e)
{
if (progressBar1.Value < progressBar1.Maximum)
{
progressBar1.PerformStep();
}
else
{
progressBar1.Value = 0;
timer.Stop();
checkBox1.Checked = false;
Form1_Load(this, EventArgs.Empty);
}
}

Feedback