• No results found

Keeping Track 65

In document c#.net (Page 80-83)

Controls for Windows Forms

3.3 Keeping Track 65

3.3.2

NumericUpDown

We can use a NumericUpDown control to specify a number. The user can change the value of the number in a NumericUpDowncontrol by clicking the up or down buttons it contains. The Minimumvalue by default is 0 and the

Maximumis 100. The default Incrementon each button click is 1, representing the amount to increment when clicking the up button or decrement when clicking the down button.

TheDecimalPlacesproperty starts with a default value of0, representing the number of places to show following the decimal. If the ThousandsSeparator

property is False, the digits will not be grouped in threes. The Value dis-played in a NumericUpDownis a Decimaltype that we will discuss later.

3.3.3 Enabling the Display

We use a DateTimePickerto allow the user to select a date, and a NumericUp-Downto allow the user to select a number. For now we just display each of the selected values in a label when the user clicks the Show button. We need to write the event handler for the button’s Clickevent. Clicking on the but-ton in the design form displays the code template

private void showButton_Click

(object sender, System.EventArgs e) {

}

The event-handling code should copy the value from the DateTimePicker

named date to the Label control under it named dateDisplay. The Value

property of a DateTimePicker holds the date that the user selected. The

ToLongDateStringmethod will convert this value to a date in the Longformat described above. The line of code we need to add is

dateDisplay.Text =

"Date selected: " + date.Value.ToLongDateString();

The event-handling code for the Showbutton should also copy the number selected using the NumericUpDowncontrol named numberto the Labelbelow it namednumberDisplay. The Valueproperty of the NumericUpDowncontrol holds the number the user selected. The ToStringmethod will convert that num-ber to a string for display. Thus, the line of code we need to add to the event handler is

Figure 3.16 Adding a Timerto the form.

numberDisplay.Text =

"Number selected " + number.Value.ToString();

The completed event-handling code for the Showbutton is

private void showButton_Click

(object sender, System.EventArgs e) {

dateDisplay.Text =

"Date selected: " + date.Value.ToLongDateString();

numberDisplay.Text =

"Number selected " + number.Value.ToString();

}

3.3.4

StatusBar

AStatusBardisplays text. By default, it is docked to the bottom of the form so that it remains there during resizing. We set the initial value of the Text

property to the empty string, but the timer introduced next will copy the current time every second. Thus the status bar functions like a digital clock showing both the date and the time.

3.3.5

Timer

ATimergenerates a Tickevent at a set interval, say, every second. It does not have a visual representation in the form of Example3-6 shown in Figure 3.14. The Visual Studio .NET design shows it below the form as shown in Figure 3.16.

We set the Intervalproperty to 1000 milliseconds, or one second, to specify the frequency of the Tick event. When the tick event occurs we want to show the current time in a StatusBarat the bottom of the form. We also want to change the background colors of the two Labelcontrols and the

A DateTimePicker allows the user to select a date and time and to dis-play it in a specific format.

ANumericUpDown rep-resents a Windows up-down control that displays numeric values. A Timer generates a Tickevent at a set interval, say, every second. A StatusBar displays text.

The

BIG

Picture

3.3 Keeping Track 67

NumericUpDownto create a flashing effect. Double-clicking on the Timer con-trol displays the event-handling template.

private void timer1_Tick(object sender, System.EventArgs e) {

}

First we update the time in the StatusBarthat we named currentDateTime. TheNow property of the DateTimetype gives the current time. We call the

ToString method to get a string representation that we copy to the Text

property of the StatusBar. The first line of code we add to the event-han-dling method for the Timeris

currentDateTime.Text = DateTime.Now.ToString();

Next we want to use the Timer to change the background colors of three controls every second. In Figure 3.14 we see that the dateDisplaycontrol has a yellow background while the numberandnumberDisplaycontrols have white backgrounds. The code

number.BackColor = dateDisplay.BackColor;

dateDisplay.BackColor = numberDisplay.BackColor;

numberDisplay.BackColor = number.BackColor;

will change the dateDisplayto white and the numberandnumberDisplay con-trols to yellow. The next time it is executed it will revert the concon-trols back to their original colors. Running this code every second will flash back and forth between these two patterns. Thus we insert this code into the event handler for the Timercontrol that will be executed every second. The com-pleted Tickevent-handling code is

private void timer1_Tick(object sender, System.EventArgs e) {

11. Which control used in Example3-6 does not have a visual repre-sentation on the form?

12. Which property controls whether a DateTimePickerwill provide a drop-down calendar?

Figure 3.17 Adding a main menu.

13. How would you configure a NumericUpDownto allow values such as prices, which have two places after the decimal point?

14. What Intervalshould a Timeruse to generate a Tickevent four times per second?

In document c#.net (Page 80-83)

Related documents