1,105,592 Community Members

How Can I set the Stopwatch Name in runtime?

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I need a solution for a small problem in my project and I'll make it as simple as possible:

At one of the forms in my project, user should be able to add a panel to the form and that form should contain 2 controls ... a label and a (stopwatch or timer).

The problem is after creating the stopwatch dynamically, I can't set a name for the stopwatch. Later in that form when user clicks on the panel stopwatch must start. How can I set the name for created stopwatch? or any other solution if my way is too stupid. Here's what I have :

Dim Panels As Integer = ds.Tables("Pool").Rows.Count
        For i = 1 To Panels 
            'set the new panel number
            Dim NextPanelNumber As Integer = FlowLayoutPanel1.Controls.Count + 1
            'controls to be added
            Dim NextPanel As New PanelControl
            Dim NextPanelLabel As New LabelControl
            Dim NextPanelStopwatch As New Stopwatch
            'determine properties for new table
            NextPanel.Width = 200
            NextPanel.Height = 114
            NextPanelStopwatch.Name = "anything" ' <-- PROBLEM IS HERE

            'add controls into new panel
            NextPanel.Controls.Add(NextPanelLabel)
Member Avatar
Reverend Jim
Noli mentula
5,450 posts since Aug 2010
Reputation Points: 761 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

The name is NextPanelStopwatch. To start it you do

NextPanelStopwatch.Start

Just make sure you declare it where it won't go out of scope.

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
2
 

or any other solution if my way is too stupid

I would not say its stupid, just not well thought out. :)

Look at what you wrote as one of your goals: " Later in that form when user clicks on the panel stopwatch must start".

To accomplish this, you will be handling the panel's "Click" event. That has nothing to do with the name that you would assign to the Stopwatch ****if it had a Name property. ****

What you do need however is a reference to the StopWatch so that you can start and stop it. All Controls have a "Tag" Property, so you could assign the StopWatch to the Panel's Tag.

However this is just plain messy and hard to maintain. I suggest that you consider creating a UserControl instead. It would house it's own timer and Click logic. Then you would just add an instance of that instead of the panel to your form.

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

TnTinMN I see what you mean but I think I didn't explain the code correctly. As you can see the 'FOR' statement counts the number of panels in the flowlayoutpanel and must create a stopwatch for each panel and name them after the panel number for ex. if there are 3 panels (panel1, panel2, panel3) the code must create 3 stopwatches with names : stopwatch1, stopwatch2 and stopwatch3. How to dod that ?

Member Avatar
TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418 [?]
Q&As Helped to Solve: 152 [?]
Skill Endorsements: 16 [?]
 
0
 

What is StopWatch, because the System.Diagnostics.Stopwatch that I am thinking of does not have a name property?

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch_properties.aspx

Or did you subclass it and add your own Name Property?

We need to be on the same page.

Member Avatar
boher
Newbie Poster
14 posts since Jan 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

The Stopwatch control gets its name from the dim statement right where you state it. that's what I know at least. I mean like :

Dim Stopwatch1 as Stopwatch

The name of that stopwatch would be "Stopwatch1".

Member Avatar
Reverend Jim
Noli mentula
5,450 posts since Aug 2010
Reputation Points: 761 [?]
Q&As Helped to Solve: 653 [?]
Skill Endorsements: 51 [?]
Moderator
Featured
 
0
 

Technically that not its name. That's just a reference to it. Consider

Dim sw1 As New StopWatch
Dim sw2 As StopWatch = sw1

sw1 and sw2 are both references to the same object but neither is the object's name. For most controls, Name is an actual property of the object (intrinsic). sw1 and sw2 exist separately from the object (extrinsic). When you destroy the object, sw1 and sw2 continue to exist.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: