Results 1 to 2 of 2

Thread: Statusbar with progressbar *RESOLVED*

Threaded View

  1. #1

    Thread Starter
    Frenzied Member vbdotnetboy's Avatar
    Join Date
    Jun 2004
    Location
    Lewisburg, PA "Next year Raiders in the Super Bowl"
    Posts
    1,310

    Question Statusbar with progressbar *RESOLVED*

    i have created a class inherits the statusbar so that i can add a progressbar to it. you can also add date and time to the statusbar. however i don't want the progressbar display right away. no problem with that. no problem displaying it the FIRST time, but then lets say i remove the progressbar and then try to add it to the statusbar again and i get an exception, "Object already has parent." I have no idea what i'm doing wrong. as of right now i've been testing to make sure i can display and then remove the progressbar with two buttons. after i get it working it will be display and removed by events...if i can get by this exception... any help would be great


    Code for the class
    VB Code:
    1. Public Class StatusProgress : Inherits StatusBar
    2.     'StatusBar panel that will contain the _ProgressBar.
    3.     Private _pbPanel As Integer = -1
    4.     'StatusBar panel for containing date
    5.     Private _DtPanelStyle As Integer = -1
    6.     'StatusBar panel for containing time.
    7.     Private _TmrPanelStyle As Integer = -1
    8.     'Format for date and time.
    9.     Private DT_Format As String = "MM/dd/yyyy"
    10.     Private Tmr_Format As String = "h:mm tt"
    11.     'Timer to control time and seconds.
    12.     Private WithEvents Tmr As New Timer
    13.     Private Const TmrSecs As Integer = 1000
    14.  
    15.     'New ProgressBar control.
    16.     Public _ProgressBar As ProgressBar
    17.  
    18.     Sub New()
    19.         '
    20.     End Sub
    21.  
    22. #Region " StatusBar Panel Properties"
    23.     Public Property ProgressBarPanel() As Integer
    24.         Get
    25.             Return _pbPanel
    26.         End Get
    27.         Set(ByVal Value As Integer)
    28.             If Value >= 0 Then
    29.                 _ProgressBar = New ProgressBar
    30.                 Me.Controls.Add(_ProgressBar)
    31.                 _pbPanel = Value
    32.                 Me.Panels(_pbPanel).Style = StatusBarPanelStyle.OwnerDraw
    33.             Else
    34.                 Me.Controls.Remove(_ProgressBar)
    35.                 _ProgressBar = Nothing
    36.                 _pbPanel = Value
    37.             End If
    38.         End Set
    39.     End Property
    40.  
    41.     Public Property StatusPanelDate() As Integer
    42.         Get
    43.             Return _DtPanelStyle
    44.         End Get
    45.         Set(ByVal Value As Integer)
    46.             _DtPanelStyle = Value
    47.             Me.Panels(_DtPanelStyle).Text = Format(System.DateTime.Today, DT_Format)
    48.         End Set
    49.     End Property
    50.  
    51.     Public Property StatusPanelTime() As Integer
    52.         Get
    53.             Return _TmrPanelStyle
    54.         End Get
    55.         Set(ByVal Value As Integer)
    56.             _TmrPanelStyle = Value
    57.             With Tmr
    58.                 .Interval = TmrSecs
    59.                 .Enabled = True
    60.                 .Start()
    61.             End With
    62.             Me.Panels(_TmrPanelStyle).Text = Format(System.DateTime.Now, Tmr_Format)
    63.         End Set
    64.     End Property
    65.  
    66.     Public Property StatusBarDateFormat() As String
    67.         Get
    68.             Return DT_Format
    69.         End Get
    70.         Set(ByVal Value As String)
    71.             DT_Format = Value
    72.         End Set
    73.     End Property
    74.  
    75.     Public Property StatusBarTimeFormat() As String
    76.         Get
    77.             Return Tmr_Format
    78.         End Get
    79.         Set(ByVal Value As String)
    80.             Tmr_Format = Value
    81.         End Set
    82.     End Property
    83. #End Region
    84.  
    85.     Private Sub Reposition(ByVal sender As Object, ByVal sbdEvent As System.Windows.Forms.StatusBarDrawItemEventArgs) Handles MyBase.DrawItem
    86.         If Not IsNothing(_ProgressBar) Then
    87.             With _ProgressBar
    88.                 .Location = New Point(sbdEvent.Bounds.X, sbdEvent.Bounds.Y)
    89.                 .Size = New Size(sbdEvent.Bounds.Width, sbdEvent.Bounds.Height)
    90.                 _ProgressBar.Show()
    91.             End With
    92.         End If
    93.     End Sub
    94.  
    95.     Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick
    96.         Me.Panels(_TmrPanelStyle).Text = Format(System.DateTime.Now, Tmr_Format)
    97.     End Sub
    98. End Class

    Code in my form minus the windows form designer code
    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3.  
    4. Public Class frmMain : Inherits System.Windows.Forms.Form
    5.     Private StatusBar As StatusProgress
    6.     Private sbProgress As New System.Windows.Forms.StatusBarPanel
    7.  
    8.     Public Sub New()
    9.         MyBase.New()
    10.  
    11.         'This call is required by the Windows Form Designer.
    12.         InitializeComponent()
    13.  
    14.         'Add any initialization after the InitializeComponent() call
    15.         StatusBar = New StatusProgress
    16.         InitializeStatusProgress()
    17.     End Sub
    18.  
    19.     Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    20.         Application.DoEvents()
    21.     End Sub
    22.  
    23.     Private Sub frmMain_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    24.         Me.Dispose(True)
    25.     End Sub
    26.  
    27. #Region " Encapsulated functions and routines for this form!"
    28.     Private Sub ThisForm_Close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click, cmdExit.Click
    29.         'Make sure the user wishes to quit.
    30.         If MessageBox.Show("Are you sure you want to quit?", "Quiting", _
    31.             MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
    32.             Me.Close()
    33.         End If
    34.     End Sub
    35.  
    36.     Private Sub InitializeStatusProgress()
    37.         Dim sbText As New System.Windows.Forms.StatusBarPanel
    38.         Dim sbDate As New System.Windows.Forms.StatusBarPanel
    39.         Dim sbTime As New System.Windows.Forms.StatusBarPanel
    40.  
    41.         With sbText
    42.             .Text = ""
    43.             .AutoSize = StatusBarPanelAutoSize.Spring
    44.         End With
    45.         With sbDate
    46.             .AutoSize = StatusBarPanelAutoSize.Contents
    47.         End With
    48.         With sbTime
    49.             .AutoSize = StatusBarPanelAutoSize.Contents
    50.         End With
    51.  
    52.         With Me.StatusBar
    53.             With .Panels
    54.                 .Add(sbText)
    55.                 .Add(sbDate)
    56.                 .Add(sbTime)
    57.             End With
    58.             .StatusPanelDate = 1
    59.             .StatusPanelTime = 2
    60.             .ShowPanels = True
    61.             .Dock = DockStyle.Bottom
    62.         End With
    63.  
    64.         Me.Controls.Add(StatusBar)
    65.     End Sub
    66. #End Region
    67.  
    68.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    69.         sbProgress.AutoSize = StatusBarPanelAutoSize.Spring
    70.         With Me.StatusBar
    71.             .Panels.Add(sbProgress)  'Error occurs here the second time i press the button
    72.             .ProgressBarPanel = 3
    73.         End With
    74.     End Sub
    75.  
    76.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    77.         With Me.StatusBar
    78.             .Panels.Remove(sbProgress)
    79.             .ProgressBarPanel = -1
    80.         End With
    81.     End Sub
    82. End Class


    thanx in advance
    Last edited by vbdotnetboy; Aug 6th, 2004 at 12:06 PM.

    Derek - Using VS 2008 99% of the time and VS 2003 1% of the time

    Please Help Us To Save Ana

    ● Helpful Links: DNR TV | Awesome site for tips | Using ADO.NET to work with Excel | Xml Namespace 2.0 Framework Changes|Ultra High Security Password Generator | Mendhak's ADO.NET Tutorial
    ● Code Bank: Random Password Generator | Generic DbProviderFactory Access
    ● Site Work: Bottle Run Xtreme | Spaids Racing.com

    Company I work for - CSSI

    WHEN POSTING PLEASE INDICATE VERSION

    Please use vbcode tags or code tags when posting code
    [highlight=vb]ALL your code goes here[/highlight] or [code]ALL your code goes here[/code]

    If my post helped you in anyway... please be kind and give me some ratings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width