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:
Public Class StatusProgress : Inherits StatusBar 'StatusBar panel that will contain the _ProgressBar. Private _pbPanel As Integer = -1 'StatusBar panel for containing date Private _DtPanelStyle As Integer = -1 'StatusBar panel for containing time. Private _TmrPanelStyle As Integer = -1 'Format for date and time. Private DT_Format As String = "MM/dd/yyyy" Private Tmr_Format As String = "h:mm tt" 'Timer to control time and seconds. Private WithEvents Tmr As New Timer Private Const TmrSecs As Integer = 1000 'New ProgressBar control. Public _ProgressBar As ProgressBar Sub New() ' End Sub #Region " StatusBar Panel Properties" Public Property ProgressBarPanel() As Integer Get Return _pbPanel End Get Set(ByVal Value As Integer) If Value >= 0 Then _ProgressBar = New ProgressBar Me.Controls.Add(_ProgressBar) _pbPanel = Value Me.Panels(_pbPanel).Style = StatusBarPanelStyle.OwnerDraw Else Me.Controls.Remove(_ProgressBar) _ProgressBar = Nothing _pbPanel = Value End If End Set End Property Public Property StatusPanelDate() As Integer Get Return _DtPanelStyle End Get Set(ByVal Value As Integer) _DtPanelStyle = Value Me.Panels(_DtPanelStyle).Text = Format(System.DateTime.Today, DT_Format) End Set End Property Public Property StatusPanelTime() As Integer Get Return _TmrPanelStyle End Get Set(ByVal Value As Integer) _TmrPanelStyle = Value With Tmr .Interval = TmrSecs .Enabled = True .Start() End With Me.Panels(_TmrPanelStyle).Text = Format(System.DateTime.Now, Tmr_Format) End Set End Property Public Property StatusBarDateFormat() As String Get Return DT_Format End Get Set(ByVal Value As String) DT_Format = Value End Set End Property Public Property StatusBarTimeFormat() As String Get Return Tmr_Format End Get Set(ByVal Value As String) Tmr_Format = Value End Set End Property #End Region Private Sub Reposition(ByVal sender As Object, ByVal sbdEvent As System.Windows.Forms.StatusBarDrawItemEventArgs) Handles MyBase.DrawItem If Not IsNothing(_ProgressBar) Then With _ProgressBar .Location = New Point(sbdEvent.Bounds.X, sbdEvent.Bounds.Y) .Size = New Size(sbdEvent.Bounds.Width, sbdEvent.Bounds.Height) _ProgressBar.Show() End With End If End Sub Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick Me.Panels(_TmrPanelStyle).Text = Format(System.DateTime.Now, Tmr_Format) End Sub End Class
Code in my form minus the windows form designer code
VB Code:
Option Explicit On Option Strict On Public Class frmMain : Inherits System.Windows.Forms.Form Private StatusBar As StatusProgress Private sbProgress As New System.Windows.Forms.StatusBarPanel Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call StatusBar = New StatusProgress InitializeStatusProgress() End Sub Private Sub frmMain_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Application.DoEvents() End Sub Private Sub frmMain_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed Me.Dispose(True) End Sub #Region " Encapsulated functions and routines for this form!" Private Sub ThisForm_Close(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click, cmdExit.Click 'Make sure the user wishes to quit. If MessageBox.Show("Are you sure you want to quit?", "Quiting", _ MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then Me.Close() End If End Sub Private Sub InitializeStatusProgress() Dim sbText As New System.Windows.Forms.StatusBarPanel Dim sbDate As New System.Windows.Forms.StatusBarPanel Dim sbTime As New System.Windows.Forms.StatusBarPanel With sbText .Text = "" .AutoSize = StatusBarPanelAutoSize.Spring End With With sbDate .AutoSize = StatusBarPanelAutoSize.Contents End With With sbTime .AutoSize = StatusBarPanelAutoSize.Contents End With With Me.StatusBar With .Panels .Add(sbText) .Add(sbDate) .Add(sbTime) End With .StatusPanelDate = 1 .StatusPanelTime = 2 .ShowPanels = True .Dock = DockStyle.Bottom End With Me.Controls.Add(StatusBar) End Sub #End Region Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click sbProgress.AutoSize = StatusBarPanelAutoSize.Spring With Me.StatusBar .Panels.Add(sbProgress) 'Error occurs here the second time i press the button .ProgressBarPanel = 3 End With End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click With Me.StatusBar .Panels.Remove(sbProgress) .ProgressBarPanel = -1 End With End Sub End Class
thanx in advance


any help would be great
Reply With Quote