Por favor, a los dos, usad las etiquetas GeShi para insertar bloques de código. Leed Las normas del foro de programación.no se como hacerlo, ya tengo el timer
Empezamos mal asumiendo que todo se soluciona o se debe solucionar con un
Timer . De todas formas, la solución al código que actualmente tienes sería comparar el valor actual del progreso,
ProgressBar.Value, con el valor máximo, la propiedad
ProgressBar.Maximum.
PB1.Increment(1);
blcarga.Text = PB1.Value.ToString() + "%";
if ((PB1.Value == PB1.Maximum)) {
MessageBox.Show("Progreso finalizado.", "", MessageBoxButtons.OK);
}
Yo optaría por heredar y extender la class
ProgressBar para exponer un evento llamado
ValueChanged que se dispare cuando el valor o posición de progreso cambie, y entonces comodamente podriamos suscribirnos a dicho evento.
Ejemplo de uso en Vb.Net:
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Handles the <see cref="ProgressBarExtended.ValueChanged"/> event of the <see cref="ProgressBarExtended1"/> control.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="sender">
''' The source of the event.
''' </param>
'''
''' <param name="e">
''' The <see cref="ProgressBarExtended.ValueChangedEventArgs"/> instance containing the event data.
''' </param>
''' ----------------------------------------------------------------------------------------------------
Private Sub ProgressBarExtended1_ValueChanged(sender As Object, e As ProgressBarExtended.ValueChangedEventArgs) _
Handles ProgressBarExtended1.ValueChanged
Dim pb As ProgressBarExtended = DirectCast(sender, ProgressBarExtended)
If (e.Value = pb.Maximum) Then
MessageBox.Show("Progreso finalizado.", "", MessageBoxButtons.OK)
End If
End Sub
Ejemplo de uso en C#:
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Handles the <see cref="ProgressBarExtended.ValueChanged"/> event of the <see cref="ProgressBarExtended1"/> control.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <param name="sender">
/// The source of the event.
/// </param>
///
/// <param name="e">
/// The <see cref="ProgressBarExtended.ValueChangedEventArgs"/> instance containing the event data.
/// </param>
/// ----------------------------------------------------------------------------------------------------
private void ProgressBarExtended1_ValueChanged(object sender, ProgressBarExtended.ValueChangedEventArgs e) {
ProgressBarExtended pb = (ProgressBarExtended)sender;
if ((e.Value == pb.Maximum)) {
MessageBox.Show("Progreso finalizado.", "", MessageBoxButtons.OK);
}
}
Código fuente original en Vb.Net:
#Region " Option Statements "
Option Strict On
Option Explicit On
Option Infer Off
#End Region
#Region " Imports "
Imports System
Imports System.Diagnostics
Imports System.Linq
Imports System.Windows.Forms
#End Region
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' An extended <see cref="ProgressBar"/>.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public Class ProgressBarExtended : Inherits ProgressBar
#Region " Events "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Occurs when the progress value changes.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public Custom Event ValueChanged As EventHandler(Of ValueChangedEventArgs)
<DebuggerNonUserCode>
<DebuggerStepThrough>
AddHandler(ByVal value As EventHandler(Of ValueChangedEventArgs))
MyBase.Events.AddHandler("ValueChangedEvent", value)
End AddHandler
<DebuggerNonUserCode>
<DebuggerStepThrough>
RemoveHandler(ByVal value As EventHandler(Of ValueChangedEventArgs))
MyBase.Events.RemoveHandler("ValueChangedEvent", value)
End RemoveHandler
<DebuggerNonUserCode>
<DebuggerStepThrough>
RaiseEvent(ByVal sender As Object, ByVal e As ValueChangedEventArgs)
Dim handler As EventHandler(Of ValueChangedEventArgs) =
DirectCast(MyBase.Events("ValueChangedEvent"), EventHandler(Of ValueChangedEventArgs))
If (handler IsNot Nothing) Then
handler.Invoke(sender, e)
End If
End RaiseEvent
End Event
#End Region
#Region " Events Data "
#Region " ValueChangedEventArgs "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Contains the event-data of a <see cref="ValueChanged"/> event.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public Class ValueChangedEventArgs : Inherits EventArgs
#Region " Properties "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets the value.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <value>
''' The value.
''' </value>
''' ----------------------------------------------------------------------------------------------------
Public Overridable ReadOnly Property Value() As Integer
<DebuggerStepThrough>
Get
Return Me.valueB
End Get
End Property
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' ( Backing field )
''' The value.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Private ReadOnly valueB As Integer
#End Region
#Region " Constructors "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Prevents a default instance of the <see cref="ValueChangedEventArgs"/> class from being created.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
<DebuggerNonUserCode>
Private Sub New()
End Sub
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Initializes a new instance of the <see cref="ValueChangedEventArgs"/> class.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="value">
''' The value.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Sub New(ByVal value As Integer)
Me.valueB = value
End Sub
#End Region
End Class
#End Region
#End Region
#Region " Event Invocators "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Raises <see cref="ValueChanged"/> event.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="e">
''' The <see cref="ValueChangedEventArgs"/> instance containing the event data.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Protected Overridable Sub OnValueChanged(ByVal e As ValueChangedEventArgs)
RaiseEvent ValueChanged(Me, e)
End Sub
#End Region
#Region " Constructors "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Initializes a new instance of the <see cref="ProgressBarExtended"/> class.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Sub New()
MyBase.New()
End Sub
#End Region
#Region " Properties "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Gets or sets the current position of the progress bar.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <value>
''' The value.
''' </value>
''' ----------------------------------------------------------------------------------------------------
Public Overridable Shadows Property Value As Integer
<DebuggerStepThrough>
Get
Return MyBase.Value
End Get
<DebuggerStepThrough>
Set(ByVal value As Integer)
Dim oldValue As Integer = MyBase.Value
MyBase.Value = value
If (value <> oldValue) Then
Me.OnValueChanged(New ValueChangedEventArgs(value))
End If
End Set
End Property
#End Region
#Region " Public Methods "
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Advances the current position of the progress bar by the specified amount.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
''' <param name="value">
''' The amount by which to increment the progress bar's current position.
''' </param>
''' ----------------------------------------------------------------------------------------------------
<DebuggerStepThrough>
Public Overridable Shadows Sub Increment(ByVal value As Integer)
Dim oldValue As Integer = MyBase.Value
MyBase.Increment(value)
If (oldValue <> MyBase.Maximum) Then
Me.OnValueChanged(New ValueChangedEventArgs(MyBase.Value))
End If
End Sub
#End Region
End Class
Código fuente traducido (online) a C#. no lo he testeado, pero requerirá pequeñas modificaciones en la sintaxis:
#region " Usings "
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
#endregion
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// An extended <see cref="ProgressBar"/>.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
public class ProgressBarExtended : ProgressBar
{
#region " Events "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Occurs when the progress value changes.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
public event EventHandler<ValueChangedEventArgs> ValueChanged {
[DebuggerNonUserCode()]
[DebuggerStepThrough()]
add { base.Events.AddHandler("ValueChangedEvent", Value); }
[DebuggerNonUserCode()]
[DebuggerStepThrough()]
remove { base.Events.RemoveHandler("ValueChangedEvent", Value); }
}
#endregion
#region " Events Data "
#region " ValueChangedEventArgs "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Contains the event-data of a <see cref="ValueChanged"/> event.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
public class ValueChangedEventArgs : EventArgs
{
#region " Properties "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Gets the value.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <value>
/// The value.
/// </value>
/// ----------------------------------------------------------------------------------------------------
public virtual int Value {
[DebuggerStepThrough()]
get { return this.valueB; }
}
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// ( Backing field )
/// The value.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
private readonly int valueB;
#endregion
#region " Constructors "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Prevents a default instance of the <see cref="ValueChangedEventArgs"/> class from being created.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
[DebuggerNonUserCode()]
private ValueChangedEventArgs()
{
}
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the <see cref="ValueChangedEventArgs"/> class.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <param name="value">
/// The value.
/// </param>
/// ----------------------------------------------------------------------------------------------------
[DebuggerStepThrough()]
public ValueChangedEventArgs(int value)
{
this.valueB = value;
}
#endregion
}
#endregion
#endregion
#region " Event Invocators "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Raises <see cref="ValueChanged"/> event.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <param name="e">
/// The <see cref="ValueChangedEventArgs"/> instance containing the event data.
/// </param>
/// ----------------------------------------------------------------------------------------------------
[DebuggerStepThrough()]
protected virtual void OnValueChanged(ValueChangedEventArgs e)
{
if (ValueChanged != null) {
ValueChanged(this, e);
}
}
#endregion
#region " Constructors "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the <see cref="ProgressBarExtended"/> class.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
[DebuggerStepThrough()]
public ProgressBarExtended() : base()
{}
#endregion
#region " Properties "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Gets or sets the current position of the progress bar.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <value>
/// The value.
/// </value>
/// ----------------------------------------------------------------------------------------------------
public virtual new int Value
{ [DebuggerStepThrough()]
get { return base.Value; }
[DebuggerStepThrough()]
set {
int oldValue = base.Value;
base.Value = value;
if ((value != oldValue)) {
this.OnValueChanged(new ValueChangedEventArgs
(value
)); }
}
}
#endregion
#region " Public Methods "
/// ----------------------------------------------------------------------------------------------------
/// <summary>
/// Advances the current position of the progress bar by the specified amount.
/// </summary>
/// ----------------------------------------------------------------------------------------------------
/// <param name="value">
/// The amount by which to increment the progress bar's current position.
/// </param>
/// ----------------------------------------------------------------------------------------------------
[DebuggerStepThrough()]
public virtual new void Increment
(int value
) {
int oldValue = base.Value;
base.Increment(value);
if ((oldValue != base.Maximum)) {
this.OnValueChanged(new ValueChangedEventArgs
(base.Value)); }
}
#endregion
}
//=======================================================
//Service provided by Telerik (www.telerik.com)
//=======================================================