changing properties of progressbar

  • Thread starter Thread starter rkbnair
  • Start date Start date
R

rkbnair

How can I set the value of a progressbar in a wait window?

frm (it contains the progressbar) is a form that is called from another
program.
I get a compilation error at the first line below.

System.Windows.Forms.ProgressBar prgrs=
frm.Controls["progressBar1"];
prgrs.Maximum = int_progressbar_max;
prgrs.value = 0;
 
Hi,
I get a compilation error at the first line below.
System.Windows.Forms.ProgressBar prgrs= frm.Controls["progressBar1"];

What's the error do you get?
If you can get the instance of the wait window, you can access controls on
this window through its Controls collection without any problem.

If the problem is still not solved, could you please send me a simple
project that could just reproduce the problem? To get my actual email
address, remove 'online' from my displayed email address.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ok I fixed the error by syntaxing this way:
System.Windows.Forms.ProgressBar prgrs =
(System.Windows.Forms.ProgressBar)frm.Controls["progressBar1"];

You said that any control can be accessed through the control collection.
However how will you access the properties of those controls?


Linda Liu said:
Hi,
I get a compilation error at the first line below.
System.Windows.Forms.ProgressBar prgrs= frm.Controls["progressBar1"];

What's the error do you get?
If you can get the instance of the wait window, you can access controls on
this window through its Controls collection without any problem.

If the problem is still not solved, could you please send me a simple
project that could just reproduce the problem? To get my actual email
address, remove 'online' from my displayed email address.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

Thank you for your reply!
However how will you access the properties of those controls?

If your only read from the properties of a control, you can do it directly.
For example,
int value = progressBar1.Value;

If you want to change the values of properties of a control and you will do
this from within the thread that this control is created on, you can do it
directly. For example, you add a ProgressBar on a form and you can change
the Value of this ProgressBar control from another form within this
application:
form1.Controls["progressBar1"].Value = 100;

However, if you want to change the values of properties of a control and
you will do this from a thread other than the thread that this control is
created on, you need to call the Control.Invoke or Control.BeginInvoke
method to execute a delegate on the thread that owns the control's
underlying window handle; otherwise, this will raise the Cross-thread
operation exception. The following is a sample.

public partial class Form1 : Form
{
delegate void SetProgressBarValueDelegate(object value);
public void SetProgressBarValue(object value)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new
SetProgressBarValueDelegate(SetProgressBarValue), new object[] { value });
}
else
{
this.progressBar1.Value = Convert.ToInt32(value);
}
}
}

Then call the SetProgressBarValue method of the Form1 to change the value
of the ProgressBar control safely.

For more information on how to make thread-safe calls to Windows Forms
controls, please refer to the following MSDN document:
http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi Linda,

Thanks for the info.
The given code works fine. But feel hard to understand. Do you have any more
documentation regarding this usage?

Do you mean the following usage will create thread problems?
System.Windows.Forms.ProgressBar prgrs =
(System.Windows.Forms.ProgressBar)frm.Controls["progressBar1"];
prgrs.Maximum = int_progressbar_max;
prgrs.Value = 0;



Linda Liu said:
Hi,

Thank you for your reply!
However how will you access the properties of those controls?

If your only read from the properties of a control, you can do it directly.
For example,
int value = progressBar1.Value;

If you want to change the values of properties of a control and you will do
this from within the thread that this control is created on, you can do it
directly. For example, you add a ProgressBar on a form and you can change
the Value of this ProgressBar control from another form within this
application:
form1.Controls["progressBar1"].Value = 100;

However, if you want to change the values of properties of a control and
you will do this from a thread other than the thread that this control is
created on, you need to call the Control.Invoke or Control.BeginInvoke
method to execute a delegate on the thread that owns the control's
underlying window handle; otherwise, this will raise the Cross-thread
operation exception. The following is a sample.

public partial class Form1 : Form
{
delegate void SetProgressBarValueDelegate(object value);
public void SetProgressBarValue(object value)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new
SetProgressBarValueDelegate(SetProgressBarValue), new object[] { value });
}
else
{
this.progressBar1.Value = Convert.ToInt32(value);
}
}
}

Then call the SetProgressBarValue method of the Form1 to change the value
of the ProgressBar control safely.

For more information on how to make thread-safe calls to Windows Forms
controls, please refer to the following MSDN document:
http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hello Linda,

Could you explain what you mean by multiple threads? Is it possible to
explain it with an example please. My calling program is processing some
records of a dataset. Each record, it should do some operations. So, I want
to display a wait window with progressbar in it. Will this be a multithread?
Or how it can be a multithread application?

Thanks.

Linda Liu said:
Hi,

Thank you for your reply!
However how will you access the properties of those controls?

If your only read from the properties of a control, you can do it directly.
For example,
int value = progressBar1.Value;

If you want to change the values of properties of a control and you will do
this from within the thread that this control is created on, you can do it
directly. For example, you add a ProgressBar on a form and you can change
the Value of this ProgressBar control from another form within this
application:
form1.Controls["progressBar1"].Value = 100;

However, if you want to change the values of properties of a control and
you will do this from a thread other than the thread that this control is
created on, you need to call the Control.Invoke or Control.BeginInvoke
method to execute a delegate on the thread that owns the control's
underlying window handle; otherwise, this will raise the Cross-thread
operation exception. The following is a sample.

public partial class Form1 : Form
{
delegate void SetProgressBarValueDelegate(object value);
public void SetProgressBarValue(object value)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new
SetProgressBarValueDelegate(SetProgressBarValue), new object[] { value });
}
else
{
this.progressBar1.Value = Convert.ToInt32(value);
}
}
}

Then call the SetProgressBarValue method of the Form1 to change the value
of the ProgressBar control safely.

For more information on how to make thread-safe calls to Windows Forms
controls, please refer to the following MSDN document:
http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hi,

Thank you for your reply! I'm sorry that I didn't make myself clear in my
previous reply.

If both the calling form and the wait form are in one application, and you
don't create multiple threads by creating Thread objects explicitly or
calling the BeginInvoke method on a delegate, there's no multiple threads
in your application and you can access the ProgressBar instance on the wait
form from the calling form directly.

For more information on Thread class and asynchronous programming using
Delegates, please refer to the following MSDN documents:

'Thread Class'
http://msdn2.microsoft.com/en-us/library/system.threading.thread.aspx

'Asynchronous Programming Using Delegates'
http://msdn2.microsoft.com/en-us/library/22t547yb.aspx

The reason why I mentioned multiple threads in my previous reply is that I
noticed you said "frm (it contains the progressbar) is a form that is
called from another
program." in your first message. If the calling form and the wait form in
your practice belong to two running applications respectively, multiple
threads issue is related because the calling form and the wait form are
running on different threads.

Hope I make myself clear now.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top