Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2008

Tip: Looking for answers? Try searching our database.

changing properties of progressbar

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
rkbnair - 23 Jan 2008 02:08 GMT
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;
Linda Liu[MSFT] - 23 Jan 2008 10:53 GMT
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.
rkbnair - 23 Jan 2008 15:24 GMT
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?

> Hi,
>
[quoted text clipped - 32 lines]
>  
> This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu[MSFT] - 24 Jan 2008 10:30 GMT
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
rkbnair - 24 Jan 2008 20:19 GMT
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;

> Hi,
>
[quoted text clipped - 50 lines]
> Linda Liu
> Microsoft Online Community Support
rkbnair - 24 Jan 2008 22:41 GMT
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.

> Hi,
>
[quoted text clipped - 50 lines]
> Linda Liu
> Microsoft Online Community Support
Linda Liu[MSFT] - 25 Jan 2008 09:21 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.