We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,171 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How Can I set the Stopwatch Name in runtime?

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)
3
Contributors
6
Replies
22 Hours
Discussion Span
3 Months Ago
Last Updated
7
Views
boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

Reverend Jim
Carpe per diem
Moderator
3,605 posts since Aug 2010
Reputation Points: 561
Solved Threads: 449
Skill Endorsements: 32

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.

TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13

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 ?

boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

TnTinMN
Practically a Master Poster
640 posts since Jun 2012
Reputation Points: 418
Solved Threads: 148
Skill Endorsements: 13

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".

boher
Newbie Poster
12 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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.

Reverend Jim
Carpe per diem
Moderator
3,605 posts since Aug 2010
Reputation Points: 561
Solved Threads: 449
Skill Endorsements: 32

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0930 seconds using 2.69MB