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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 19, 2011, 12:45 AM EDT
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
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 19, 2011, 12:00 AM EDT
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;
}
}
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 19, 2011, 12:00 AM EDT
if (progressbar.value == 30)
this.opacity -= 0.1;
This should work.
Zoran
Kanhialal kkRank #8,477 · 221 reputationPosted Jul 19, 2011, 12:31 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 19, 2011, 12:33 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 19, 2011, 12:36 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 19, 2011, 12:51 AM EDT
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
}
robert alis5 reputationPosted Jul 20, 2011, 12:53 AM EDT
if (_progressBar.Value == _progressBar.Maximum && this.Opacity == 0)
Close(); // Close the form as it becomes invisible
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 20, 2011, 12:58 AM EDT
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
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 21, 2011, 3:11 PM EDT
Did this code solve your problem?
Zoran
Kanhialal kkRank #8,477 · 221 reputationPosted Jul 23, 2011, 4:45 AM EDT
Zoran HorvatRank #792 · 2.9K reputationPosted Jul 23, 2011, 7:17 AM EDT
Please don't forget to tick "This is correct answer" for the post which has solved the problem.
Thanks,
Zoran