Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: PichusMR en 8 Noviembre 2015, 22:52 pm



Título: Abrir form(formulario) cuando la Progressbar este al 100%
Publicado por: PichusMR en 8 Noviembre 2015, 22:52 pm
Quisiera saber como abrir un nuevo form(formulario) cuando se termine de cargar la progressbar al 100%, no se como hacerlo, ya tengo el timer y la progressbar y asi, aqui dejo la parte del timer y del progressbar, necesito ayuda. Gracias


Uso visual studio 2012 y el lenguaje es C#



Código
  1.        private void Form1_Load(object sender, EventArgs e)
  2.        {
  3.  
  4.            timer1.Start();
  5.  
  6.        }
  7.  
  8.        private void timer1_Tick(object sender, EventArgs e)
  9.        {
  10.  
  11.            PB1.Increment(1);
  12.            lblcarga.Text = PB1.Value.ToString() + "%";
  13.  
  14.  
  15.        }


Título: Re: Abrir form(formulario) cuando la Progressbar este al 100%
Publicado por: nevachana en 8 Noviembre 2015, 23:27 pm
No veo que cargues nada especial mientras el programa carga   >:(
haz un simple (no tengo tengo teclado en condiciuones y no puedo escribir todos los simbolos).
Código
  1. if(int.parse(lblcarga.Text) ; 100)
  2. {
  3. lblcarga.text = int++;
  4. }
  5. else{
  6. timer.enabled = false;
  7. // empiezas el thread del nuevo form.
  8. }


Título: Re: Abrir form(formulario) cuando la Progressbar este al 100%
Publicado por: Eleкtro en 9 Noviembre 2015, 08:44 am
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 :P. 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.

Código
  1. PB1.Increment(1);
  2. blcarga.Text = PB1.Value.ToString() + "%";
  3.  
  4. if ((PB1.Value == PB1.Maximum)) {
  5. MessageBox.Show("Progreso finalizado.", "", MessageBoxButtons.OK);
  6. }



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:
Código
  1. ''' ----------------------------------------------------------------------------------------------------
  2. ''' <summary>
  3. ''' Handles the <see cref="ProgressBarExtended.ValueChanged"/> event of the <see cref="ProgressBarExtended1"/> control.
  4. ''' </summary>
  5. ''' ----------------------------------------------------------------------------------------------------
  6. ''' <param name="sender">
  7. ''' The source of the event.
  8. ''' </param>
  9. '''
  10. ''' <param name="e">
  11. ''' The <see cref="ProgressBarExtended.ValueChangedEventArgs"/> instance containing the event data.
  12. ''' </param>
  13. ''' ----------------------------------------------------------------------------------------------------
  14. Private Sub ProgressBarExtended1_ValueChanged(sender As Object, e As ProgressBarExtended.ValueChangedEventArgs) _
  15. Handles ProgressBarExtended1.ValueChanged
  16.  
  17.    Dim pb As ProgressBarExtended = DirectCast(sender, ProgressBarExtended)
  18.  
  19.    If (e.Value = pb.Maximum) Then
  20.        MessageBox.Show("Progreso finalizado.", "", MessageBoxButtons.OK)
  21.    End If
  22.  
  23. End Sub

Ejemplo de uso en C#:
Código
  1. /// ----------------------------------------------------------------------------------------------------
  2. /// <summary>
  3. /// Handles the <see cref="ProgressBarExtended.ValueChanged"/> event of the <see cref="ProgressBarExtended1"/> control.
  4. /// </summary>
  5. /// ----------------------------------------------------------------------------------------------------
  6. /// <param name="sender">
  7. /// The source of the event.
  8. /// </param>
  9. ///
  10. /// <param name="e">
  11. /// The <see cref="ProgressBarExtended.ValueChangedEventArgs"/> instance containing the event data.
  12. /// </param>
  13. /// ----------------------------------------------------------------------------------------------------
  14. private void ProgressBarExtended1_ValueChanged(object sender, ProgressBarExtended.ValueChangedEventArgs e) {
  15.  
  16. ProgressBarExtended pb = (ProgressBarExtended)sender;
  17. if ((e.Value == pb.Maximum)) {
  18. MessageBox.Show("Progreso finalizado.", "", MessageBoxButtons.OK);
  19. }
  20.  
  21. }

Código fuente original en Vb.Net:
Código
  1. #Region " Option Statements "
  2.  
  3. Option Strict On
  4. Option Explicit On
  5. Option Infer Off
  6.  
  7. #End Region
  8.  
  9. #Region " Imports "
  10.  
  11. Imports System
  12. Imports System.Diagnostics
  13. Imports System.Linq
  14. Imports System.Windows.Forms
  15.  
  16. #End Region
  17.  
  18. ''' ----------------------------------------------------------------------------------------------------
  19. ''' <summary>
  20. ''' An extended <see cref="ProgressBar"/>.
  21. ''' </summary>
  22. ''' ----------------------------------------------------------------------------------------------------
  23. Public Class ProgressBarExtended : Inherits ProgressBar
  24.  
  25. #Region " Events "
  26.  
  27.    ''' ----------------------------------------------------------------------------------------------------
  28.    ''' <summary>
  29.    ''' Occurs when the progress value changes.
  30.    ''' </summary>
  31.    ''' ----------------------------------------------------------------------------------------------------
  32.    Public Custom Event ValueChanged As EventHandler(Of ValueChangedEventArgs)
  33.  
  34.        <DebuggerNonUserCode>
  35.        <DebuggerStepThrough>
  36.        AddHandler(ByVal value As EventHandler(Of ValueChangedEventArgs))
  37.            MyBase.Events.AddHandler("ValueChangedEvent", value)
  38.        End AddHandler
  39.  
  40.        <DebuggerNonUserCode>
  41.        <DebuggerStepThrough>
  42.        RemoveHandler(ByVal value As EventHandler(Of ValueChangedEventArgs))
  43.            MyBase.Events.RemoveHandler("ValueChangedEvent", value)
  44.        End RemoveHandler
  45.  
  46.        <DebuggerNonUserCode>
  47.        <DebuggerStepThrough>
  48.        RaiseEvent(ByVal sender As Object, ByVal e As ValueChangedEventArgs)
  49.            Dim handler As EventHandler(Of ValueChangedEventArgs) =
  50.                DirectCast(MyBase.Events("ValueChangedEvent"), EventHandler(Of ValueChangedEventArgs))
  51.  
  52.            If (handler IsNot Nothing) Then
  53.                handler.Invoke(sender, e)
  54.            End If
  55.        End RaiseEvent
  56.  
  57.    End Event
  58.  
  59. #End Region
  60.  
  61. #Region " Events Data "
  62.  
  63. #Region " ValueChangedEventArgs "
  64.  
  65.    ''' ----------------------------------------------------------------------------------------------------
  66.    ''' <summary>
  67.    ''' Contains the event-data of a <see cref="ValueChanged"/> event.
  68.    ''' </summary>
  69.    ''' ----------------------------------------------------------------------------------------------------
  70.    Public Class ValueChangedEventArgs : Inherits EventArgs
  71.  
  72. #Region " Properties "
  73.  
  74.        ''' ----------------------------------------------------------------------------------------------------
  75.        ''' <summary>
  76.        ''' Gets the value.
  77.        ''' </summary>
  78.        ''' ----------------------------------------------------------------------------------------------------
  79.        ''' <value>
  80.        ''' The value.
  81.        ''' </value>
  82.        ''' ----------------------------------------------------------------------------------------------------
  83.        Public Overridable ReadOnly Property Value() As Integer
  84.            <DebuggerStepThrough>
  85.            Get
  86.                Return Me.valueB
  87.            End Get
  88.        End Property
  89.        ''' ----------------------------------------------------------------------------------------------------
  90.        ''' <summary>
  91.        ''' ( Backing field )
  92.        ''' The value.
  93.        ''' </summary>
  94.        ''' ----------------------------------------------------------------------------------------------------
  95.        Private ReadOnly valueB As Integer
  96.  
  97. #End Region
  98.  
  99. #Region " Constructors "
  100.  
  101.        ''' ----------------------------------------------------------------------------------------------------
  102.        ''' <summary>
  103.        ''' Prevents a default instance of the <see cref="ValueChangedEventArgs"/> class from being created.
  104.        ''' </summary>
  105.        ''' ----------------------------------------------------------------------------------------------------
  106.        <DebuggerNonUserCode>
  107.        Private Sub New()
  108.        End Sub
  109.  
  110.        ''' ----------------------------------------------------------------------------------------------------
  111.        ''' <summary>
  112.        ''' Initializes a new instance of the <see cref="ValueChangedEventArgs"/> class.
  113.        ''' </summary>
  114.        ''' ----------------------------------------------------------------------------------------------------
  115.        ''' <param name="value">
  116.        ''' The value.
  117.        ''' </param>
  118.        ''' ----------------------------------------------------------------------------------------------------
  119.        <DebuggerStepThrough>
  120.        Public Sub New(ByVal value As Integer)
  121.  
  122.            Me.valueB = value
  123.  
  124.        End Sub
  125.  
  126. #End Region
  127.  
  128.    End Class
  129.  
  130. #End Region
  131.  
  132. #End Region
  133.  
  134. #Region " Event Invocators "
  135.  
  136.    ''' ----------------------------------------------------------------------------------------------------
  137.    ''' <summary>
  138.    ''' Raises <see cref="ValueChanged"/> event.
  139.    ''' </summary>
  140.    ''' ----------------------------------------------------------------------------------------------------
  141.    ''' <param name="e">
  142.    ''' The <see cref="ValueChangedEventArgs"/> instance containing the event data.
  143.    ''' </param>
  144.    ''' ----------------------------------------------------------------------------------------------------
  145.    <DebuggerStepThrough>
  146.    Protected Overridable Sub OnValueChanged(ByVal e As ValueChangedEventArgs)
  147.  
  148.        RaiseEvent ValueChanged(Me, e)
  149.  
  150.    End Sub
  151.  
  152. #End Region
  153.  
  154. #Region " Constructors "
  155.  
  156.    ''' ----------------------------------------------------------------------------------------------------
  157.    ''' <summary>
  158.    ''' Initializes a new instance of the <see cref="ProgressBarExtended"/> class.
  159.    ''' </summary>
  160.    ''' ----------------------------------------------------------------------------------------------------
  161.    <DebuggerStepThrough>
  162.    Public Sub New()
  163.  
  164.        MyBase.New()
  165.  
  166.    End Sub
  167.  
  168. #End Region
  169.  
  170. #Region " Properties "
  171.  
  172.    ''' ----------------------------------------------------------------------------------------------------
  173.    ''' <summary>
  174.    ''' Gets or sets the current position of the progress bar.
  175.    ''' </summary>
  176.    ''' ----------------------------------------------------------------------------------------------------
  177.    ''' <value>
  178.    ''' The value.
  179.    ''' </value>
  180.    ''' ----------------------------------------------------------------------------------------------------
  181.    Public Overridable Shadows Property Value As Integer
  182.        <DebuggerStepThrough>
  183.        Get
  184.            Return MyBase.Value
  185.        End Get
  186.        <DebuggerStepThrough>
  187.        Set(ByVal value As Integer)
  188.            Dim oldValue As Integer = MyBase.Value
  189.            MyBase.Value = value
  190.            If (value <> oldValue) Then
  191.                Me.OnValueChanged(New ValueChangedEventArgs(value))
  192.            End If
  193.        End Set
  194.    End Property
  195.  
  196. #End Region
  197.  
  198. #Region " Public Methods "
  199.  
  200.    ''' ----------------------------------------------------------------------------------------------------
  201.    ''' <summary>
  202.    ''' Advances the current position of the progress bar by the specified amount.
  203.    ''' </summary>
  204.    ''' ----------------------------------------------------------------------------------------------------
  205.    ''' <param name="value">
  206.    ''' The amount by which to increment the progress bar's current position.
  207.    ''' </param>
  208.    ''' ----------------------------------------------------------------------------------------------------
  209.    <DebuggerStepThrough>
  210.    Public Overridable Shadows Sub Increment(ByVal value As Integer)
  211.  
  212.        Dim oldValue As Integer = MyBase.Value
  213.        MyBase.Increment(value)
  214.  
  215.        If (oldValue <> MyBase.Maximum) Then
  216.            Me.OnValueChanged(New ValueChangedEventArgs(MyBase.Value))
  217.        End If
  218.  
  219.    End Sub
  220.  
  221. #End Region
  222.  
  223. End Class

Código fuente traducido (online) a C#. no lo he testeado, pero requerirá pequeñas modificaciones en la sintaxis:
Código
  1. #region " Usings "
  2.  
  3. using System;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7.  
  8. #endregion
  9.  
  10. /// ----------------------------------------------------------------------------------------------------
  11. /// <summary>
  12. /// An extended <see cref="ProgressBar"/>.
  13. /// </summary>
  14. /// ----------------------------------------------------------------------------------------------------
  15. public class ProgressBarExtended : ProgressBar
  16. {
  17.  
  18.    #region " Events "
  19.  
  20.    /// ----------------------------------------------------------------------------------------------------
  21.    /// <summary>
  22.    /// Occurs when the progress value changes.
  23.    /// </summary>
  24.    /// ----------------------------------------------------------------------------------------------------
  25.    public event EventHandler<ValueChangedEventArgs> ValueChanged {
  26.  
  27.        [DebuggerNonUserCode()]
  28.        [DebuggerStepThrough()]
  29.        add { base.Events.AddHandler("ValueChangedEvent", Value); }
  30.  
  31.        [DebuggerNonUserCode()]
  32.        [DebuggerStepThrough()]
  33.        remove { base.Events.RemoveHandler("ValueChangedEvent", Value); }
  34.  
  35.    }
  36.    #endregion
  37.  
  38.    #region " Events Data "
  39.  
  40.    #region " ValueChangedEventArgs "
  41.  
  42.    /// ----------------------------------------------------------------------------------------------------
  43.    /// <summary>
  44.    /// Contains the event-data of a <see cref="ValueChanged"/> event.
  45.    /// </summary>
  46.    /// ----------------------------------------------------------------------------------------------------
  47.    public class ValueChangedEventArgs : EventArgs
  48.    {
  49.  
  50.        #region " Properties "
  51.  
  52.        /// ----------------------------------------------------------------------------------------------------
  53.        /// <summary>
  54.        /// Gets the value.
  55.        /// </summary>
  56.        /// ----------------------------------------------------------------------------------------------------
  57.        /// <value>
  58.        /// The value.
  59.        /// </value>
  60.        /// ----------------------------------------------------------------------------------------------------
  61.        public virtual int Value {
  62.            [DebuggerStepThrough()]
  63.            get { return this.valueB; }
  64.        }
  65.  
  66.        /// ----------------------------------------------------------------------------------------------------
  67.        /// <summary>
  68.        /// ( Backing field )
  69.        /// The value.
  70.        /// </summary>
  71.        /// ----------------------------------------------------------------------------------------------------
  72.        private readonly int valueB;
  73.        #endregion
  74.  
  75.        #region " Constructors "
  76.  
  77.        /// ----------------------------------------------------------------------------------------------------
  78.        /// <summary>
  79.        /// Prevents a default instance of the <see cref="ValueChangedEventArgs"/> class from being created.
  80.        /// </summary>
  81.        /// ----------------------------------------------------------------------------------------------------
  82.        [DebuggerNonUserCode()]
  83.        private ValueChangedEventArgs()
  84.        {
  85.        }
  86.  
  87.        /// ----------------------------------------------------------------------------------------------------
  88.        /// <summary>
  89.        /// Initializes a new instance of the <see cref="ValueChangedEventArgs"/> class.
  90.        /// </summary>
  91.        /// ----------------------------------------------------------------------------------------------------
  92.        /// <param name="value">
  93.        /// The value.
  94.        /// </param>
  95.        /// ----------------------------------------------------------------------------------------------------
  96.        [DebuggerStepThrough()]
  97.        public ValueChangedEventArgs(int value)
  98.        {
  99.            this.valueB = value;
  100.        }
  101.  
  102.        #endregion
  103.  
  104.    }
  105.  
  106.    #endregion
  107.  
  108.    #endregion
  109.  
  110.    #region " Event Invocators "
  111.  
  112.    /// ----------------------------------------------------------------------------------------------------
  113.    /// <summary>
  114.    /// Raises <see cref="ValueChanged"/> event.
  115.    /// </summary>
  116.    /// ----------------------------------------------------------------------------------------------------
  117.    /// <param name="e">
  118.    /// The <see cref="ValueChangedEventArgs"/> instance containing the event data.
  119.    /// </param>
  120.    /// ----------------------------------------------------------------------------------------------------
  121.    [DebuggerStepThrough()]
  122.    protected virtual void OnValueChanged(ValueChangedEventArgs e)
  123.    {
  124.        if (ValueChanged != null) {
  125.            ValueChanged(this, e);
  126.        }
  127.    }
  128.  
  129.    #endregion
  130.  
  131.    #region " Constructors "
  132.  
  133.    /// ----------------------------------------------------------------------------------------------------
  134.    /// <summary>
  135.    /// Initializes a new instance of the <see cref="ProgressBarExtended"/> class.
  136.    /// </summary>
  137.    /// ----------------------------------------------------------------------------------------------------
  138.    [DebuggerStepThrough()]
  139.    public ProgressBarExtended() : base()
  140.    {}
  141.  
  142.    #endregion
  143.  
  144.    #region " Properties "
  145.  
  146.    /// ----------------------------------------------------------------------------------------------------
  147.    /// <summary>
  148.    /// Gets or sets the current position of the progress bar.
  149.    /// </summary>
  150.    /// ----------------------------------------------------------------------------------------------------
  151.    /// <value>
  152.    /// The value.
  153.    /// </value>
  154.    /// ----------------------------------------------------------------------------------------------------
  155.    public virtual new int Value {
  156.        [DebuggerStepThrough()]
  157.        get { return base.Value; }
  158.        [DebuggerStepThrough()]
  159.        set {
  160.            int oldValue = base.Value;
  161.            base.Value = value;
  162.            if ((value != oldValue)) {
  163.                this.OnValueChanged(new ValueChangedEventArgs(value));
  164.            }
  165.        }
  166.    }
  167.  
  168.    #endregion
  169.  
  170.    #region " Public Methods "
  171.  
  172.    /// ----------------------------------------------------------------------------------------------------
  173.    /// <summary>
  174.    /// Advances the current position of the progress bar by the specified amount.
  175.    /// </summary>
  176.    /// ----------------------------------------------------------------------------------------------------
  177.    /// <param name="value">
  178.    /// The amount by which to increment the progress bar's current position.
  179.    /// </param>
  180.    /// ----------------------------------------------------------------------------------------------------
  181.    [DebuggerStepThrough()]
  182.    public virtual new void Increment(int value)
  183.    {
  184.        int oldValue = base.Value;
  185.        base.Increment(value);
  186.  
  187.        if ((oldValue != base.Maximum)) {
  188.            this.OnValueChanged(new ValueChangedEventArgs(base.Value));
  189.        }
  190.  
  191.    }
  192.  
  193.    #endregion
  194.  
  195. }
  196.  
  197. //=======================================================
  198. //Service provided by Telerik (www.telerik.com)
  199. //=======================================================