How to create a control like the Timer

  • Thread starter Thread starter Carsten Unterberg
  • Start date Start date
C

Carsten Unterberg

Hi,

I try to create a control like the Timer in c# with Vs2008, which is visible
as an icon in the designer during the designtime and invisible during the
runtime. In former times, width vb6 it was possible. I think it is possible
in Vs2008 too, but I don't know how...
Does someone knows how?

Thanks

Carsten
 
Hi,

I have a timer control in the Device Components Toolbox group that can
be dragged onto the forms surface and appears as a control at design
time. Whilst this is in VS2005 I assume that it is available in
VS2008 too.

Once you have dragged this control onto the form is resides in the
list of components with no design-time interface (such as dataset,
mainMenu, etc). You can then enable or disable the control, set its
interval, change its name, etc.

Hope this helps.

Jason
 
Hi Jason,

thanks at first for your quick answer. Your answer helped me a little, but
the fact, which I need to know it, what I have to do that my control resides
in that list of componenets below the designer. Which properties from my
control must I set?

Thanks

Carsten
 
Firstly, the timer will not tick if it is not enabled, but I suggest
that you configure the Interval to a valid number of milliseconds
first. Once done you need to enable the control.

This can either all be done using the designer or in code:


// Create a global variable object of the timer control
Timer myTimer = new Timer();

private void Form1_Load(object sender, EventArgs e)
{
// Set the interval to 1 second
myTimer.Interval = 1000;

// Create an event handler to process the tick event of
the timer control
myTimer.Tick += new EventHandler(myTimer_Tick);

// Enable the timer
myTimer.Enabled = true;
}

void myTimer_Tick(object sender, EventArgs e)
{
// Change the label text to the current time
this.label1.Text = DateTime.Now.ToLongTimeString();
}

The code above creates a new timer, sets its interval to 1000
milliseconds (1 sec), sets up an event handler for the tick event and
then enables the timer. Each second the contents of a label named
label1 is updated with the current time.

Hope that helps.

Jason
 
Hi Jason,

thanks, but maybe we missunderstood eachother. I want to create a control
with the same desingtime-/runtime-behavoir, not with the same functionality
as the timer-control.

Thanks

Carsten
 
Back
Top