Hi,
I am want to (--) decrease the opacity of a form on timer
just like...
if(progressbar.value=30)
{
this.opacity-=10;
}
But this is not working
..
Please help..
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Zoran HorvatPosted Jul 19, 2011, 4:45 AM
Try this source code:
using System.Drawing;
using System.Windows.Forms;
namespace ConsoleApplication
{
class MyForm : Form
{
public MyForm()
{
_progressBar = new ProgressBar();
_progressBar.Minimum = 0;
_progressBar.Maximum = 100;
_progressBar.Value = 0;
_progressBar.Location = new Point(10, 10);
_progressBar.Width = ClientSize.Width - 20;
Controls.Add(_progressBar);
_timer = new System.Threading.Timer(new System.Threading.TimerCallback(TimerCallback), this, 200, 200);
IncreaseProgress(0); // Forces opacity to be recalculated
}
private delegate void IncreaseProgressDelegate(int change);
private void IncreaseProgress(int change)
{
int newValue = _progressBar.Value + change;
if (newValue > _progressBar.Maximum)
newValue = _progressBar.Maximum;
else if (newValue < _progressBar.Minimum)
newValue = _progressBar.Minimum;
_progressBar.Value = newValue;
this.Opacity = (double)_progressBar.Value / ((double)(_progressBar.Maximum - _progressBar.Minimum));
}
private void TimerCallback(object state)
{
MyForm form = (MyForm)state;
form.BeginInvoke(new IncreaseProgressDelegate(IncreaseProgress), 5);
}
private ProgressBar _progressBar;
private System.Threading.Timer _timer;
}
class Program
{
static void Main(string[] args)
{
MyForm form = new MyForm();
form.ShowDialog();
}
}
}
It is a console application but you have to add System.Windows.Forms reference to work.
It creates a form with a progress bar and sets opacity equal to percentual value of the progress. So when progress is 0, opacity is 0 (i.e. form is completely invisible). Progress bar moves on timer and as soon as it reaches its maximum value 100, opacity of the form reaches value 1.0, which means that it is completely visible.
Zoran
Zoran HorvatPosted Jul 23, 2011, 11:17 AM
Please don't forget to tick "This is correct answer" for the post which has solved the problem.
Thanks,
Zoran
Kanhialal kkPosted Jul 23, 2011, 8:45 AM
Zoran HorvatPosted Jul 21, 2011, 7:11 PM
Did this code solve your problem?
Zoran
Zoran HorvatPosted Jul 20, 2011, 4:58 AM
It would only be stupid to set opacity to 0 if that form's controls are the only mean of controling it - then user can't see the close button or something similar.
Zoran
robert alisPosted Jul 20, 2011, 4:53 AM
if (_progressBar.Value == _progressBar.Maximum && this.Opacity == 0)
Close(); // Close the form as it becomes invisible
Zoran HorvatPosted Jul 19, 2011, 4:51 AM
private void IncreaseProgress(int change)
{
int newValue = _progressBar.Value + change;
if (newValue > _progressBar.Maximum)
newValue = _progressBar.Maximum;
else if (newValue < _progressBar.Minimum)
newValue = _progressBar.Minimum;
_progressBar.Value = newValue;
this.Opacity = (double)(_progressBar.Maximum - _progressBar.Value) / ((double)(_progressBar.Maximum - _progressBar.Minimum));
if (_progressBar.Value == _progressBar.Maximum && this.Opacity == 0)
Close(); // Close the form as it becomes invisible
}
Dorababu MekaPosted Jul 19, 2011, 4:36 AM
Dorababu MekaPosted Jul 19, 2011, 4:33 AM
Kanhialal kkPosted Jul 19, 2011, 4:31 AM
Zoran HorvatPosted Jul 19, 2011, 4:00 AM
if (progressbar.value == 30)
this.opacity -= 0.1;
This should work.
Zoran
Dorababu MekaPosted Jul 19, 2011, 4:00 AM
I just created a sample one and code as follows ot works fine for me
private void Form2_Load(object sender, EventArgs e)
{
progressbar.Minimum = 0;
progressbar.Maximum = 100;
progressbar.Value = 30;
if (progressbar.Value == 30)
{
this.Opacity = .75;
}
}